Skip to content

Commit 1e55401

Browse files
authored
Nullability: add key constraint (#536)
1 parent 0b629e0 commit 1e55401

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+103
-40
lines changed

BitFaster.Caching/Atomic/AsyncAtomicFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace BitFaster.Caching.Atomic
1414
/// <typeparam name="V">The type of the value.</typeparam>
1515
[DebuggerDisplay("IsValueCreated={IsValueCreated}, Value={ValueIfCreated}")]
1616
public sealed class AsyncAtomicFactory<K, V> : IEquatable<AsyncAtomicFactory<K, V>>
17+
where K : notnull
1718
{
1819
private Initializer initializer;
1920

BitFaster.Caching/Atomic/AtomicFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace BitFaster.Caching.Atomic
1414
/// <typeparam name="V">The type of the value.</typeparam>
1515
[DebuggerDisplay("IsValueCreated={IsValueCreated}, Value={ValueIfCreated}")]
1616
public sealed class AtomicFactory<K, V> : IEquatable<AtomicFactory<K, V>>
17+
where K : notnull
1718
{
1819
private Initializer initializer;
1920

BitFaster.Caching/Atomic/AtomicFactoryAsyncCache.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace BitFaster.Caching.Atomic
1515
[DebuggerTypeProxy(typeof(AtomicFactoryAsyncCache<,>.AsyncCacheDebugView))]
1616
[DebuggerDisplay("Count = {Count}")]
1717
public sealed class AtomicFactoryAsyncCache<K, V> : IAsyncCache<K, V>
18+
where K : notnull
1819
{
1920
private readonly ICache<K, AsyncAtomicFactory<K, V>> cache;
2021
private readonly Optional<ICacheEvents<K, V>> events;

BitFaster.Caching/Atomic/AtomicFactoryCache.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace BitFaster.Caching.Atomic
1313
[DebuggerTypeProxy(typeof(CacheDebugView<,>))]
1414
[DebuggerDisplay("Count = {Count}")]
1515
public sealed class AtomicFactoryCache<K, V> : ICache<K, V>
16+
where K : notnull
1617
{
1718
private readonly ICache<K, AtomicFactory<K, V>> cache;
1819
private readonly Optional<ICacheEvents<K, V>> events;

BitFaster.Caching/Atomic/AtomicFactoryScopedAsyncCache.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ namespace BitFaster.Caching.Atomic
1414
/// <typeparam name="V">The type of values in the cache.</typeparam>
1515
[DebuggerTypeProxy(typeof(ScopedAsyncCacheDebugView<,>))]
1616
[DebuggerDisplay("Count = {Count}")]
17-
public sealed class AtomicFactoryScopedAsyncCache<K, V> : IScopedAsyncCache<K, V> where V : IDisposable
17+
public sealed class AtomicFactoryScopedAsyncCache<K, V> : IScopedAsyncCache<K, V>
18+
where K : notnull
19+
where V : IDisposable
1820
{
1921
private readonly ICache<K, ScopedAsyncAtomicFactory<K, V>> cache;
2022
private readonly Optional<ICacheEvents<K, Scoped<V>>> events;

BitFaster.Caching/Atomic/AtomicFactoryScopedCache.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ namespace BitFaster.Caching.Atomic
1313
/// <typeparam name="V">The type of values in the cache.</typeparam>
1414
[DebuggerTypeProxy(typeof(ScopedCacheDebugView<,>))]
1515
[DebuggerDisplay("Count = {Count}")]
16-
public sealed class AtomicFactoryScopedCache<K, V> : IScopedCache<K, V> where V : IDisposable
16+
public sealed class AtomicFactoryScopedCache<K, V> : IScopedCache<K, V>
17+
where K : notnull
18+
where V : IDisposable
1719
{
1820
private readonly ICache<K, ScopedAtomicFactory<K, V>> cache;
1921
private readonly Optional<ICacheEvents<K, Scoped<V>>> events;

BitFaster.Caching/Atomic/ConcurrentDictionaryExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static class ConcurrentDictionaryExtensions
1818
/// <param name="valueFactory">The function used to generate a value for the key.</param>
1919
/// <returns>The value for the key. This will be either the existing value for the key if the key is already in the dictionary, or the new value if the key was not in the dictionary.</returns>
2020
public static V GetOrAdd<K, V>(this ConcurrentDictionary<K, AtomicFactory<K, V>> dictionary, K key, Func<K, V> valueFactory)
21+
where K : notnull
2122
{
2223
var atomicFactory = dictionary.GetOrAdd(key, _ => new AtomicFactory<K, V>());
2324
return atomicFactory.GetValue(key, valueFactory);
@@ -32,6 +33,7 @@ public static V GetOrAdd<K, V>(this ConcurrentDictionary<K, AtomicFactory<K, V>>
3233
/// <param name="factoryArgument">An argument value to pass into valueFactory.</param>
3334
/// <returns>The value for the key. This will be either the existing value for the key if the key is already in the dictionary, or the new value if the key was not in the dictionary.</returns>
3435
public static V GetOrAdd<K, V, TArg>(this ConcurrentDictionary<K, AtomicFactory<K, V>> dictionary, K key, Func<K, TArg, V> valueFactory, TArg factoryArgument)
36+
where K : notnull
3537
{
3638
var atomicFactory = dictionary.GetOrAdd(key, _ => new AtomicFactory<K, V>());
3739
return atomicFactory.GetValue(key, valueFactory, factoryArgument);
@@ -45,6 +47,7 @@ public static V GetOrAdd<K, V, TArg>(this ConcurrentDictionary<K, AtomicFactory<
4547
/// <param name="valueFactory">The function used to generate a value for the key.</param>
4648
/// <returns>The value for the key. This will be either the existing value for the key if the key is already in the dictionary, or the new value if the key was not in the dictionary.</returns>
4749
public static ValueTask<V> GetOrAddAsync<K, V>(this ConcurrentDictionary<K, AsyncAtomicFactory<K, V>> dictionary, K key, Func<K, Task<V>> valueFactory)
50+
where K : notnull
4851
{
4952
var asyncAtomicFactory = dictionary.GetOrAdd(key, _ => new AsyncAtomicFactory<K, V>());
5053
return asyncAtomicFactory.GetValueAsync(key, valueFactory);
@@ -59,6 +62,7 @@ public static ValueTask<V> GetOrAddAsync<K, V>(this ConcurrentDictionary<K, Asyn
5962
/// <param name="factoryArgument">An argument value to pass into valueFactory.</param>
6063
/// <returns>The value for the key. This will be either the existing value for the key if the key is already in the dictionary, or the new value if the key was not in the dictionary.</returns>
6164
public static ValueTask<V> GetOrAddAsync<K, V, TArg>(this ConcurrentDictionary<K, AsyncAtomicFactory<K, V>> dictionary, K key, Func<K, TArg, Task<V>> valueFactory, TArg factoryArgument)
65+
where K : notnull
6266
{
6367
var asyncAtomicFactory = dictionary.GetOrAdd(key, _ => new AsyncAtomicFactory<K, V>());
6468
return asyncAtomicFactory.GetValueAsync(key, valueFactory, factoryArgument);
@@ -72,6 +76,7 @@ public static ValueTask<V> GetOrAddAsync<K, V, TArg>(this ConcurrentDictionary<K
7276
/// <param name="value">When this method returns, contains the object from the ConcurrentDictionary that has the specified key, or the default value of the type if the operation failed.</param>
7377
/// <returns>true if the key was found in the ConcurrentDictionary; otherwise, false.</returns>
7478
public static bool TryGetValue<K, V>(this ConcurrentDictionary<K, AtomicFactory<K, V>> dictionary, K key, out V value)
79+
where K : notnull
7580
{
7681
AtomicFactory<K, V> output;
7782
var ret = dictionary.TryGetValue(key, out output);
@@ -93,6 +98,7 @@ public static bool TryGetValue<K, V>(this ConcurrentDictionary<K, AtomicFactory<
9398
/// <param name="key">The key of the value to get.</param>
9499
/// <param name="value">When this method returns, contains the object from the ConcurrentDictionary that has the specified key, or the default value of the type if the operation failed.</param>
95100
public static bool TryGetValue<K, V>(this ConcurrentDictionary<K, AsyncAtomicFactory<K, V>> dictionary, K key, out V value)
101+
where K : notnull
96102
{
97103
AsyncAtomicFactory<K, V> output;
98104
var ret = dictionary.TryGetValue(key, out output);
@@ -114,6 +120,7 @@ public static bool TryGetValue<K, V>(this ConcurrentDictionary<K, AsyncAtomicFac
114120
/// <param name="item">The KeyValuePair representing the key and value to remove.</param>
115121
/// <returns>true if the object was removed successfully; otherwise, false.</returns>
116122
public static bool TryRemove<K, V>(this ConcurrentDictionary<K, AtomicFactory<K, V>> dictionary, KeyValuePair<K, V> item)
123+
where K : notnull
117124
{
118125
var kvp = new KeyValuePair<K, AtomicFactory<K, V>>(item.Key, new AtomicFactory<K, V>(item.Value));
119126
#if NET6_0_OR_GREATER
@@ -131,6 +138,7 @@ public static bool TryRemove<K, V>(this ConcurrentDictionary<K, AtomicFactory<K,
131138
/// <param name="item">The KeyValuePair representing the key and value to remove.</param>
132139
/// <returns>true if the object was removed successfully; otherwise, false.</returns>
133140
public static bool TryRemove<K, V>(this ConcurrentDictionary<K, AsyncAtomicFactory<K, V>> dictionary, KeyValuePair<K, V> item)
141+
where K : notnull
134142
{
135143
var kvp = new KeyValuePair<K, AsyncAtomicFactory<K, V>>(item.Key, new AsyncAtomicFactory<K, V>(item.Value));
136144
#if NET6_0_OR_GREATER
@@ -149,6 +157,7 @@ public static bool TryRemove<K, V>(this ConcurrentDictionary<K, AsyncAtomicFacto
149157
/// <param name="value">When this method returns, contains the object removed from the ConcurrentDictionary, or the default value of the TValue type if key does not exist.</param>
150158
/// <returns>true if the object was removed successfully; otherwise, false.</returns>
151159
public static bool TryRemove<K, V>(this ConcurrentDictionary<K, AtomicFactory<K, V>> dictionary, K key, out V value)
160+
where K : notnull
152161
{
153162
if (dictionary.TryRemove(key, out var atomic))
154163
{
@@ -168,6 +177,7 @@ public static bool TryRemove<K, V>(this ConcurrentDictionary<K, AtomicFactory<K,
168177
/// <param name="value">When this method returns, contains the object removed from the ConcurrentDictionary, or the default value of the TValue type if key does not exist.</param>
169178
/// <returns>true if the object was removed successfully; otherwise, false.</returns>
170179
public static bool TryRemove<K, V>(this ConcurrentDictionary<K, AsyncAtomicFactory<K, V>> dictionary, K key, out V value)
180+
where K : notnull
171181
{
172182
if (dictionary.TryRemove(key, out var atomic))
173183
{

BitFaster.Caching/Atomic/ScopedAsyncAtomicFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ namespace BitFaster.Caching.Atomic
1212
/// <typeparam name="K">The type of the key.</typeparam>
1313
/// <typeparam name="V">The type of the value.</typeparam>
1414
[DebuggerDisplay("IsScopeCreated={initializer == null}, Value={ScopeIfCreated}")]
15-
public sealed class ScopedAsyncAtomicFactory<K, V> : IScoped<V>, IDisposable where V : IDisposable
15+
public sealed class ScopedAsyncAtomicFactory<K, V> : IScoped<V>, IDisposable
16+
where K : notnull
17+
where V : IDisposable
1618
{
1719
private Scoped<V> scope;
1820
private Initializer initializer;

BitFaster.Caching/Atomic/ScopedAtomicFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ namespace BitFaster.Caching.Atomic
2727
///</list>
2828
/// </remarks>
2929
[DebuggerDisplay("IsScopeCreated={initializer == null}, Value={ScopeIfCreated}")]
30-
public sealed class ScopedAtomicFactory<K, V> : IScoped<V>, IDisposable where V : IDisposable
30+
public sealed class ScopedAtomicFactory<K, V> : IScoped<V>, IDisposable
31+
where K : notnull
32+
where V : IDisposable
3133
{
3234
private Scoped<V> scope;
3335
private Initializer initializer;

BitFaster.Caching/BitFaster.Caching.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<GenerateDocumentationFile>true</GenerateDocumentationFile>
2828
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
2929
<IsTrimmable>true</IsTrimmable>
30+
<Nullable>disable</Nullable>
3031
<!--Package Validation-->
3132
<EnablePackageValidation>true</EnablePackageValidation>
3233
<PackageValidationBaselineVersion>2.4.0</PackageValidationBaselineVersion>

0 commit comments

Comments
 (0)