Create.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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<IAsyncEnumerator<T>> getEnumerator)
  13. {
  14. if (getEnumerator == null)
  15. {
  16. throw new ArgumentNullException(nameof(getEnumerator));
  17. }
  18. return new AnonymousAsyncEnumerable<T>(getEnumerator);
  19. }
  20. public static IAsyncEnumerator<T> CreateEnumerator<T>(Func<CancellationToken, Task<bool>> moveNext, Func<T> current, Action dispose)
  21. {
  22. if (moveNext == null)
  23. {
  24. throw new ArgumentNullException(nameof(moveNext));
  25. }
  26. // Note: Many methods pass null in for the second two parameters. We're assuming
  27. // That the caller is responsible and knows what they're doing
  28. return new AnonymousAsyncIterator<T>(moveNext, current, dispose);
  29. }
  30. private static IAsyncEnumerator<T> CreateEnumerator<T>(Func<CancellationToken, TaskCompletionSource<bool>, Task<bool>> moveNext, Func<T> current, Action dispose)
  31. {
  32. var self = new AnonymousAsyncIterator<T>(
  33. async ct =>
  34. {
  35. var tcs = new TaskCompletionSource<bool>();
  36. var stop = new Action(
  37. () =>
  38. {
  39. tcs.TrySetCanceled();
  40. });
  41. using (ct.Register(stop))
  42. {
  43. return await moveNext(ct, tcs)
  44. .ConfigureAwait(false);
  45. }
  46. },
  47. current,
  48. dispose
  49. );
  50. return self;
  51. }
  52. private class AnonymousAsyncEnumerable<T> : IAsyncEnumerable<T>
  53. {
  54. private readonly Func<IAsyncEnumerator<T>> getEnumerator;
  55. public AnonymousAsyncEnumerable(Func<IAsyncEnumerator<T>> getEnumerator)
  56. {
  57. Debug.Assert(getEnumerator != null);
  58. this.getEnumerator = getEnumerator;
  59. }
  60. public IAsyncEnumerator<T> GetEnumerator()
  61. {
  62. return getEnumerator();
  63. }
  64. }
  65. private sealed class AnonymousAsyncIterator<T> : AsyncIterator<T>
  66. {
  67. private readonly Func<T> currentFunc;
  68. private readonly Func<CancellationToken, Task<bool>> moveNext;
  69. private Action dispose;
  70. public AnonymousAsyncIterator(Func<CancellationToken, Task<bool>> moveNext, Func<T> currentFunc, Action dispose)
  71. {
  72. Debug.Assert(moveNext != null);
  73. this.moveNext = moveNext;
  74. this.currentFunc = currentFunc;
  75. Volatile.Write(ref this.dispose, dispose);
  76. // Explicit call to initialize enumerator mode
  77. GetEnumerator();
  78. }
  79. public override AsyncIterator<T> Clone()
  80. {
  81. throw new NotSupportedException("AnonymousAsyncIterator cannot be cloned. It is only intended for use as an iterator.");
  82. }
  83. public override void Dispose()
  84. {
  85. Interlocked.Exchange(ref this.dispose, null)?.Invoke();
  86. base.Dispose();
  87. }
  88. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  89. {
  90. switch (state)
  91. {
  92. case AsyncIteratorState.Allocated:
  93. state = AsyncIteratorState.Iterating;
  94. goto case AsyncIteratorState.Iterating;
  95. case AsyncIteratorState.Iterating:
  96. if (await moveNext(cancellationToken).ConfigureAwait(false))
  97. {
  98. current = currentFunc();
  99. return true;
  100. }
  101. Dispose();
  102. break;
  103. }
  104. return false;
  105. }
  106. }
  107. }
  108. }