Create.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  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. using System.Diagnostics;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerable
  11. {
  12. public static IAsyncEnumerable<T> CreateEnumerable<T>(Func<CancellationToken, IAsyncEnumerator<T>> getEnumerator)
  13. {
  14. if (getEnumerator == null)
  15. throw Error.ArgumentNull(nameof(getEnumerator));
  16. return new AnonymousAsyncEnumerable<T>(getEnumerator);
  17. }
  18. private sealed class AnonymousAsyncEnumerable<T> : IAsyncEnumerable<T>
  19. {
  20. private readonly Func<CancellationToken, IAsyncEnumerator<T>> _getEnumerator;
  21. public AnonymousAsyncEnumerable(Func<CancellationToken, IAsyncEnumerator<T>> getEnumerator)
  22. {
  23. Debug.Assert(getEnumerator != null);
  24. _getEnumerator = getEnumerator;
  25. }
  26. public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken) => _getEnumerator(cancellationToken);
  27. }
  28. }
  29. }