Annotate ObjectPool with nullable (#22823)

* Annotate ObjectPool with nullable

* fixup
This commit is contained in:
Pranav K 2020-06-16 12:43:24 -07:00 committed by GitHub
parent 00bba2e6d6
commit b6bde6cbc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 27 additions and 23 deletions

View File

@ -3,6 +3,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>netstandard2.0;$(DefaultNetCoreTargetFramework)</TargetFrameworks> <TargetFrameworks>netstandard2.0;$(DefaultNetCoreTargetFramework)</TargetFrameworks>
<TargetFrameworks Condition="'$(DotNetBuildFromSource)' == 'true'">$(DefaultNetCoreTargetFramework)</TargetFrameworks> <TargetFrameworks Condition="'$(DotNetBuildFromSource)' == 'true'">$(DefaultNetCoreTargetFramework)</TargetFrameworks>
<Nullable>annotations</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'"> <ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<Compile Include="Microsoft.Extensions.ObjectPool.netstandard2.0.cs" /> <Compile Include="Microsoft.Extensions.ObjectPool.netstandard2.0.cs" />

View File

@ -22,7 +22,7 @@ namespace Microsoft.Extensions.ObjectPool
public override T Create() { throw null; } public override T Create() { throw null; }
public override bool Return(T obj) { throw null; } public override bool Return(T obj) { throw null; }
} }
public partial interface IPooledObjectPolicy<T> public partial interface IPooledObjectPolicy<T> where T : notnull
{ {
T Create(); T Create();
bool Return(T obj); bool Return(T obj);
@ -40,7 +40,7 @@ namespace Microsoft.Extensions.ObjectPool
} }
public static partial class ObjectPool public static partial class ObjectPool
{ {
public static Microsoft.Extensions.ObjectPool.ObjectPool<T> Create<T>(Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T> policy = null) where T : class, new() { throw null; } public static Microsoft.Extensions.ObjectPool.ObjectPool<T> Create<T>(Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T>? policy = null) where T : class, new() { throw null; }
} }
public abstract partial class ObjectPoolProvider public abstract partial class ObjectPoolProvider
{ {
@ -59,7 +59,7 @@ namespace Microsoft.Extensions.ObjectPool
public abstract T Get(); public abstract T Get();
public abstract void Return(T obj); public abstract void Return(T obj);
} }
public abstract partial class PooledObjectPolicy<T> : Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T> public abstract partial class PooledObjectPolicy<T> : Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T> where T : notnull
{ {
protected PooledObjectPolicy() { } protected PooledObjectPolicy() { }
public abstract T Create(); public abstract T Create();

View File

@ -22,7 +22,7 @@ namespace Microsoft.Extensions.ObjectPool
public override T Create() { throw null; } public override T Create() { throw null; }
public override bool Return(T obj) { throw null; } public override bool Return(T obj) { throw null; }
} }
public partial interface IPooledObjectPolicy<T> public partial interface IPooledObjectPolicy<T> where T : notnull
{ {
T Create(); T Create();
bool Return(T obj); bool Return(T obj);
@ -40,7 +40,7 @@ namespace Microsoft.Extensions.ObjectPool
} }
public static partial class ObjectPool public static partial class ObjectPool
{ {
public static Microsoft.Extensions.ObjectPool.ObjectPool<T> Create<T>(Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T> policy = null) where T : class, new() { throw null; } public static Microsoft.Extensions.ObjectPool.ObjectPool<T> Create<T>(Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T>? policy = null) where T : class, new() { throw null; }
} }
public abstract partial class ObjectPoolProvider public abstract partial class ObjectPoolProvider
{ {
@ -59,7 +59,7 @@ namespace Microsoft.Extensions.ObjectPool
public abstract T Get(); public abstract T Get();
public abstract void Return(T obj); public abstract void Return(T obj);
} }
public abstract partial class PooledObjectPolicy<T> : Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T> public abstract partial class PooledObjectPolicy<T> : Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T> where T : notnull
{ {
protected PooledObjectPolicy() { } protected PooledObjectPolicy() { }
public abstract T Create(); public abstract T Create();

View File

@ -1,8 +1,9 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Threading; using System.Threading;
@ -18,10 +19,10 @@ namespace Microsoft.Extensions.ObjectPool
private protected readonly ObjectWrapper[] _items; private protected readonly ObjectWrapper[] _items;
private protected readonly IPooledObjectPolicy<T> _policy; private protected readonly IPooledObjectPolicy<T> _policy;
private protected readonly bool _isDefaultPolicy; private protected readonly bool _isDefaultPolicy;
private protected T _firstItem; private protected T? _firstItem;
// This class was introduced in 2.1 to avoid the interface call where possible // This class was introduced in 2.1 to avoid the interface call where possible
private protected readonly PooledObjectPolicy<T> _fastPolicy; private protected readonly PooledObjectPolicy<T>? _fastPolicy;
/// <summary> /// <summary>
/// Creates an instance of <see cref="DefaultObjectPool{T}"/>. /// Creates an instance of <see cref="DefaultObjectPool{T}"/>.
@ -97,7 +98,7 @@ namespace Microsoft.Extensions.ObjectPool
[DebuggerDisplay("{Element}")] [DebuggerDisplay("{Element}")]
private protected struct ObjectWrapper private protected struct ObjectWrapper
{ {
public T Element; public T? Element;
} }
} }
} }

