Skip to content

Commit

Permalink
Upgrade DictionaryExtensions
Browse files Browse the repository at this point in the history
Signed-off-by: Serhii A. Hrytsenko <[email protected]>
  • Loading branch information
gritcsenko committed Jan 21, 2025
1 parent 31c6635 commit 0e60504
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
namespace HomeInventory.Core;
using System.Runtime.InteropServices;

namespace HomeInventory.Core;

public static class DictionaryExtensions
{
public static TResult GetOrAdd<TKey, TValue, TResult>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TResult> createValueFunc)
where TKey : notnull
where TResult : TValue =>
dictionary.TryGetValue(key, out var value)
? (TResult)value!
: dictionary.Add(key, createValueFunc);

public static async ValueTask<TResult> GetOrAddAsync<TKey, TValue, TResult>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, Task<TResult>> createValueFunc)
where TKey : notnull
where TResult : TValue =>
dictionary.TryGetValue(key, out var value)
? (TResult)value!
: await dictionary.AddAsync(key, createValueFunc);

private static TResult Add<TKey, TValue, TResult>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TResult> createValueFunc)
public static TResult GetOrAdd<TKey, TValue, TResult>(this Dictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TResult> createValueFunc)
where TKey : notnull
where TResult : TValue
{
var newValue = createValueFunc(key);
dictionary.Add(key, newValue);
return newValue;
ref var val = ref CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out var exists);
if (!exists)
{
val = createValueFunc(key);
}

return (TResult)val!;
}

private static async Task<TResult> AddAsync<TKey, TValue, TResult>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, Task<TResult>> createValueFunc)
public static async ValueTask<TResult> GetOrAddAsync<TKey, TValue, TResult>(this Dictionary<TKey, TValue> dictionary, TKey key, Func<TKey, Task<TResult>> createValueFunc)
where TKey : notnull
where TResult : TValue
{
var newValue = await createValueFunc(key);
dictionary.Add(key, newValue);
return newValue;
ref var val = ref CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out var exists);
if (!exists)
{
val = await createValueFunc(key);

Check failure on line 27 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs

View workflow job for this annotation

GitHub Actions / Build .NET

A 'ref' local cannot be preserved across 'await' or 'yield' boundary.

Check failure on line 27 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs

View workflow job for this annotation

GitHub Actions / Build .NET

A 'ref' local cannot be preserved across 'await' or 'yield' boundary.

Check failure on line 27 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs

View workflow job for this annotation

GitHub Actions / Build .NET

A 'ref' local cannot be preserved across 'await' or 'yield' boundary.

Check failure on line 27 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs

View workflow job for this annotation

GitHub Actions / Build .NET

A 'ref' local cannot be preserved across 'await' or 'yield' boundary.

Check failure on line 27 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

A 'ref' local cannot be preserved across 'await' or 'yield' boundary.

Check failure on line 27 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

A 'ref' local cannot be preserved across 'await' or 'yield' boundary.

Check failure on line 27 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

A 'ref' local cannot be preserved across 'await' or 'yield' boundary.

Check failure on line 27 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

A 'ref' local cannot be preserved across 'await' or 'yield' boundary.
}

return (TResult)val!;

Check failure on line 30 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs

View workflow job for this annotation

GitHub Actions / Build .NET

A 'ref' local cannot be preserved across 'await' or 'yield' boundary.

Check failure on line 30 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs

View workflow job for this annotation

GitHub Actions / Build .NET

A 'ref' local cannot be preserved across 'await' or 'yield' boundary.

Check failure on line 30 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs

View workflow job for this annotation

GitHub Actions / Build .NET

A 'ref' local cannot be preserved across 'await' or 'yield' boundary.

Check failure on line 30 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs

View workflow job for this annotation

GitHub Actions / Build .NET

A 'ref' local cannot be preserved across 'await' or 'yield' boundary.

Check failure on line 30 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

A 'ref' local cannot be preserved across 'await' or 'yield' boundary.

Check failure on line 30 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

A 'ref' local cannot be preserved across 'await' or 'yield' boundary.

Check failure on line 30 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

A 'ref' local cannot be preserved across 'await' or 'yield' boundary.

Check failure on line 30 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

A 'ref' local cannot be preserved across 'await' or 'yield' boundary.
}
}

0 comments on commit 0e60504

Please sign in to comment.