Skip to content

Commit 6339ed9

Browse files
authored
Nullability: enable by default and fix all errors (#537)
1 parent 1e55401 commit 6339ed9

Some content is hidden

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

51 files changed

+455
-215
lines changed

BitFaster.Caching.UnitTests/Atomic/AtomicFactoryAsyncCacheTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void WhenRemovedEventHandlerIsRegisteredItIsFired()
9191
this.removedItems.First().Key.Should().Be(1);
9292
}
9393

94-
// backcompat: remove conditional compile
94+
// backcompat: remove conditional compile
9595
#if NETCOREAPP3_0_OR_GREATER
9696
[Fact]
9797
public void WhenUpdatedEventHandlerIsRegisteredItIsFired()

BitFaster.Caching/Atomic/AsyncAtomicFactory.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ namespace BitFaster.Caching.Atomic
1616
public sealed class AsyncAtomicFactory<K, V> : IEquatable<AsyncAtomicFactory<K, V>>
1717
where K : notnull
1818
{
19-
private Initializer initializer;
19+
private Initializer? initializer;
2020

2121
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
22-
private V value;
22+
private V? value;
2323

2424
/// <summary>
2525
/// Initializes a new instance of the <see cref="AsyncAtomicFactory{K, V}"/> class.
@@ -49,7 +49,7 @@ public async ValueTask<V> GetValueAsync(K key, Func<K, Task<V>> valueFactory)
4949
{
5050
if (initializer == null)
5151
{
52-
return value;
52+
return value!;
5353
}
5454

5555
return await CreateValueAsync(key, new AsyncValueFactory<K, V>(valueFactory)).ConfigureAwait(false);
@@ -67,7 +67,7 @@ public async ValueTask<V> GetValueAsync<TArg>(K key, Func<K, TArg, Task<V>> valu
6767
{
6868
if (initializer == null)
6969
{
70-
return value;
70+
return value!;
7171
}
7272

7373
return await CreateValueAsync(key, new AsyncValueFactoryArg<K, TArg, V>(valueFactory, factoryArgument)).ConfigureAwait(false);
@@ -81,7 +81,7 @@ public async ValueTask<V> GetValueAsync<TArg>(K key, Func<K, TArg, Task<V>> valu
8181
/// <summary>
8282
/// Gets the value if it has been initialized, else default.
8383
/// </summary>
84-
public V ValueIfCreated
84+
public V? ValueIfCreated
8585
{
8686
get
8787
{
@@ -95,13 +95,13 @@ public V ValueIfCreated
9595
}
9696

9797
///<inheritdoc/>
98-
public override bool Equals(object obj)
98+
public override bool Equals(object? obj)
9999
{
100100
return Equals(obj as AsyncAtomicFactory<K, V>);
101101
}
102102

103103
///<inheritdoc/>
104-
public bool Equals(AsyncAtomicFactory<K, V> other)
104+
public bool Equals(AsyncAtomicFactory<K, V>? other)
105105
{
106106
if (other is null || !IsValueCreated || !other.IsValueCreated)
107107
{
@@ -119,7 +119,7 @@ public override int GetHashCode()
119119
return 0;
120120
}
121121

122-
return ValueIfCreated.GetHashCode();
122+
return ValueIfCreated!.GetHashCode();
123123
}
124124

125125
private async ValueTask<V> CreateValueAsync<TFactory>(K key, TFactory valueFactory) where TFactory : struct, IAsyncValueFactory<K, V>
@@ -132,13 +132,13 @@ private async ValueTask<V> CreateValueAsync<TFactory>(K key, TFactory valueFacto
132132
Volatile.Write(ref initializer, null);
133133
}
134134

135-
return value;
135+
return value!;
136136
}
137137

138138
private class Initializer
139139
{
140140
private bool isInitialized;
141-
private Task<V> valueTask;
141+
private Task<V>? valueTask;
142142

143143
public async ValueTask<V> CreateValueAsync<TFactory>(K key, TFactory valueFactory) where TFactory : struct, IAsyncValueFactory<K, V>
144144
{
@@ -172,7 +172,7 @@ private Task<V> DoubleCheck(Task<V> value)
172172
// Fast path
173173
if (Volatile.Read(ref isInitialized))
174174
{
175-
return valueTask;
175+
return valueTask!;
176176
}
177177

178178
lock (this)
@@ -184,7 +184,7 @@ private Task<V> DoubleCheck(Task<V> value)
184184
}
185185
}
186186

187-
return valueTask;
187+
return valueTask!;
188188
}
189189
#pragma warning restore CA2002 // Do not lock on objects with weak identity
190190
}

BitFaster.Caching/Atomic/AtomicFactory.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ namespace BitFaster.Caching.Atomic
1616
public sealed class AtomicFactory<K, V> : IEquatable<AtomicFactory<K, V>>
1717
where K : notnull
1818
{
19-
private Initializer initializer;
19+
private Initializer? initializer;
2020

2121
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
22-
private V value;
22+
private V? value;
2323

2424
/// <summary>
2525
/// Initializes a new instance of the <see cref="AtomicFactory{K, V}"/> class.
@@ -49,7 +49,7 @@ public V GetValue(K key, Func<K, V> valueFactory)
4949
{
5050
if (initializer == null)
5151
{
52-
return value;
52+
return value!;
5353
}
5454

5555
return CreateValue(key, new ValueFactory<K, V>(valueFactory));
@@ -67,7 +67,7 @@ public V GetValue<TArg>(K key, Func<K, TArg, V> valueFactory, TArg factoryArgume
6767
{
6868
if (initializer == null)
6969
{
70-
return value;
70+
return value!;
7171
}
7272

7373
return CreateValue(key, new ValueFactoryArg<K, TArg, V>(valueFactory, factoryArgument));
@@ -81,7 +81,7 @@ public V GetValue<TArg>(K key, Func<K, TArg, V> valueFactory, TArg factoryArgume
8181
/// <summary>
8282
/// Gets the value if it has been initialized, else default.
8383
/// </summary>
84-
public V ValueIfCreated
84+
public V? ValueIfCreated
8585
{
8686
get
8787
{
@@ -125,17 +125,17 @@ private V CreateValue<TFactory>(K key, TFactory valueFactory) where TFactory : s
125125
}
126126
}
127127

128-
return value;
128+
return value!;
129129
}
130130

131131
///<inheritdoc/>
132-
public override bool Equals(object obj)
132+
public override bool Equals(object? obj)
133133
{
134134
return Equals(obj as AtomicFactory<K, V>);
135135
}
136136

137137
///<inheritdoc/>
138-
public bool Equals(AtomicFactory<K, V> other)
138+
public bool Equals(AtomicFactory<K, V>? other)
139139
{
140140
if (other is null || !IsValueCreated || !other.IsValueCreated)
141141
{
@@ -153,23 +153,23 @@ public override int GetHashCode()
153153
return 0;
154154
}
155155

156-
return ValueIfCreated.GetHashCode();
156+
return ValueIfCreated!.GetHashCode();
157157
}
158158

159159
#pragma warning disable CA2002 // Do not lock on objects with weak identity
160160
private class Initializer
161161
{
162162
private bool isInitialized;
163-
private V value;
164-
private ExceptionDispatchInfo exceptionDispatch;
163+
private V? value;
164+
private ExceptionDispatchInfo? exceptionDispatch;
165165

166166
public V CreateValue<TFactory>(K key, TFactory valueFactory) where TFactory : struct, IValueFactory<K, V>
167167
{
168168
lock (this)
169169
{
170170
if (isInitialized)
171171
{
172-
return value;
172+
return value!;
173173
}
174174

175175
// If a previous thread called the factory and failed, throw the same error instead

BitFaster.Caching/Atomic/AtomicFactoryAsyncCache.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public AtomicFactoryAsyncCache(ICache<K, AsyncAtomicFactory<K, V>> cache)
3333

3434
if (cache.Events.HasValue)
3535
{
36-
this.events = new Optional<ICacheEvents<K, V>>(new EventProxy(cache.Events.Value));
36+
this.events = new Optional<ICacheEvents<K, V>>(new EventProxy(cache.Events.Value!));
3737
}
3838
else
3939
{
@@ -91,14 +91,14 @@ public ValueTask<V> GetOrAddAsync<TArg>(K key, Func<K, TArg, Task<V>> valueFacto
9191
}
9292

9393
///<inheritdoc/>
94-
public bool TryGet(K key, out V value)
94+
public bool TryGet(K key, [MaybeNullWhen(false)] out V value)
9595
{
96-
AsyncAtomicFactory<K, V> output;
96+
AsyncAtomicFactory<K, V>? output;
9797
var ret = cache.TryGet(key, out output);
9898

99-
if (ret && output.IsValueCreated)
99+
if (ret && output!.IsValueCreated)
100100
{
101-
value = output.ValueIfCreated;
101+
value = output.ValueIfCreated!;
102102
return true;
103103
}
104104

@@ -128,11 +128,11 @@ public bool TryRemove(KeyValuePair<K, V> item)
128128
/// <remarks>
129129
/// If the value factory is still executing, the default value will be returned.
130130
/// </remarks>
131-
public bool TryRemove(K key, out V value)
131+
public bool TryRemove(K key, [MaybeNullWhen(false)] out V value)
132132
{
133133
if (cache.TryRemove(key, out var atomic))
134134
{
135-
value = atomic.ValueIfCreated;
135+
value = atomic.ValueIfCreated!;
136136
return true;
137137
}
138138

@@ -154,7 +154,7 @@ public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
154154
{
155155
if (kvp.Value.IsValueCreated)
156156
{
157-
yield return new KeyValuePair<K, V>(kvp.Key, kvp.Value.ValueIfCreated);
157+
yield return new KeyValuePair<K, V>(kvp.Key, kvp.Value.ValueIfCreated!);
158158
}
159159
}
160160
}
@@ -173,12 +173,12 @@ public EventProxy(ICacheEvents<K, AsyncAtomicFactory<K, V>> inner)
173173

174174
protected override ItemRemovedEventArgs<K, V> TranslateOnRemoved(ItemRemovedEventArgs<K, AsyncAtomicFactory<K, V>> inner)
175175
{
176-
return new ItemRemovedEventArgs<K, V>(inner.Key, inner.Value.ValueIfCreated, inner.Reason);
176+
return new ItemRemovedEventArgs<K, V>(inner.Key, inner.Value!.ValueIfCreated, inner.Reason);
177177
}
178178

179179
protected override ItemUpdatedEventArgs<K, V> TranslateOnUpdated(ItemUpdatedEventArgs<K, AsyncAtomicFactory<K, V>> inner)
180180
{
181-
return new ItemUpdatedEventArgs<K, V>(inner.Key, inner.OldValue.ValueIfCreated, inner.NewValue.ValueIfCreated);
181+
return new ItemUpdatedEventArgs<K, V>(inner.Key, inner.OldValue!.ValueIfCreated, inner.NewValue!.ValueIfCreated);
182182
}
183183
}
184184

@@ -207,7 +207,7 @@ public KeyValuePair<K, V>[] Items
207207
}
208208
}
209209

210-
public ICacheMetrics Metrics => cache.Metrics.Value;
210+
public ICacheMetrics? Metrics => cache.Metrics.Value;
211211
}
212212
}
213213
}

BitFaster.Caching/Atomic/AtomicFactoryCache.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Diagnostics;
5+
using System.Diagnostics.CodeAnalysis;
56

67
namespace BitFaster.Caching.Atomic
78
{
@@ -31,7 +32,7 @@ public AtomicFactoryCache(ICache<K, AtomicFactory<K, V>> cache)
3132

3233
if (cache.Events.HasValue)
3334
{
34-
this.events = new Optional<ICacheEvents<K, V>>(new EventProxy(cache.Events.Value));
35+
this.events = new Optional<ICacheEvents<K, V>>(new EventProxy(cache.Events.Value!));
3536
}
3637
else
3738
{
@@ -90,14 +91,14 @@ public V GetOrAdd<TArg>(K key, Func<K, TArg, V> valueFactory, TArg factoryArgume
9091
}
9192

9293
///<inheritdoc/>
93-
public bool TryGet(K key, out V value)
94+
public bool TryGet(K key, [MaybeNullWhen(false)] out V value)
9495
{
95-
AtomicFactory<K, V> output;
96+
AtomicFactory<K, V>? output;
9697
var ret = cache.TryGet(key, out output);
9798

98-
if (ret && output.IsValueCreated)
99+
if (ret && output!.IsValueCreated)
99100
{
100-
value = output.ValueIfCreated;
101+
value = output.ValueIfCreated!;
101102
return true;
102103
}
103104

@@ -121,11 +122,11 @@ public bool TryRemove(KeyValuePair<K, V> item)
121122
/// <remarks>
122123
/// If the value factory is still executing, the default value will be returned.
123124
/// </remarks>
124-
public bool TryRemove(K key, out V value)
125+
public bool TryRemove(K key, [MaybeNullWhen(false)] out V value)
125126
{
126127
if (cache.TryRemove(key, out var atomic))
127128
{
128-
value = atomic.ValueIfCreated;
129+
value = atomic.ValueIfCreated!;
129130
return true;
130131
}
131132

@@ -153,7 +154,7 @@ public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
153154
{
154155
if (kvp.Value.IsValueCreated)
155156
{
156-
yield return new KeyValuePair<K, V>(kvp.Key, kvp.Value.ValueIfCreated);
157+
yield return new KeyValuePair<K, V>(kvp.Key, kvp.Value.ValueIfCreated!);
157158
}
158159
}
159160
}
@@ -172,12 +173,12 @@ public EventProxy(ICacheEvents<K, AtomicFactory<K, V>> inner)
172173

173174
protected override ItemRemovedEventArgs<K, V> TranslateOnRemoved(ItemRemovedEventArgs<K, AtomicFactory<K, V>> inner)
174175
{
175-
return new ItemRemovedEventArgs<K, V>(inner.Key, inner.Value.ValueIfCreated, inner.Reason);
176+
return new ItemRemovedEventArgs<K, V>(inner.Key, inner.Value!.ValueIfCreated, inner.Reason);
176177
}
177178

178179
protected override ItemUpdatedEventArgs<K, V> TranslateOnUpdated(ItemUpdatedEventArgs<K, AtomicFactory<K, V>> inner)
179180
{
180-
return new ItemUpdatedEventArgs<K, V>(inner.Key, inner.OldValue.ValueIfCreated, inner.NewValue.ValueIfCreated);
181+
return new ItemUpdatedEventArgs<K, V>(inner.Key, inner.OldValue!.ValueIfCreated, inner.NewValue!.ValueIfCreated);
181182
}
182183
}
183184
}

0 commit comments

Comments
 (0)