fix: fix bug introduced by d29663f081

Since value is a ref returned value, mutating it before returning was actually intended behaviour, since the reassignment causes the dictionary value to be mutated too.

Also, what game was this ref watching?!
This commit is contained in:
Oliver Booth 2023-04-01 22:31:54 +01:00
parent 9df0fde96d
commit 34c49a2228
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
1 changed files with 3 additions and 12 deletions

View File

@ -54,16 +54,7 @@ public static class DictionaryExtensions
#if NET6_0_OR_GREATER #if NET6_0_OR_GREATER
ref var value = ref CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out bool exists); ref var value = ref CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out bool exists);
if (exists) return value = exists ? updateValueFactory(key, value!) : addValue;
{
value = updateValueFactory(key, value!);
}
else
{
value = addValue;
}
return value;
#else #else
if (dictionary.TryGetValue(key, out TValue? old)) if (dictionary.TryGetValue(key, out TValue? old))
{ {
@ -182,7 +173,7 @@ public static class DictionaryExtensions
#if NET6_0_OR_GREATER #if NET6_0_OR_GREATER
ref TValue? value = ref CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out bool exists); ref TValue? value = ref CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out bool exists);
return exists ? updateValueFactory(key, value!) : addValueFactory(key); return value = exists ? updateValueFactory(key, value!) : addValueFactory(key);
#else #else
if (dictionary.TryGetValue(key, out TValue? old)) if (dictionary.TryGetValue(key, out TValue? old))
{ {
@ -319,7 +310,7 @@ public static class DictionaryExtensions
#if NET6_0_OR_GREATER #if NET6_0_OR_GREATER
ref TValue? value = ref CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out bool exists); ref TValue? value = ref CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out bool exists);
return exists ? updateValueFactory(key, value!, factoryArgument) : addValueFactory(key, factoryArgument); return value = exists ? updateValueFactory(key, value!, factoryArgument) : addValueFactory(key, factoryArgument);
#else #else
if (dictionary.TryGetValue(key, out TValue? old)) if (dictionary.TryGetValue(key, out TValue? old))
{ {