ToAsyncEnumerable.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace System.Linq
  11. {
  12. public static partial class AsyncEnumerable
  13. {
  14. public static IAsyncEnumerable<TSource> ToAsyncEnumerable<TSource>(this IEnumerable<TSource> source)
  15. {
  16. if (source == null)
  17. throw new ArgumentNullException(nameof(source));
  18. return new AsyncEnumerableAdapter<TSource>(source);
  19. }
  20. public static IAsyncEnumerable<TSource> ToAsyncEnumerable<TSource>(this Task<TSource> task)
  21. {
  22. if (task == null)
  23. throw new ArgumentNullException(nameof(task));
  24. return CreateEnumerable(
  25. () =>
  26. {
  27. var called = 0;
  28. var value = default(TSource);
  29. return CreateEnumerator(
  30. async ct =>
  31. {
  32. if (Interlocked.CompareExchange(ref called, 1, 0) == 0)
  33. {
  34. value = await task.ConfigureAwait(false);
  35. return true;
  36. }
  37. return false;
  38. },
  39. () => value,
  40. () => { });
  41. });
  42. }
  43. public static IEnumerable<TSource> ToEnumerable<TSource>(this IAsyncEnumerable<TSource> source)
  44. {
  45. if (source == null)
  46. throw new ArgumentNullException(nameof(source));
  47. return ToEnumerable_(source);
  48. }
  49. private static IEnumerable<TSource> ToEnumerable_<TSource>(IAsyncEnumerable<TSource> source)
  50. {
  51. using (var e = source.GetEnumerator())
  52. {
  53. while (true)
  54. {
  55. if (!e.MoveNext(CancellationToken.None)
  56. .Result)
  57. break;
  58. var c = e.Current;
  59. yield return c;
  60. }
  61. }
  62. }
  63. internal sealed class AsyncEnumerableAdapter<T> : AsyncIterator<T>, IIListProvider<T>
  64. {
  65. private readonly IEnumerable<T> source;
  66. private IEnumerator<T> enumerator;
  67. public AsyncEnumerableAdapter(IEnumerable<T> source)
  68. {
  69. Debug.Assert(source != null);
  70. this.source = source;
  71. }
  72. public override AsyncIterator<T> Clone()
  73. {
  74. return new AsyncEnumerableAdapter<T>(source);
  75. }
  76. public override void Dispose()
  77. {
  78. if (enumerator != null)
  79. {
  80. enumerator.Dispose();
  81. enumerator = null;
  82. }
  83. base.Dispose();
  84. }
  85. protected override Task<bool> MoveNextCore(CancellationToken cancellationToken)
  86. {
  87. switch (state)
  88. {
  89. case State.Allocated:
  90. enumerator = source.GetEnumerator();
  91. state = State.Iterating;
  92. goto case State.Iterating;
  93. case State.Iterating:
  94. if (enumerator.MoveNext())
  95. {
  96. current = enumerator.Current;
  97. return Task.FromResult(true);
  98. }
  99. Dispose();
  100. break;
  101. }
  102. return Task.FromResult(false);
  103. }
  104. // These optimizations rely on the Sys.Linq impls from IEnumerable to optimize
  105. // and short circuit as appropriate
  106. public Task<T[]> ToArrayAsync(CancellationToken cancellationToken)
  107. {
  108. return Task.FromResult(source.ToArray());
  109. }
  110. public Task<List<T>> ToListAsync(CancellationToken cancellationToken)
  111. {
  112. return Task.FromResult(source.ToList());
  113. }
  114. public Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  115. {
  116. return Task.FromResult(source.Count());
  117. }
  118. }
  119. }
  120. }