123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT License.
- // See the LICENSE file in the project root for more information.
- using System.Collections;
- using System.Collections.Generic;
- using System.Threading;
- using System.Threading.Tasks;
- namespace System.Linq
- {
- #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
- public static partial class AsyncEnumerable
- {
- /// <summary>
- /// Converts an enumerable sequence to an async-enumerable sequence.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
- /// <param name="source">Enumerable sequence to convert to an async-enumerable sequence.</param>
- /// <returns>The async-enumerable sequence whose elements are pulled from the given enumerable sequence.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
- public static IAsyncEnumerable<TSource> ToAsyncEnumerable<TSource>(this IEnumerable<TSource> source)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- return source switch
- {
- IList<TSource> list => new AsyncIListEnumerableAdapter<TSource>(list),
- ICollection<TSource> collection => new AsyncICollectionEnumerableAdapter<TSource>(collection),
- _ => new AsyncEnumerableAdapter<TSource>(source),
- };
- }
- private sealed class AsyncEnumerableAdapter<T> : AsyncIterator<T>, IAsyncIListProvider<T>
- {
- private readonly IEnumerable<T> _source;
- private IEnumerator<T>? _enumerator;
-
- public AsyncEnumerableAdapter(IEnumerable<T> source)
- {
- _source = source;
- }
- public override AsyncIteratorBase<T> Clone() => new AsyncEnumerableAdapter<T>(_source);
- public override async ValueTask DisposeAsync()
- {
- if (_enumerator != null)
- {
- _enumerator.Dispose();
- _enumerator = null;
- }
- await base.DisposeAsync().ConfigureAwait(false);
- }
- protected override async ValueTask<bool> MoveNextCore()
- {
- switch (_state)
- {
- case AsyncIteratorState.Allocated:
- _enumerator = _source.GetEnumerator();
- _state = AsyncIteratorState.Iterating;
- goto case AsyncIteratorState.Iterating;
- case AsyncIteratorState.Iterating:
- if (_enumerator!.MoveNext())
- {
- _current = _enumerator.Current;
- return true;
- }
- await DisposeAsync().ConfigureAwait(false);
- break;
- }
- return false;
- }
- //
- // NB: These optimizations rely on the System.Linq implementation of IEnumerable<T> operators to optimize
- // and short-circuit as appropriate.
- //
- public ValueTask<T[]> ToArrayAsync(CancellationToken cancellationToken)
- {
- cancellationToken.ThrowIfCancellationRequested();
- return new ValueTask<T[]>(_source.ToArray());
- }
- public ValueTask<List<T>> ToListAsync(CancellationToken cancellationToken)
- {
- cancellationToken.ThrowIfCancellationRequested();
- return new ValueTask<List<T>>(_source.ToList());
- }
- public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
- {
- cancellationToken.ThrowIfCancellationRequested();
- return new ValueTask<int>(_source.Count());
- }
- }
- private sealed class AsyncIListEnumerableAdapter<T> : AsyncIterator<T>, IAsyncIListProvider<T>, IList<T>
- {
- private readonly IList<T> _source;
- private IEnumerator<T>? _enumerator;
- public AsyncIListEnumerableAdapter(IList<T> source)
- {
- _source = source;
- }
- public override AsyncIteratorBase<T> Clone() => new AsyncIListEnumerableAdapter<T>(_source);
- public override async ValueTask DisposeAsync()
- {
- if (_enumerator != null)
- {
- _enumerator.Dispose();
- _enumerator = null;
- }
- await base.DisposeAsync().ConfigureAwait(false);
- }
- protected override async ValueTask<bool> MoveNextCore()
- {
- switch (_state)
- {
- case AsyncIteratorState.Allocated:
- _enumerator = _source.GetEnumerator();
- _state = AsyncIteratorState.Iterating;
- goto case AsyncIteratorState.Iterating;
- case AsyncIteratorState.Iterating:
- if (_enumerator!.MoveNext())
- {
- _current = _enumerator.Current;
- return true;
- }
- await DisposeAsync().ConfigureAwait(false);
- break;
- }
- return false;
- }
- public override IAsyncEnumerable<TResult> Select<TResult>(Func<T, TResult> selector) => new SelectIListIterator<T, TResult>(_source, selector);
- //
- // NB: These optimizations rely on the System.Linq implementation of IEnumerable<T> operators to optimize
- // and short-circuit as appropriate.
- //
- public ValueTask<T[]> ToArrayAsync(CancellationToken cancellationToken)
- {
- cancellationToken.ThrowIfCancellationRequested();
- return new ValueTask<T[]>(_source.ToArray());
- }
- public ValueTask<List<T>> ToListAsync(CancellationToken cancellationToken)
- {
- cancellationToken.ThrowIfCancellationRequested();
- return new ValueTask<List<T>>(_source.ToList());
- }
- public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
- {
- cancellationToken.ThrowIfCancellationRequested();
- return new ValueTask<int>(_source.Count);
- }
- IEnumerator<T> IEnumerable<T>.GetEnumerator() => _source.GetEnumerator();
- IEnumerator IEnumerable.GetEnumerator() => _source.GetEnumerator();
- void ICollection<T>.Add(T item) => _source.Add(item);
- void ICollection<T>.Clear() => _source.Clear();
- bool ICollection<T>.Contains(T item) => _source.Contains(item);
- void ICollection<T>.CopyTo(T[] array, int arrayIndex) => _source.CopyTo(array, arrayIndex);
- bool ICollection<T>.Remove(T item) => _source.Remove(item);
- int ICollection<T>.Count => _source.Count;
- bool ICollection<T>.IsReadOnly => _source.IsReadOnly;
- int IList<T>.IndexOf(T item) => _source.IndexOf(item);
- void IList<T>.Insert(int index, T item) => _source.Insert(index, item);
- void IList<T>.RemoveAt(int index) => _source.RemoveAt(index);
- T IList<T>.this[int index]
- {
- get => _source[index];
- set => _source[index] = value;
- }
- }
- private sealed class AsyncICollectionEnumerableAdapter<T> : AsyncIterator<T>, IAsyncIListProvider<T>, ICollection<T>
- {
- private readonly ICollection<T> _source;
- private IEnumerator<T>? _enumerator;
- public AsyncICollectionEnumerableAdapter(ICollection<T> source)
- {
- _source = source;
- }
- public override AsyncIteratorBase<T> Clone() => new AsyncICollectionEnumerableAdapter<T>(_source);
- public override async ValueTask DisposeAsync()
- {
- if (_enumerator != null)
- {
- _enumerator.Dispose();
- _enumerator = null;
- }
- await base.DisposeAsync().ConfigureAwait(false);
- }
- protected override async ValueTask<bool> MoveNextCore()
- {
- switch (_state)
- {
- case AsyncIteratorState.Allocated:
- _enumerator = _source.GetEnumerator();
- _state = AsyncIteratorState.Iterating;
- goto case AsyncIteratorState.Iterating;
- case AsyncIteratorState.Iterating:
- if (_enumerator!.MoveNext())
- {
- _current = _enumerator.Current;
- return true;
- }
- await DisposeAsync().ConfigureAwait(false);
- break;
- }
- return false;
- }
- //
- // NB: These optimizations rely on the System.Linq implementation of IEnumerable<T> operators to optimize
- // and short-circuit as appropriate.
- //
- public ValueTask<T[]> ToArrayAsync(CancellationToken cancellationToken)
- {
- cancellationToken.ThrowIfCancellationRequested();
- return new ValueTask<T[]>(_source.ToArray());
- }
- public ValueTask<List<T>> ToListAsync(CancellationToken cancellationToken)
- {
- cancellationToken.ThrowIfCancellationRequested();
- return new ValueTask<List<T>>(_source.ToList());
- }
- public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
- {
- cancellationToken.ThrowIfCancellationRequested();
- return new ValueTask<int>(_source.Count);
- }
- IEnumerator<T> IEnumerable<T>.GetEnumerator() => _source.GetEnumerator();
- IEnumerator IEnumerable.GetEnumerator() => _source.GetEnumerator();
- void ICollection<T>.Add(T item) => _source.Add(item);
- void ICollection<T>.Clear() => _source.Clear();
- bool ICollection<T>.Contains(T item) => _source.Contains(item);
- void ICollection<T>.CopyTo(T[] array, int arrayIndex) => _source.CopyTo(array, arrayIndex);
- bool ICollection<T>.Remove(T item) => _source.Remove(item);
- int ICollection<T>.Count => _source.Count;
- bool ICollection<T>.IsReadOnly => _source.IsReadOnly;
- }
- }
- #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
- }
|