mirror of
https://github.com/chylex/.NET-Community-Toolkit.git
synced 2025-04-10 11:15:45 +02:00
Improved nullability annotations
This commit is contained in:
parent
acafc5d705
commit
d10e6aea8a
Microsoft.Toolkit.Mvvm/Messaging
Messenger.cs
Microsoft.Collections.Extensions
@ -122,12 +122,12 @@ public void Register<TMessage, TToken>(object recipient, TToken token, Action<TM
|
||||
// Get the <TMessage, TToken> registration list for this recipient
|
||||
Mapping<TMessage, TToken> values = GetOrAddMapping<TMessage, TToken>();
|
||||
var key = new Recipient(recipient);
|
||||
ref DictionarySlim<TToken, Action<TMessage>> map = ref values.GetOrAddValueRef(key);
|
||||
ref DictionarySlim<TToken, Action<TMessage>>? map = ref values.GetOrAddValueRef(key);
|
||||
|
||||
map ??= new DictionarySlim<TToken, Action<TMessage>>();
|
||||
|
||||
// Add the new registration entry
|
||||
ref Action<TMessage> handler = ref map.GetOrAddValueRef(token);
|
||||
ref Action<TMessage>? handler = ref map.GetOrAddValueRef(token);
|
||||
|
||||
if (!(handler is null))
|
||||
{
|
||||
@ -137,7 +137,7 @@ public void Register<TMessage, TToken>(object recipient, TToken token, Action<TM
|
||||
handler = action;
|
||||
|
||||
// Make sure this registration map is tracked for the current recipient
|
||||
ref HashSet<IMapping> set = ref this.recipientsMap.GetOrAddValueRef(key);
|
||||
ref HashSet<IMapping>? set = ref this.recipientsMap.GetOrAddValueRef(key);
|
||||
|
||||
set ??= new HashSet<IMapping>();
|
||||
|
||||
@ -153,13 +153,13 @@ public void Unregister(object recipient)
|
||||
// If the recipient has no registered messages at all, ignore
|
||||
var key = new Recipient(recipient);
|
||||
|
||||
if (!this.recipientsMap.TryGetValue(key, out HashSet<IMapping> set))
|
||||
if (!this.recipientsMap.TryGetValue(key, out HashSet<IMapping>? set))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Removes all the lists of registered handlers for the recipient
|
||||
foreach (IMapping map in set)
|
||||
foreach (IMapping map in set!)
|
||||
{
|
||||
map.Remove(key);
|
||||
|
||||
@ -191,7 +191,7 @@ public void Unregister<TToken>(object recipient, TToken token)
|
||||
// Get the shared set of mappings for the recipient, if present
|
||||
var key = new Recipient(recipient);
|
||||
|
||||
if (!this.recipientsMap.TryGetValue(key, out HashSet<IMapping> set))
|
||||
if (!this.recipientsMap.TryGetValue(key, out HashSet<IMapping>? set))
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -200,7 +200,7 @@ public void Unregister<TToken>(object recipient, TToken token)
|
||||
// array, as we can't modify the contents of the set while iterating it.
|
||||
// The rented buffer is oversized and will also include mappings for
|
||||
// handlers of messages that are registered through a different token.
|
||||
var maps = ArrayPool<IDictionarySlim<Recipient, IDictionarySlim<TToken>>>.Shared.Rent(set.Count);
|
||||
var maps = ArrayPool<IDictionarySlim<Recipient, IDictionarySlim<TToken>>>.Shared.Rent(set!.Count);
|
||||
int i = 0;
|
||||
|
||||
try
|
||||
@ -278,13 +278,13 @@ public void Unregister<TMessage, TToken>(object recipient, TToken token)
|
||||
|
||||
var key = new Recipient(recipient);
|
||||
|
||||
if (!mapping!.TryGetValue(key, out DictionarySlim<TToken, Action<TMessage>> map))
|
||||
if (!mapping!.TryGetValue(key, out DictionarySlim<TToken, Action<TMessage>>? map))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove the target handler
|
||||
map.Remove(token);
|
||||
map!.Remove(token);
|
||||
|
||||
// If the map is empty, it means that the current recipient has no remaining
|
||||
// registered handlers for the current <TMessage, TToken> combination, regardless,
|
||||
@ -412,7 +412,7 @@ private bool TryGetMapping<TMessage, TToken>(out Mapping<TMessage, TToken>? mapp
|
||||
{
|
||||
var key = new Type2(typeof(TMessage), typeof(TToken));
|
||||
|
||||
if (this.typesMap.TryGetValue(key, out IMapping target))
|
||||
if (this.typesMap.TryGetValue(key, out IMapping? target))
|
||||
{
|
||||
// This method and the ones above are the only ones handling values in the types map,
|
||||
// and here we are sure that the object reference we have points to an instance of the
|
||||
@ -440,7 +440,7 @@ private Mapping<TMessage, TToken> GetOrAddMapping<TMessage, TToken>()
|
||||
where TToken : IEquatable<TToken>
|
||||
{
|
||||
var key = new Type2(typeof(TMessage), typeof(TToken));
|
||||
ref IMapping target = ref this.typesMap.GetOrAddValueRef(key);
|
||||
ref IMapping? target = ref this.typesMap.GetOrAddValueRef(key);
|
||||
|
||||
target ??= new Mapping<TMessage, TToken>();
|
||||
|
||||
|
@ -34,6 +34,7 @@ namespace Microsoft.Collections.Extensions
|
||||
[DebuggerDisplay("Count = {Count}")]
|
||||
internal class DictionarySlim<TKey, TValue> : IDictionarySlim<TKey, TValue>
|
||||
where TKey : IEquatable<TKey>
|
||||
where TValue : class
|
||||
{
|
||||
// See info in CoreFX labs for how this works
|
||||
private static readonly Entry[] InitialEntries = new Entry[1];
|
||||
@ -45,7 +46,7 @@ internal class DictionarySlim<TKey, TValue> : IDictionarySlim<TKey, TValue>
|
||||
private struct Entry
|
||||
{
|
||||
public TKey Key;
|
||||
public TValue Value;
|
||||
public TValue? Value;
|
||||
public int Next;
|
||||
}
|
||||
|
||||
@ -74,7 +75,7 @@ public TValue this[TKey key]
|
||||
{
|
||||
if (key.Equals(entries[i].Key))
|
||||
{
|
||||
return entries[i].Value;
|
||||
return entries[i].Value!;
|
||||
}
|
||||
}
|
||||
|
||||
@ -117,7 +118,7 @@ public bool ContainsKey(TKey key)
|
||||
/// <param name="key">The key to look for.</param>
|
||||
/// <param name="value">The value found, otherwise <see langword="default"/>.</param>
|
||||
/// <returns>Whether or not the key was present.</returns>
|
||||
public bool TryGetValue(TKey key, out TValue value)
|
||||
public bool TryGetValue(TKey key, out TValue? value)
|
||||
{
|
||||
Entry[] entries = this.entries;
|
||||
|
||||
@ -127,7 +128,7 @@ public bool TryGetValue(TKey key, out TValue value)
|
||||
{
|
||||
if (key.Equals(entries[i].Key))
|
||||
{
|
||||
value = entries[i].Value;
|
||||
value = entries[i].Value!;
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -183,7 +184,7 @@ public bool Remove(TKey key)
|
||||
/// </summary>
|
||||
/// <param name="key">Key to look for</param>
|
||||
/// <returns>Reference to the new or existing value</returns>
|
||||
public ref TValue GetOrAddValueRef(TKey key)
|
||||
public ref TValue? GetOrAddValueRef(TKey key)
|
||||
{
|
||||
Entry[] entries = this.entries;
|
||||
int bucketIndex = key.GetHashCode() & (this.buckets.Length - 1);
|
||||
@ -201,8 +202,14 @@ public ref TValue GetOrAddValueRef(TKey key)
|
||||
return ref AddKey(key, bucketIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a slot for a new value to add for a specified key.
|
||||
/// </summary>
|
||||
/// <param name="key">The key to use to add the new value.</param>
|
||||
/// <param name="bucketIndex">The target bucked index to use.</param>
|
||||
/// <returns>A reference to the slot for the new value to add.</returns>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private ref TValue AddKey(TKey key, int bucketIndex)
|
||||
private ref TValue? AddKey(TKey key, int bucketIndex)
|
||||
{
|
||||
Entry[] entries = this.entries;
|
||||
int entryIndex;
|
||||
@ -307,7 +314,7 @@ public bool MoveNext()
|
||||
|
||||
this.current = new KeyValuePair<TKey, TValue>(
|
||||
entries[this.index].Key,
|
||||
entries[this.index++].Value);
|
||||
entries[this.index++].Value!);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ namespace Microsoft.Collections.Extensions
|
||||
/// <typeparam name="TValue">The covariant type of values in the dictionary.</typeparam>
|
||||
internal interface IDictionarySlim<in TKey, out TValue> : IDictionarySlim<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
where TValue : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the value with the specified key.
|
||||
|
Loading…
Reference in New Issue
Block a user