Skip to content

Add ToImmutable*Async extension methods to System.Linq.Async #1540

@bash

Description

@bash

I would like to propose the addition of new extension methods that allow collecting an IAsyncEnumerable into the immutable collections from System.Collections.Immutable.

The overloads below are copied from System.Collections.Immutable with a few changes to make them async friendly.

  • Add Async suffix
  • Change return type to ValueTask
  • Add CancellationToken parameter
public static ValueTask<ImmutableArray<TSource>> ToImmutableArrayAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default);

public static ValueTask<ImmutableDictionary<TKey, TValue>> ToImmutableDictionaryAsync<TSource, TKey, TValue>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TValue> elementSelector, IEqualityComparer<TKey>? keyComparer, IEqualityComparer<TValue>? valueComparer, CancellationToken cancellationToken = default) where TKey : notnull;

public static ValueTask<ImmutableDictionary<TKey, TValue>> ToImmutableDictionaryAsync<TSource, TKey, TValue>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TValue> elementSelector, IEqualityComparer<TKey>? keyComparer, CancellationToken cancellationToken = default) where TKey : notnull;

public static ValueTask<ImmutableDictionary<TKey, TSource>> ToImmutableDictionaryAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken = default) where TKey : notnull;

public static ValueTask<ImmutableDictionary<TKey, TSource>> ToImmutableDictionaryAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? keyComparer, CancellationToken cancellationToken = default) where TKey : notnull;

public static ValueTask<ImmutableDictionary<TKey, TValue>> ToImmutableDictionaryAsync<TSource, TKey, TValue>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TValue> elementSelector, CancellationToken cancellationToken = default) where TKey : notnull;

public static ValueTask<ImmutableDictionary<TKey, TValue>> ToImmutableDictionaryAsync<TKey, TValue>(this IAsyncEnumerable<KeyValuePair<TKey, TValue>> source, IEqualityComparer<TKey>? keyComparer, IEqualityComparer<TValue>? valueComparer, CancellationToken cancellationToken = default) where TKey : notnull;

public static ValueTask<ImmutableDictionary<TKey, TValue>> ToImmutableDictionaryAsync<TKey, TValue>(this IAsyncEnumerable<KeyValuePair<TKey, TValue>> source, IEqualityComparer<TKey>? keyComparer, CancellationToken cancellationToken = default) where TKey : notnull;

public static ValueTask<ImmutableDictionary<TKey, TValue>> ToImmutableDictionaryAsync<TKey, TValue>(this IAsyncEnumerable<KeyValuePair<TKey, TValue>> source, CancellationToken cancellationToken = default) where TKey : notnull;

public static ValueTask<ImmutableHashSet<TSource>> ToImmutableHashSetAsync<TSource>(this IAsyncEnumerable<TSource> source, IEqualityComparer<TSource>? equalityComparer, CancellationToken cancellationToken = default);

public static ValueTask<ImmutableHashSet<TSource>> ToImmutableHashSetAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default);

public static ValueTask<ImmutableList<TSource>> ToImmutableListAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default);

public static ValueTask<ImmutableSortedDictionary<TKey, TValue>> ToImmutableSortedDictionaryAsync<TSource, TKey, TValue>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TValue> elementSelector, IComparer<TKey>? keyComparer, IEqualityComparer<TValue>? valueComparer, CancellationToken cancellationToken = default) where TKey : notnull;

public static ValueTask<ImmutableSortedDictionary<TKey, TValue>> ToImmutableSortedDictionaryAsync<TSource, TKey, TValue>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TValue> elementSelector, IComparer<TKey>? keyComparer, CancellationToken cancellationToken = default) where TKey : notnull;

public static ValueTask<ImmutableSortedDictionary<TKey, TValue>> ToImmutableSortedDictionaryAsync<TSource, TKey, TValue>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TValue> elementSelector, CancellationToken cancellationToken = default) where TKey : notnull;

public static ValueTask<ImmutableSortedDictionary<TKey, TValue>> ToImmutableSortedDictionaryAsync<TKey, TValue>(this IAsyncEnumerable<KeyValuePair<TKey, TValue>> source, IComparer<TKey>? keyComparer, IEqualityComparer<TValue>? valueComparer, CancellationToken cancellationToken = default) where TKey : notnull;

public static ValueTask<ImmutableSortedDictionary<TKey, TValue>> ToImmutableSortedDictionaryAsync<TKey, TValue>(this IAsyncEnumerable<KeyValuePair<TKey, TValue>> source, IComparer<TKey>? keyComparer, CancellationToken cancellationToken = default) where TKey : notnull;

public static ValueTask<ImmutableSortedDictionary<TKey, TValue>> ToImmutableSortedDictionaryAsync<TKey, TValue>(this IAsyncEnumerable<KeyValuePair<TKey, TValue>> source, CancellationToken cancellationToken = default) where TKey : notnull;

public static ValueTask<ImmutableSortedSet<TSource>> ToImmutableSortedSetAsync<TSource>(this IAsyncEnumerable<TSource> source, IComparer<TSource>? comparer, CancellationToken cancellationToken = default);

public static ValueTask<ImmutableSortedSet<TSource>> ToImmutableSortedSetAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default);

Pros/Cons

  • Pro: Feature parity with the extensions available in the .NET SDK.
  • Con: Requires a new NuGet dependency for older target frameworks because they don't include System.Collections.Immutable.
  • others?

I'd happily create a PR for this feature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions