Skip to content

Commit b6bde6c

Browse files
authored
Annotate ObjectPool with nullable (#22823)
* Annotate ObjectPool with nullable * fixup
1 parent 00bba2e commit b6bde6c

12 files changed

+27
-23
lines changed

src/ObjectPool/ref/Microsoft.Extensions.ObjectPool.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;$(DefaultNetCoreTargetFramework)</TargetFrameworks>
55
<TargetFrameworks Condition="'$(DotNetBuildFromSource)' == 'true'">$(DefaultNetCoreTargetFramework)</TargetFrameworks>
6+
<Nullable>annotations</Nullable>
67
</PropertyGroup>
78
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
89
<Compile Include="Microsoft.Extensions.ObjectPool.netstandard2.0.cs" />

src/ObjectPool/ref/Microsoft.Extensions.ObjectPool.netcoreapp.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public DefaultPooledObjectPolicy() { }
2222
public override T Create() { throw null; }
2323
public override bool Return(T obj) { throw null; }
2424
}
25-
public partial interface IPooledObjectPolicy<T>
25+
public partial interface IPooledObjectPolicy<T> where T : notnull
2626
{
2727
T Create();
2828
bool Return(T obj);
@@ -40,7 +40,7 @@ public override void Return(T obj) { }
4040
}
4141
public static partial class ObjectPool
4242
{
43-
public static Microsoft.Extensions.ObjectPool.ObjectPool<T> Create<T>(Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T> policy = null) where T : class, new() { throw null; }
43+
public static Microsoft.Extensions.ObjectPool.ObjectPool<T> Create<T>(Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T>? policy = null) where T : class, new() { throw null; }
4444
}
4545
public abstract partial class ObjectPoolProvider
4646
{
@@ -59,7 +59,7 @@ protected ObjectPool() { }
5959
public abstract T Get();
6060
public abstract void Return(T obj);
6161
}
62-
public abstract partial class PooledObjectPolicy<T> : Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T>
62+
public abstract partial class PooledObjectPolicy<T> : Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T> where T : notnull
6363
{
6464
protected PooledObjectPolicy() { }
6565
public abstract T Create();

src/ObjectPool/ref/Microsoft.Extensions.ObjectPool.netstandard2.0.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public DefaultPooledObjectPolicy() { }
2222
public override T Create() { throw null; }
2323
public override bool Return(T obj) { throw null; }
2424
}
25-
public partial interface IPooledObjectPolicy<T>
25+
public partial interface IPooledObjectPolicy<T> where T : notnull
2626
{
2727
T Create();
2828
bool Return(T obj);
@@ -40,7 +40,7 @@ public override void Return(T obj) { }
4040
}
4141
public static partial class ObjectPool
4242
{
43-
public static Microsoft.Extensions.ObjectPool.ObjectPool<T> Create<T>(Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T> policy = null) where T : class, new() { throw null; }
43+
public static Microsoft.Extensions.ObjectPool.ObjectPool<T> Create<T>(Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T>? policy = null) where T : class, new() { throw null; }
4444
}
4545
public abstract partial class ObjectPoolProvider
4646
{
@@ -59,7 +59,7 @@ protected ObjectPool() { }
5959
public abstract T Get();
6060
public abstract void Return(T obj);
6161
}
62-
public abstract partial class PooledObjectPolicy<T> : Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T>
62+
public abstract partial class PooledObjectPolicy<T> : Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T> where T : notnull
6363
{
6464
protected PooledObjectPolicy() { }
6565
public abstract T Create();

src/ObjectPool/src/DefaultObjectPool.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
55
using System.Diagnostics;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Runtime.CompilerServices;
78
using System.Threading;
89

@@ -18,10 +19,10 @@ public class DefaultObjectPool<T> : ObjectPool<T> where T : class
1819
private protected readonly ObjectWrapper[] _items;
1920
private protected readonly IPooledObjectPolicy<T> _policy;
2021
private protected readonly bool _isDefaultPolicy;
21-
private protected T _firstItem;
22+
private protected T? _firstItem;
2223

2324
// This class was introduced in 2.1 to avoid the interface call where possible
24-
private protected readonly PooledObjectPolicy<T> _fastPolicy;
25+
private protected readonly PooledObjectPolicy<T>? _fastPolicy;
2526

2627
/// <summary>
2728
/// Creates an instance of <see cref="DefaultObjectPool{T}"/>.
@@ -97,7 +98,7 @@ public override void Return(T obj)
9798
[DebuggerDisplay("{Element}")]
9899
private protected struct ObjectWrapper
99100
{
100-
public T Element;
101+
public T? Element;
101102
}
102103
}
103104
}

