AsyncEnumerable.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. namespace System.Linq
  6. {
  7. public static partial class AsyncEnumerable
  8. {
  9. public static IAsyncEnumerable<T> CreateEnumerable<T>(Func<CancellationToken, IAsyncEnumerator<T>> getEnumerator)
  10. {
  11. if (getEnumerator == null)
  12. throw Error.ArgumentNull(nameof(getEnumerator));
  13. return new AnonymousAsyncEnumerable<T>(getEnumerator);
  14. }
  15. private sealed class AnonymousAsyncEnumerable<T> : IAsyncEnumerable<T>
  16. {
  17. private readonly Func<CancellationToken, IAsyncEnumerator<T>> _getEnumerator;
  18. public AnonymousAsyncEnumerable(Func<CancellationToken, IAsyncEnumerator<T>> getEnumerator)
  19. {
  20. Debug.Assert(getEnumerator != null);
  21. _getEnumerator = getEnumerator;
  22. }
  23. public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken) => _getEnumerator(cancellationToken);
  24. }
  25. }
  26. }