Skip to content

Commit 6198481

Browse files
committed
Enable nullable reference types and annotate codebase
Enabled C# nullable reference types and updated all relevant code to use nullable annotations and null-forgiving operators where appropriate. Constructors, method signatures, and interface implementations now accept nullable parameters as needed. Updated default assignments and enumerator implementations to avoid nullable warnings. Added [DoesNotReturn] to ThrowHelper methods and improved exception handling for nullability. These changes enhance code safety and compatibility with modern C# nullability analysis.
1 parent ac16b60 commit 6198481

12 files changed

Lines changed: 377 additions & 333 deletions

Collections.Pooled/Collections.Pooled.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<IncludeContentInPack>true</IncludeContentInPack>
3838
<GenerateDocumentationFile>true</GenerateDocumentationFile>
3939
<IncludeSymbols>false</IncludeSymbols>
40+
<Nullable>enable</Nullable>
4041
</PropertyGroup>
4142

4243
</Project>

Collections.Pooled/HashHelpers.SerializationInfoTable.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
// Used by Hashtable and Dictionary's SeralizationInfo .ctor's to store the SeralizationInfo
66
// object until OnDeserialization is called.
77

8-
using System.Threading;
98
using System.Runtime.CompilerServices;
109
using System.Runtime.Serialization;
10+
using System.Threading;
1111

