AsyncEnumerable.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. // REVIEW: [LDM-2018-11-28] Should return type be a struct or just the interface type?
  19. public static WithCancellationTokenAsyncEnumerable<T> WithCancellationToken<T>(this IAsyncEnumerable<T> source, CancellationToken cancellationToken)
  20. {
  21. if (source == null)
  22. throw Error.ArgumentNull(nameof(source));
  23. return new WithCancellationTokenAsyncEnumerable<T>(source, cancellationToken);
  24. }
  25. private sealed class AnonymousAsyncEnumerable<T> : IAsyncEnumerable<T>
  26. {
  27. private readonly Func<CancellationToken, IAsyncEnumerator<T>> _getEnumerator;
  28. public AnonymousAsyncEnumerable(Func<CancellationToken, IAsyncEnumerator<T>> getEnumerator)
  29. {
  30. Debug.Assert(getEnumerator != null);
  31. _getEnumerator = getEnumerator;
  32. }
  33. public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken)
  34. {
  35. cancellationToken.ThrowIfCancellationRequested(); // NB: [LDM-2018-11-28] Equivalent to async iterator behavior.
  36. return _getEnumerator(cancellationToken);
  37. }
  38. }
  39. // REVIEW: Explicit implementation of the interfaces allows for composition with other "modifier operators" such as ConfigureAwait.
  40. // We expect that the "await foreach" statement will bind to the public struct methods, thus avoiding boxing.
  41. public readonly struct WithCancellationTokenAsyncEnumerable<T> : IAsyncEnumerable<T>
  42. {
  43. private readonly IAsyncEnumerable<T> _source;
  44. private readonly CancellationToken _cancellationToken;
  45. public WithCancellationTokenAsyncEnumerable(IAsyncEnumerable<T> source, CancellationToken cancellationToken)
  46. {
  47. _source = source;
  48. _cancellationToken = cancellationToken;
  49. }
  50. // REVIEW: Should we simply ignore the second cancellation token or should we link the two?
  51. // REVIEW: [LDM-2018-11-28] Should we have eager cancellation here too?
  52. public WithCancellationAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken)
  53. => new WithCancellationAsyncEnumerator(_source.GetAsyncEnumerator(_cancellationToken));
  54. IAsyncEnumerator<T> IAsyncEnumerable<T>.GetAsyncEnumerator(CancellationToken cancellationToken)
  55. => GetAsyncEnumerator(cancellationToken);
  56. public readonly struct WithCancellationAsyncEnumerator : IAsyncEnumerator<T>
  57. {
  58. private readonly IAsyncEnumerator<T> _enumerator;
  59. public WithCancellationAsyncEnumerator(IAsyncEnumerator<T> enumerator)
  60. {
  61. _enumerator = enumerator;
  62. }
  63. public T Current => _enumerator.Current;
  64. public ValueTask DisposeAsync() => _enumerator.DisposeAsync();
  65. public ValueTask<bool> MoveNextAsync() => _enumerator.MoveNextAsync();
  66. }
  67. }
  68. }
  69. }