Create.cs 4.3 KB

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