Any.cs 5.6 KB

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