1212
namespace Collections.Pooled
1313
{
1414
internal static partial class HashHelpers
15-
{
16-
private static ConditionalWeakTable<object, SerializationInfo> s_serializationInfoTable;
15+
{
16+
private static ConditionalWeakTable<object, SerializationInfo>? s_serializationInfoTable;
1717

1818
public static ConditionalWeakTable<object, SerializationInfo> SerializationInfoTable
1919
{

Collections.Pooled/NonRandomizedStringEqualityComparer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Diagnostics;
8-
using System.Runtime.CompilerServices;
98
using System.Runtime.Serialization;
109

1110
namespace Collections.Pooled
@@ -30,7 +29,7 @@ private NonRandomizedStringEqualityComparer(SerializationInfo information, Strea
3029

3130
public sealed override bool Equals(string x, string y) => string.Equals(x, y);
3231

33-
public sealed override int GetHashCode(string str)
32+
public sealed override int GetHashCode(string str)
3433
=> str is null ? 0 : str.Length == 0 ? s_empyStringHashCode : GetNonRandomizedHashCode(str);
3534

3635
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)

Collections.Pooled/PooledDictionary.cs

Lines changed: 112 additions & 112 deletions
Large diffs are not rendered by default.

Collections.Pooled/PooledExtensions.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static PooledList<T> ToPooledList<T>(this Memory<T> memory)
4242
public static PooledDictionary<TKey, TValue> ToPooledDictionary<TSource, TKey, TValue>(
4343
this IEnumerable<TSource> source,
4444
Func<TSource, TKey> keySelector, Func<TSource, TValue> valueSelector,
45-
IEqualityComparer<TKey> comparer = null)
45+
IEqualityComparer<TKey>? comparer = null)
4646
{
4747
if (source == null)
4848
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
@@ -62,7 +62,7 @@ public static PooledDictionary<TKey, TValue> ToPooledDictionary<TSource, TKey, T
6262
public static PooledDictionary<TKey, TValue> ToPooledDictionary<TSource, TKey, TValue>(
6363
this ReadOnlySpan<TSource> source,
6464
Func<TSource, TKey> keySelector, Func<TSource, TValue> valueSelector,
65-
IEqualityComparer<TKey> comparer = null)
65+
IEqualityComparer<TKey>? comparer = null)
6666
{
6767
var dict = new PooledDictionary<TKey, TValue>(source.Length, comparer);
6868
foreach (var item in source)
@@ -112,7 +112,7 @@ public static PooledDictionary<TKey, TValue> ToPooledDictionary<TSource, TKey, T
112112
/// </summary>
113113
public static PooledDictionary<TKey, TSource> ToPooledDictionary<TSource, TKey>(
114114
this IEnumerable<TSource> source,
115-
Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer = null)
115+
Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer = null)
116116
{
117117
if (source == null)
118118
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
@@ -131,7 +131,7 @@ public static PooledDictionary<TKey, TSource> ToPooledDictionary<TSource, TKey>(
131131
/// </summary>
132132
public static PooledDictionary<TKey, TSource> ToPooledDictionary<TSource, TKey>(
133133
this ReadOnlySpan<TSource> source,
134-
Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer = null)
134+
Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer = null)
135135
{
136136
var dict = new PooledDictionary<TKey, TSource>(source.Length, comparer);
137137
foreach (var item in source)
@@ -147,7 +147,7 @@ public static PooledDictionary<TKey, TSource> ToPooledDictionary<TSource, TKey>(
147147
/// key selector and comparer.
148148
/// </summary>
149149
public static PooledDictionary<TKey, TSource> ToPooledDictionary<TSource, TKey>(this Span<TSource> source,
150-
Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer = null)
150+
Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer = null)
151151
{
152152
return ToPooledDictionary((ReadOnlySpan<TSource>)source, keySelector, comparer);
153153
}
@@ -158,7 +158,7 @@ public static PooledDictionary<TKey, TSource> ToPooledDictionary<TSource, TKey>(
158158
/// </summary>
159159
public static PooledDictionary<TKey, TSource> ToPooledDictionary<TSource, TKey>(
160160
this ReadOnlyMemory<TSource> source,
161-
Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer = null)
161+
Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer = null)
162162
{
163163
return ToPooledDictionary(source.Span, keySelector, comparer);
164164
}
@@ -168,7 +168,7 @@ public static PooledDictionary<TKey, TSource> ToPooledDictionary<TSource, TKey>(
168168
/// key selector and comparer.
169169
/// </summary>
170170
public static PooledDictionary<TKey, TSource> ToPooledDictionary<TSource, TKey>(this Memory<TSource> source,
171-
Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer = null)
171+
Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer = null)
172172
{
173173
return ToPooledDictionary(source.Span, keySelector, comparer);
174174
}
@@ -178,7 +178,7 @@ public static PooledDictionary<TKey, TSource> ToPooledDictionary<TSource, TKey>(
178178
/// </summary>
179179
public static PooledDictionary<TKey, TValue> ToPooledDictionary<TKey, TValue>(
180180
this IEnumerable<(TKey, TValue)> source,
181-
IEqualityComparer<TKey> comparer = null)
181+
IEqualityComparer<TKey>? comparer = null)
182182
{
183183
return new PooledDictionary<TKey, TValue>(source, comparer);
184184
}
@@ -188,7 +188,7 @@ public static PooledDictionary<TKey, TValue> ToPooledDictionary<TKey, TValue>(
188188
/// </summary>
189189
public static PooledDictionary<TKey, TValue> ToPooledDictionary<TKey, TValue>(
190190
this IEnumerable<KeyValuePair<TKey, TValue>> source,
191-
IEqualityComparer<TKey> comparer = null)
191+
IEqualityComparer<TKey>? comparer = null)
192192
{
193193
return new PooledDictionary<TKey, TValue>(source, comparer);
194194
}
@@ -198,7 +198,7 @@ public static PooledDictionary<TKey, TValue> ToPooledDictionary<TKey, TValue>(
198198
/// </summary>
199199
public static PooledDictionary<TKey, TValue> ToPooledDictionary<TKey, TValue>(
200200
this IEnumerable<Tuple<TKey, TValue>> source,
201-
IEqualityComparer<TKey> comparer = null)
201+
IEqualityComparer<TKey>? comparer = null)
202202
{
203203
if (source == null)
204204
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
@@ -217,7 +217,7 @@ public static PooledDictionary<TKey, TValue> ToPooledDictionary<TKey, TValue>(
217217
/// </summary>
218218
public static PooledDictionary<TKey, TValue> ToPooledDictionary<TKey, TValue>(
219219
this ReadOnlySpan<(TKey, TValue)> source,
220-
IEqualityComparer<TKey> comparer = null)
220+
IEqualityComparer<TKey>? comparer = null)
221221
{
222222
return new PooledDictionary<TKey, TValue>(source, comparer);
223223
}
@@ -226,7 +226,7 @@ public static PooledDictionary<TKey, TValue> ToPooledDictionary<TKey, TValue>(
226226
/// Creates a <see cref="PooledDictionary{TKey,TValue}"/> from a span of key/value tuples.
227227
/// </summary>
228228
public static PooledDictionary<TKey, TValue> ToPooledDictionary<TKey, TValue>(this Span<(TKey, TValue)> source,
229-
IEqualityComparer<TKey> comparer = null)
229+
IEqualityComparer<TKey>? comparer = null)
230230
{
231231
return new PooledDictionary<TKey, TValue>(source, comparer);
232232
}
@@ -235,19 +235,19 @@ public static PooledDictionary<TKey, TValue> ToPooledDictionary<TKey, TValue>(th
235235

236236
#region PooledSet
237237

238-
public static PooledSet<T> ToPooledSet<T>(this IEnumerable<T> source, IEqualityComparer<T> comparer = null)
238+
public static PooledSet<T> ToPooledSet<T>(this IEnumerable<T> source, IEqualityComparer<T>? comparer = null)
239239
=> new PooledSet<T>(source, comparer);
240240

241-
public static PooledSet<T> ToPooledSet<T>(this Span<T> source, IEqualityComparer<T> comparer = null)
241+
public static PooledSet<T> ToPooledSet<T>(this Span<T> source, IEqualityComparer<T>? comparer = null)
242242
=> new PooledSet<T>(source, comparer);
243243

244-
public static PooledSet<T> ToPooledSet<T>(this ReadOnlySpan<T> source, IEqualityComparer<T> comparer = null)
244+
public static PooledSet<T> ToPooledSet<T>(this ReadOnlySpan<T> source, IEqualityComparer<T>? comparer = null)
245245
=> new PooledSet<T>(source, comparer);
246246

247-
public static PooledSet<T> ToPooledSet<T>(this Memory<T> source, IEqualityComparer<T> comparer = null)
247+
public static PooledSet<T> ToPooledSet<T>(this Memory<T> source, IEqualityComparer<T>? comparer = null)
248248
=> new PooledSet<T>(source.Span, comparer);
249249

250-
public static PooledSet<T> ToPooledSet<T>(this ReadOnlyMemory<T> source, IEqualityComparer<T> comparer = null)
250+
public static PooledSet<T> ToPooledSet<T>(this ReadOnlyMemory<T> source, IEqualityComparer<T>? comparer = null)
251251
=> new PooledSet<T>(source.Span, comparer);
252252

253253
#endregion

Collections.Pooled/PooledList.cs

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class PooledList<T> : IList<T>, IReadOnlyPooledList<T>, IList, IDisposabl
3737
private static readonly T[] s_emptyArray = Array.Empty<T>();
3838

3939
[NonSerialized] private ArrayPool<T> _pool;
40-
[NonSerialized] private object _syncRoot;
40+
[NonSerialized] private object? _syncRoot;
4141

4242
private T[] _items; // Do not rename (binary serialization)
4343
private int _size; // Do not rename (binary serialization)
@@ -334,59 +334,59 @@ public PooledList(IEnumerable<T> collection, ClearMode clearMode, ArrayPool<T> c
334334
break;
335335

336336
case ICollection<T> c:
337-
{
338-
int count = c.Count;
339-
if (count == 0)
340-
{
341-
_items = s_emptyArray;
342-
}
343-
else
344337
{
345-
_items = _pool.Rent(count);
346-
c.CopyTo(_items, 0);
347-
_size = count;
348-
}
338+
int count = c.Count;
339+
if (count == 0)
340+
{
341+
_items = s_emptyArray;
342+
}
343+
else
344+
{
345+
_items = _pool.Rent(count);
346+
c.CopyTo(_items, 0);
347+
_size = count;
348+
}
349349

350-
break;
351-
}
350+
break;
351+
}
352352

353353
case ICollection c:
354-
{
355-
int count = c.Count;
356-
if (count == 0)
357354
{
358-
_items = s_emptyArray;
359-
}
360-
else
361-
{
362-
_items = _pool.Rent(count);
363-
c.CopyTo(_items, 0);
364-
_size = count;
365-
}
355+
int count = c.Count;
356+
if (count == 0)
357+
{
358+
_items = s_emptyArray;
359+
}
360+
else
361+
{
362+
_items = _pool.Rent(count);
363+
c.CopyTo(_items, 0);
364+
_size = count;
365+
}
366366

367-
break;
368-
}
367+
break;
368+
}
369369

370370
case IReadOnlyCollection<T> c:
371-
{
372-
int count = c.Count;
373-
if (count == 0)
374-
{
375-
_items = s_emptyArray;
376-
}
377-
else
378371
{
379-
_items = _pool.Rent(count);
380-
_size = 0;
381-
using (var en = c.GetEnumerator())
372+
int count = c.Count;
373+
if (count == 0)
382374
{
383-
while (en.MoveNext())
384-
Add(en.Current);
375+
_items = s_emptyArray;
376+
}
377+
else
378+
{
379+
_items = _pool.Rent(count);
380+
_size = 0;
381+
using (var en = c.GetEnumerator())
382+
{
383+
while (en.MoveNext())
384+
Add(en.Current);
385+
}
385386
}
386-
}
387387

388-
break;
389-
}
388+
break;
389+
}
390390

391391
default:
392392

@@ -490,7 +490,7 @@ object ICollection.SyncRoot
490490
{
491491
if (_syncRoot == null)
492492
{
493-
Interlocked.CompareExchange<object>(ref _syncRoot, new object(), null);
493+
Interlocked.CompareExchange<object?>(ref _syncRoot, new object(), null);
494494
}
495495

496496
return _syncRoot;
@@ -532,20 +532,20 @@ private static bool IsCompatibleObject(object value)
532532
return ((value is T) || (value == null && default(T) == null));
533533
}
534534

535-
object IList.this[int index]
535+
object? IList.this[int index]
536536
{
537537
get { return this[index]; }
538538
set
539539
{
540-
ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(value, ExceptionArgument.value);
540+
ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(value!, ExceptionArgument.value);
541541

542542
try
543543
{
544-
this[index] = (T)value;
544+
this[index] = (T)value!;
545545
}
546546
catch (InvalidCastException)
547547
{
548-
ThrowHelper.ThrowWrongValueTypeArgumentException(value, typeof(T));
548+
ThrowHelper.ThrowWrongValueTypeArgumentException(value!, typeof(T));
549549
}
550550
}
551551
}
@@ -677,7 +677,7 @@ public int BinarySearch(int index, int count, T item, IComparer<T> comparer)
677677
/// then that is used for comparison, otherwise <see cref="Comparer{T}.Default"/> is used.
678678
/// </summary>
679679
public int BinarySearch(T item)
680-
=> BinarySearch(0, Count, item, null);
680+
=> BinarySearch(0, Count, item, null!);
681681

682682
/// <summary>
683683
/// Searches the list for a given element using a binary search
@@ -821,7 +821,7 @@ public bool TryFind(Func<T, bool> match, out T result)
821821
}
822822
}
823823

824-
result = default;
824+
result = default!;
825825
return false;
826826
}
827827

@@ -884,7 +884,7 @@ public bool TryFindLast(Func<T, bool> match, out T result)
884884
}
885885
}
886886

