Any.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. public static Task<bool> AnyAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  12. {
  13. if (source == null)
  14. throw Error.ArgumentNull(nameof(source));
  15. return Core(source, cancellationToken);
  16. static async Task<bool> Core(IAsyncEnumerable<TSource> _source, CancellationToken _cancellationToken)
  17. {
  18. #if CSHARP8
  19. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  20. {
  21. return await e.MoveNextAsync();
  22. }
  23. #else
  24. var e = _source.GetAsyncEnumerator(_cancellationToken);
  25. try
  26. {
  27. return await e.MoveNextAsync().ConfigureAwait(false);
  28. }
  29. finally
  30. {
  31. await e.DisposeAsync().ConfigureAwait(false);
  32. }
  33. #endif
  34. }
  35. }
  36. public static Task<bool> AnyAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
  37. {
  38. if (source == null)
  39. throw Error.ArgumentNull(nameof(source));
  40. if (predicate == null)
  41. throw Error.ArgumentNull(nameof(predicate));
  42. return Core(source, predicate, cancellationToken);
  43. static async Task<bool> Core(IAsyncEnumerable<TSource> _source, Func<TSource, bool> _predicate, CancellationToken _cancellationToken)
  44. {
  45. #if CSHARP8
  46. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  47. {
  48. if (_predicate(item))
  49. {
  50. return true;
  51. }
  52. }
  53. #else
  54. var e = _source.GetAsyncEnumerator(_cancellationToken);
  55. try
  56. {
  57. while (await e.MoveNextAsync().ConfigureAwait(false))
  58. {
  59. if (_predicate(e.Current))
  60. return true;
  61. }
  62. }
  63. finally
  64. {
  65. await e.DisposeAsync().ConfigureAwait(false);
  66. }
  67. #endif
  68. return false;
  69. }
  70. }
  71. public static Task<bool> AnyAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  72. {
  73. if (source == null)
  74. throw Error.ArgumentNull(nameof(source));
  75. if (predicate == null)
  76. throw Error.ArgumentNull(nameof(predicate));
  77. return Core(source, predicate, cancellationToken);
  78. static async Task<bool> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  79. {
  80. #if CSHARP8
  81. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  82. {
  83. if (await _predicate(item).ConfigureAwait(false))
  84. {
  85. return true;
  86. }
  87. }
  88. #else
  89. var e = _source.GetAsyncEnumerator(_cancellationToken);
  90. try
  91. {
  92. while (await e.MoveNextAsync().ConfigureAwait(false))
  93. {
  94. if (await _predicate(e.Current).ConfigureAwait(false))
  95. return true;
  96. }
  97. }
  98. finally
  99. {
  100. await e.DisposeAsync().ConfigureAwait(false);
  101. }
  102. #endif
  103. return false;
  104. }
  105. }
  106. #if !NO_DEEP_CANCELLATION
  107. public static Task<bool> AnyAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  108. {
  109. if (source == null)
  110. throw Error.ArgumentNull(nameof(source));
  111. if (predicate == null)
  112. throw Error.ArgumentNull(nameof(predicate));
  113. return Core(source, predicate, cancellationToken);
  114. static async Task<bool> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  115. {
  116. #if CSHARP8
  117. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  118. {
  119. if (await _predicate(item, _cancellationToken).ConfigureAwait(false))
  120. {
  121. return true;
  122. }
  123. }
  124. #else
  125. var e = _source.GetAsyncEnumerator(_cancellationToken);
  126. try
  127. {
  128. while (await e.MoveNextAsync().ConfigureAwait(false))
  129. {
  130. if (await _predicate(e.Current, _cancellationToken).ConfigureAwait(false))
  131. return true;
  132. }
  133. }
  134. finally
  135. {
  136. await e.DisposeAsync().ConfigureAwait(false);
  137. }
  138. #endif
  139. return false;
  140. }
  141. }
  142. #endif
  143. }
  144. }