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>
<TargetFrameworks>netstandard2.0;$(DefaultNetCoreTargetFramework)</TargetFrameworks>
<TargetFrameworks Condition="'$(DotNetBuildFromSource)' == 'true'">$(DefaultNetCoreTargetFramework)</TargetFrameworks>
<Nullable>annotations</Nullable>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<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 bool Return(T obj) { throw null; }
}
public partial interface IPooledObjectPolicy<T>
public partial interface IPooledObjectPolicy<T> where T : notnull
{
T Create();
bool Return(T obj);
@ -40,7 +40,7 @@ namespace Microsoft.Extensions.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
{
@ -59,7 +59,7 @@ namespace Microsoft.Extensions.ObjectPool
public abstract T Get();
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() { }
public abstract T Create();

View File

@ -22,7 +22,7 @@ namespace Microsoft.Extensions.ObjectPool
public override T Create() { 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();
bool Return(T obj);
@ -40,7 +40,7 @@ namespace Microsoft.Extensions.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
{
@ -59,7 +59,7 @@ namespace Microsoft.Extensions.ObjectPool
public abstract T Get();
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() { }
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.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Threading;
@ -18,10 +19,10 @@ namespace Microsoft.Extensions.ObjectPool
private protected readonly ObjectWrapper[] _items;
private protected readonly IPooledObjectPolicy<T> _policy;
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
private protected readonly PooledObjectPolicy<T> _fastPolicy;
private protected readonly PooledObjectPolicy<T>? _fastPolicy;
/// <summary>
/// Creates an instance of <see cref="DefaultObjectPool{T}"/>.
@ -97,7 +98,7 @@ namespace Microsoft.Extensions.ObjectPool
[DebuggerDisplay("{Element}")]
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.
using System;
using System.Runtime.CompilerServices;
using System.Threading;
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)
{

View File

@ -7,7 +7,7 @@ namespace Microsoft.Extensions.ObjectPool
/// Represents a policy for managing pooled objects.
/// </summary>
/// <typeparam name="T">The type of object which is being pooled.</typeparam>
public interface IPooledObjectPolicy<T>
public interface IPooledObjectPolicy<T> where T : notnull
{
/// <summary>
/// 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.
using System;
@ -31,8 +31,7 @@ namespace Microsoft.Extensions.ObjectPool
public override void Return(T obj)
{
Tracker tracker;
if (_trackers.TryGetValue(obj, out tracker))
if (_trackers.TryGetValue(obj, out var tracker))
{
_trackers.Remove(obj);
tracker.Dispose();

View File

@ -8,6 +8,7 @@
<PackageTags>pooling</PackageTags>
<IsPackable>true</IsPackable>
<IsAspNetCoreApp>true</IsAspNetCoreApp>
<Nullable>enable</Nullable>
</PropertyGroup>
<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.
using System.Diagnostics.CodeAnalysis;
namespace Microsoft.Extensions.ObjectPool
{
/// <summary>
@ -28,7 +30,7 @@ namespace Microsoft.Extensions.ObjectPool
public static class ObjectPool
{
/// <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();
return provider.Create(policy ?? new DefaultPooledObjectPolicy<T>());

View File

@ -3,7 +3,7 @@
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();

View File

@ -2,6 +2,7 @@
<PropertyGroup>
<TargetFrameworks>$(DefaultNetCoreTargetFramework);net472</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>
<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.
using System.Threading;
@ -8,8 +8,8 @@ namespace Microsoft.Extensions.ObjectPool
{
public class ThreadingTest
{
private CancellationTokenSource _cts;
private DefaultObjectPool<Item> _pool;
private CancellationTokenSource _cts = default!;
private DefaultObjectPool<Item> _pool = default!;
private bool _foundError;
[Fact]