5
5
using System ;
6
6
using System . Buffers ;
7
7
using System . Collections . Generic ;
8
+ using System . Diagnostics . CodeAnalysis ;
8
9
using System . Runtime . CompilerServices ;
9
10
using System . Threading ;
10
11
using Microsoft . Collections . Extensions ;
@@ -102,7 +103,7 @@ public bool IsRegistered<TMessage, TToken>(object recipient, TToken token)
102
103
103
104
Recipient key = new ( recipient ) ;
104
105
105
- return mapping ! . ContainsKey ( key ) ;
106
+ return mapping . ContainsKey ( key ) ;
106
107
}
107
108
}
108
109
@@ -155,7 +156,7 @@ public void UnregisterAll(object recipient)
155
156
}
156
157
157
158
// Removes all the lists of registered handlers for the recipient
158
- foreach ( IMapping mapping in set ! )
159
+ foreach ( IMapping mapping in set )
159
160
{
160
161
if ( mapping . TryRemove ( key ) &&
161
162
mapping . Count == 0 )
@@ -168,12 +169,12 @@ public void UnregisterAll(object recipient)
168
169
// dictionary (a hashed collection) only costs O(1) in the best case, while
169
170
// if we had tried to iterate the whole dictionary every time we would have
170
171
// paid an O(n) minimum cost for each single remove operation.
171
- this . typesMap . TryRemove ( mapping . TypeArguments , out _ ) ;
172
+ this . typesMap . TryRemove ( mapping . TypeArguments ) ;
172
173
}
173
174
}
174
175
175
176
// Remove the associated set in the recipients map
176
- this . recipientsMap . TryRemove ( key , out _ ) ;
177
+ this . recipientsMap . TryRemove ( key ) ;
177
178
}
178
179
}
179
180
@@ -212,7 +213,7 @@ public void UnregisterAll<TToken>(object recipient, TToken token)
212
213
// the opportunities to reuse existing buffers for both. When we need to reference an item
213
214
// stored in the buffer with the type we know it will have, we use Unsafe.As<T> to avoid the
214
215
// expensive type check in the cast, since we already know the assignment will be valid.
215
- maps = ArrayPool < object > . Shared . Rent ( set ! . Count ) ;
216
+ maps = ArrayPool < object > . Shared . Rent ( set . Count ) ;
216
217
217
218
foreach ( IMapping item in set )
218
219
{
@@ -256,7 +257,7 @@ public void UnregisterAll<TToken>(object recipient, TToken token)
256
257
set . Remove ( Unsafe . As < IMapping > ( map ) ) &&
257
258
set . Count == 0 )
258
259
{
259
- this . recipientsMap . TryRemove ( key , out _ ) ;
260
+ this . recipientsMap . TryRemove ( key ) ;
260
261
}
261
262
}
262
263
}
@@ -297,28 +298,28 @@ public void Unregister<TMessage, TToken>(object recipient, TToken token)
297
298
298
299
Recipient key = new ( recipient ) ;
299
300
300
- if ( ! mapping ! . TryGetValue ( key , out DictionarySlim < TToken , object > ? dictionary ) )
301
+ if ( ! mapping . TryGetValue ( key , out DictionarySlim < TToken , object > ? dictionary ) )
301
302
{
302
303
return ;
303
304
}
304
305
305
306
// Remove the target handler
306
- if ( dictionary ! . TryRemove ( token , out _ ) &&
307
+ if ( dictionary . TryRemove ( token ) &&
307
308
dictionary . Count == 0 )
308
309
{
309
310
// If the map is empty, it means that the current recipient has no remaining
310
311
// registered handlers for the current <TMessage, TToken> combination, regardless,
311
312
// of the specific token value (ie. the channel used to receive messages of that type).
312
313
// We can remove the map entirely from this container, and remove the link to the map itself
313
314
// to the current mapping between existing registered recipients (or entire recipients too).
314
- mapping . TryRemove ( key , out _ ) ;
315
+ mapping . TryRemove ( key ) ;
315
316
316
317
HashSet < IMapping > set = this . recipientsMap [ key ] ;
317
318
318
319
if ( set . Remove ( mapping ) &&
319
320
set . Count == 0 )
320
321
{
321
- this . recipientsMap . TryRemove ( key , out _ ) ;
322
+ this . recipientsMap . TryRemove ( key ) ;
322
323
}
323
324
}
324
325
}
@@ -379,7 +380,7 @@ public TMessage Send<TMessage, TToken>(TMessage message, TToken token)
379
380
// which will always be greater or equal than the ones matching the previous test.
380
381
// We're still using a checked span accesses here though to make sure an out of
381
382
// bounds write can never happen even if an error was present in the logic above.
382
- pairs [ 2 * i ] = handler ! ;
383
+ pairs [ 2 * i ] = handler ;
383
384
pairs [ ( 2 * i ) + 1 ] = mappingEnumerator . Key . Target ;
384
385
i ++ ;
385
386
}
@@ -439,7 +440,7 @@ public void Reset()
439
440
/// <param name="mapping">The resulting <see cref="Mapping{TMessage,TToken}"/> instance, if found.</param>
440
441
/// <returns>Whether or not the required <see cref="Mapping{TMessage,TToken}"/> instance was found.</returns>
441
442
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
442
- private bool TryGetMapping < TMessage , TToken > ( out Mapping < TMessage , TToken > ? mapping )
443
+ private bool TryGetMapping < TMessage , TToken > ( [ NotNullWhen ( true ) ] out Mapping < TMessage , TToken > ? mapping )
443
444
where TMessage : class
444
445
where TToken : IEquatable < TToken >
445
446
{
0 commit comments