Create.cs 1.2 KB

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