// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information. using System.Collections.Generic; using System.Diagnostics; using System.Threading; using System.Threading.Tasks; namespace System.Linq { public static partial class AsyncEnumerable { public static IAsyncEnumerable CreateEnumerable(Func> getEnumerator) { if (getEnumerator == null) throw new ArgumentNullException(nameof(getEnumerator)); return new AnonymousAsyncEnumerable(_ => getEnumerator()); } public static IAsyncEnumerable CreateEnumerable(Func> getEnumerator) { if (getEnumerator == null) throw new ArgumentNullException(nameof(getEnumerator)); return new AnonymousAsyncEnumerable(getEnumerator); } public static IAsyncEnumerable CreateEnumerable(Func>> getEnumerator) { if (getEnumerator == null) throw new ArgumentNullException(nameof(getEnumerator)); return new AnonymousAsyncEnumerableWithTask(_ => getEnumerator()); } public static IAsyncEnumerable CreateEnumerable(Func>> getEnumerator) { if (getEnumerator == null) throw new ArgumentNullException(nameof(getEnumerator)); return new AnonymousAsyncEnumerableWithTask(getEnumerator); } public static IAsyncEnumerator CreateEnumerator(Func> moveNext, Func current, Func dispose) { return AsyncEnumerator.Create(moveNext, current, dispose); } private static IAsyncEnumerator CreateEnumerator(Func, ValueTask> moveNext, Func current, Func dispose) { return AsyncEnumerator.Create(moveNext, current, dispose); } private sealed class AnonymousAsyncEnumerable : IAsyncEnumerable { private readonly Func> getEnumerator; public AnonymousAsyncEnumerable(Func> getEnumerator) { Debug.Assert(getEnumerator != null); this.getEnumerator = getEnumerator; } public IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken) => getEnumerator(cancellationToken); } private sealed class AnonymousAsyncEnumerableWithTask : IAsyncEnumerable { private readonly Func>> getEnumerator; public AnonymousAsyncEnumerableWithTask(Func>> getEnumerator) { Debug.Assert(getEnumerator != null); this.getEnumerator = getEnumerator; } public IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken) => new Enumerator(getEnumerator, cancellationToken); private sealed class Enumerator : IAsyncEnumerator { private Func>> getEnumerator; private readonly CancellationToken cancellationToken; private IAsyncEnumerator enumerator; public Enumerator(Func>> getEnumerator, CancellationToken cancellationToken) { Debug.Assert(getEnumerator != null); this.getEnumerator = getEnumerator; this.cancellationToken = cancellationToken; } public T Current { get { if (enumerator == null) throw new InvalidOperationException(); return enumerator.Current; } } public async ValueTask DisposeAsync() { var old = Interlocked.Exchange(ref enumerator, DisposedEnumerator.Instance); if (enumerator != null) { await enumerator.DisposeAsync().ConfigureAwait(false); } } public ValueTask MoveNextAsync() { if (enumerator == null) { return InitAndMoveNextAsync(); } return enumerator.MoveNextAsync(); } private async ValueTask InitAndMoveNextAsync() { try { enumerator = await getEnumerator(cancellationToken).ConfigureAwait(false); } catch (Exception ex) { enumerator = Throw(ex).GetAsyncEnumerator(cancellationToken); throw; } finally { getEnumerator = null; } return await enumerator.MoveNextAsync().ConfigureAwait(false); } private sealed class DisposedEnumerator : IAsyncEnumerator { public static readonly DisposedEnumerator Instance = new DisposedEnumerator(); public T Current => throw new ObjectDisposedException("this"); public ValueTask DisposeAsync() => TaskExt.CompletedTask; public ValueTask MoveNextAsync() => throw new ObjectDisposedException("this"); } } } } }