View File

@ -1,8 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Runtime.CompilerServices;
using System.Threading; using System.Threading;
namespace Microsoft.Extensions.ObjectPool namespace Microsoft.Extensions.ObjectPool
@ -82,7 +81,7 @@ namespace Microsoft.Extensions.ObjectPool
} }
} }
private void DisposeItem(T item) private void DisposeItem(T? item)
{ {
if (item is IDisposable disposable) if (item is IDisposable disposable)
{ {

View File

@ -7,7 +7,7 @@ namespace Microsoft.Extensions.ObjectPool
/// Represents a policy for managing pooled objects. /// Represents a policy for managing pooled objects.
/// </summary> /// </summary>
/// <typeparam name="T">The type of object which is being pooled.</typeparam> /// <typeparam name="T">The type of object which is being pooled.</typeparam>
public interface IPooledObjectPolicy<T> public interface IPooledObjectPolicy<T> where T : notnull
{ {
/// <summary> /// <summary>
/// Create a <typeparamref name="T"/>. /// Create a <typeparamref name="T"/>.

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
@ -31,8 +31,7 @@ namespace Microsoft.Extensions.ObjectPool
public override void Return(T obj) public override void Return(T obj)
{ {
Tracker tracker; if (_trackers.TryGetValue(obj, out var tracker))
if (_trackers.TryGetValue(obj, out tracker))
{ {
_trackers.Remove(obj); _trackers.Remove(obj);
tracker.Dispose(); tracker.Dispose();

View File

@ -8,6 +8,7 @@
<PackageTags>pooling</PackageTags> <PackageTags>pooling</PackageTags>
<IsPackable>true</IsPackable> <IsPackable>true</IsPackable>
<IsAspNetCoreApp>true</IsAspNetCoreApp> <IsAspNetCoreApp>true</IsAspNetCoreApp>
<Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Diagnostics.CodeAnalysis;
namespace Microsoft.Extensions.ObjectPool namespace Microsoft.Extensions.ObjectPool
{ {
/// <summary> /// <summary>
@ -28,7 +30,7 @@ namespace Microsoft.Extensions.ObjectPool
public static class ObjectPool public static class ObjectPool
{ {
/// <inheritdoc /> /// <inheritdoc />
public static ObjectPool<T> Create<T>(IPooledObjectPolicy<T> policy = null) where T : class, new() public static ObjectPool<T> Create<T>(IPooledObjectPolicy<T>? policy = null) where T : class, new()
{ {
var provider = new DefaultObjectPoolProvider(); var provider = new DefaultObjectPoolProvider();
return provider.Create(policy ?? new DefaultPooledObjectPolicy<T>()); return provider.Create(policy ?? new DefaultPooledObjectPolicy<T>());

View File

@ -3,7 +3,7 @@
namespace Microsoft.Extensions.ObjectPool namespace Microsoft.Extensions.ObjectPool
{ {
public abstract class PooledObjectPolicy<T> : IPooledObjectPolicy<T> public abstract class PooledObjectPolicy<T> : IPooledObjectPolicy<T> where T : notnull
{ {
public abstract T Create(); public abstract T Create();

View File

@ -2,6 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>$(DefaultNetCoreTargetFramework);net472</TargetFrameworks> <TargetFrameworks>$(DefaultNetCoreTargetFramework);net472</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading; using System.Threading;
@ -8,8 +8,8 @@ namespace Microsoft.Extensions.ObjectPool
{ {
public class ThreadingTest public class ThreadingTest
{ {
private CancellationTokenSource _cts; private CancellationTokenSource _cts = default!;
private DefaultObjectPool<Item> _pool; private DefaultObjectPool<Item> _pool = default!;
private bool _foundError; private bool _foundError;
[Fact] [Fact]