887-
result = default;
887+
result = default!;
888888
return false;
889889
}
890890

@@ -1350,7 +1350,7 @@ public void RemoveAt(int index)
13501350
if (_clearOnFree)
13511351
{
13521352
// Clear the removed element so that the gc can reclaim the reference.
1353-
_items[_size] = default;
1353+
_items[_size] = default!;
13541354
}
13551355
}
13561356

@@ -1424,7 +1424,7 @@ public void Reverse(int index, int count)
14241424
/// Array.Sort.
14251425
/// </summary>
14261426
public void Sort()
1427-
=> Sort(0, Count, null);
1427+
=> Sort(0, Count, null!);
14281428

14291429
/// <summary>
14301430
/// Sorts the elements in this list. Uses Array.Sort with the
@@ -1587,7 +1587,7 @@ internal Enumerator(PooledList<T> list)
15871587
_list = list;
15881588
_index = 0;
15891589
_version = list._version;
1590-
_current = default;
1590+
_current = default!;
15911591
}
15921592

15931593
public void Dispose()
@@ -1616,13 +1616,13 @@ private bool MoveNextRare()
16161616
}
16171617

16181618
_index = _list._size + 1;
1619-
_current = default;
1619+
_current = default!;
16201620
return false;
16211621
}
16221622

16231623
public T Current => _current;
16241624

1625-
object IEnumerator.Current
1625+
object? IEnumerator.Current
16261626
{
16271627
get
16281628
{
@@ -1643,7 +1643,7 @@ void IEnumerator.Reset()
16431643
}
16441644

16451645
_index = 0;
1646-
_current = default;
1646+
_current = default!;
16471647
}
16481648
}
16491649

0 commit comments

Comments
 (0)