src/ObjectPool/src/DisposableObjectPool.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Runtime.CompilerServices;
65
using System.Threading;
76

87
namespace Microsoft.Extensions.ObjectPool
@@ -82,7 +81,7 @@ public void Dispose()
8281
}
8382
}
8483

85-
private void DisposeItem(T item)
84+
private void DisposeItem(T? item)
8685
{
8786
if (item is IDisposable disposable)
8887
{

src/ObjectPool/src/IPooledObjectPolicy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Microsoft.Extensions.ObjectPool
77
/// Represents a policy for managing pooled objects.
88
/// </summary>
99
/// <typeparam name="T">The type of object which is being pooled.</typeparam>
10-
public interface IPooledObjectPolicy<T>
10+
public interface IPooledObjectPolicy<T> where T : notnull
1111
{
1212
/// <summary>
1313
/// Create a <typeparamref name="T"/>.

src/ObjectPool/src/LeakTrackingObjectPool.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -31,8 +31,7 @@ public override T Get()
3131

3232
public override void Return(T obj)
3333
{
34-
Tracker tracker;
35-
if (_trackers.TryGetValue(obj, out tracker))
34+
if (_trackers.TryGetValue(obj, out var tracker))
3635
{
3736
_trackers.Remove(obj);
3837
tracker.Dispose();

src/ObjectPool/src/Microsoft.Extensions.ObjectPool.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<PackageTags>pooling</PackageTags>
99
<IsPackable>true</IsPackable>
1010
<IsAspNetCoreApp>true</IsAspNetCoreApp>
11+
<Nullable>enable</Nullable>
1112
</PropertyGroup>
1213

1314
<ItemGroup>

src/ObjectPool/src/ObjectPool.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
46
namespace Microsoft.Extensions.ObjectPool
57
{
68
/// <summary>
@@ -28,7 +30,7 @@ public abstract class ObjectPool<T> where T : class
2830
public static class ObjectPool
2931
{
3032
/// <inheritdoc />
31-
public static ObjectPool<T> Create<T>(IPooledObjectPolicy<T> policy = null) where T : class, new()
33+
public static ObjectPool<T> Create<T>(IPooledObjectPolicy<T>? policy = null) where T : class, new()
3234
{
3335
var provider = new DefaultObjectPoolProvider();
3436
return provider.Create(policy ?? new DefaultPooledObjectPolicy<T>());

src/ObjectPool/src/PooledObjectPolicy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Microsoft.Extensions.ObjectPool
55
{
6-
public abstract class PooledObjectPolicy<T> : IPooledObjectPolicy<T>
6+
public abstract class PooledObjectPolicy<T> : IPooledObjectPolicy<T> where T : notnull
77
{
88
public abstract T Create();
99

src/ObjectPool/test/Microsoft.Extensions.ObjectPool.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>$(DefaultNetCoreTargetFramework);net472</TargetFrameworks>
5+
<Nullable>enable</Nullable>
56
</PropertyGroup>
67

78
<ItemGroup>

src/ObjectPool/test/ThreadingTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.Threading;
@@ -8,8 +8,8 @@ namespace Microsoft.Extensions.ObjectPool
88
{
99
public class ThreadingTest
1010
{
11-
private CancellationTokenSource _cts;
12-
private DefaultObjectPool<Item> _pool;
11+
private CancellationTokenSource _cts = default!;
12+
private DefaultObjectPool<Item> _pool = default!;
1313
private bool _foundError;
1414

1515
[Fact]

0 commit comments

Comments
 (0)