Create.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. public static IAsyncEnumerator<T> CreateEnumerator<T>(Func<ValueTask<bool>> moveNext, Func<T> current, Func<ValueTask> dispose)
  19. {
  20. return AsyncEnumerator.Create(moveNext, current, dispose);
  21. }
  22. private static IAsyncEnumerator<T> CreateEnumerator<T>(Func<TaskCompletionSource<bool>, ValueTask<bool>> moveNext, Func<T> current, Func<ValueTask> dispose)
  23. {
  24. return AsyncEnumerator.Create(moveNext, current, dispose);
  25. }
  26. private sealed class AnonymousAsyncEnumerable<T> : IAsyncEnumerable<T>
  27. {
  28. private readonly Func<CancellationToken, IAsyncEnumerator<T>> _getEnumerator;
  29. public AnonymousAsyncEnumerable(Func<CancellationToken, IAsyncEnumerator<T>> getEnumerator)
  30. {
  31. Debug.Assert(getEnumerator != null);
  32. _getEnumerator = getEnumerator;
  33. }
  34. public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken) => _getEnumerator(cancellationToken);
  35. }
  36. }
  37. }