1
0

Any.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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. /// <summary>
  12. /// Determines whether an async-enumerable sequence contains any elements.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  15. /// <param name="source">An async-enumerable sequence to check for non-emptiness.</param>
  16. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  17. /// <returns>An async-enumerable sequence containing a single element determining whether the source sequence contains any elements.</returns>
  18. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  19. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  20. public static ValueTask<bool> AnyAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  21. {
  22. if (source == null)
  23. throw Error.ArgumentNull(nameof(source));
  24. return Core(source, cancellationToken);
  25. static async ValueTask<bool> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  26. {
  27. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  28. return await e.MoveNextAsync();
  29. }
  30. }
  31. /// <summary>
  32. /// Determines whether any element of an async-enumerable sequence satisfies a condition.
  33. /// </summary>
  34. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  35. /// <param name="source">An async-enumerable sequence whose elements to apply the predicate to.</param>
  36. /// <param name="predicate">A function to test each element for a condition.</param>
  37. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  38. /// <returns>An async-enumerable sequence containing a single element determining whether any elements in the source sequence pass the test in the specified predicate.</returns>
  39. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
  40. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  41. public static ValueTask<bool> AnyAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
  42. {
  43. if (source == null)
  44. throw Error.ArgumentNull(nameof(source));
  45. if (predicate == null)
  46. throw Error.ArgumentNull(nameof(predicate));
  47. return Core(source, predicate, cancellationToken);
  48. static async ValueTask<bool> Core(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  49. {
  50. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  51. {
  52. if (predicate(item))
  53. {
  54. return true;
  55. }
  56. }
  57. return false;
  58. }
  59. }
  60. /// <summary>
  61. /// Determines whether any element in an async-enumerable sequence satisfies a condition.
  62. /// </summary>
  63. /// <typeparam name="TSource">The type of element in the sequence.</typeparam>
  64. /// <param name="source">An async-enumerable sequence whose elements to apply the predicate to.</param>
  65. /// <param name="predicate">An asynchronous predicate to apply to each element of the source sequence.</param>
  66. /// <param name="cancellationToken">An optional cancellation token to be used for cancelling the sequence at any time.</param>
  67. /// <returns>A ValueTask containing a value indicating whether any elements in the source sequence pass the test in the specified predicate.</returns>
  68. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is <see langword="null"/>.</exception>
  69. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  70. [GenerateAsyncOverload]
  71. private static ValueTask<bool> AnyAwaitAsyncCore<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 ValueTask<bool> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  79. {
  80. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  81. {
  82. if (await predicate(item).ConfigureAwait(false))
  83. {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. }
  90. #if !NO_DEEP_CANCELLATION
  91. [GenerateAsyncOverload]
  92. internal static ValueTask<bool> AnyAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  93. {
  94. if (source == null)
  95. throw Error.ArgumentNull(nameof(source));
  96. if (predicate == null)
  97. throw Error.ArgumentNull(nameof(predicate));
  98. return Core(source, predicate, cancellationToken);
  99. static async ValueTask<bool> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  100. {
  101. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  102. {
  103. if (await predicate(item, cancellationToken).ConfigureAwait(false))
  104. {
  105. return true;
  106. }
  107. }
  108. return false;
  109. }
  110. }
  111. #endif
  112. }
  113. }