Any.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. /// <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. internal static ValueTask<bool> AnyAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  61. {
  62. if (source == null)
  63. throw Error.ArgumentNull(nameof(source));
  64. if (predicate == null)
  65. throw Error.ArgumentNull(nameof(predicate));
  66. return Core(source, predicate, cancellationToken);
  67. static async ValueTask<bool> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  68. {
  69. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  70. {
  71. if (await predicate(item).ConfigureAwait(false))
  72. {
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. }
  79. #if !NO_DEEP_CANCELLATION
  80. internal static ValueTask<bool> AnyAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  81. {
  82. if (source == null)
  83. throw Error.ArgumentNull(nameof(source));
  84. if (predicate == null)
  85. throw Error.ArgumentNull(nameof(predicate));
  86. return Core(source, predicate, cancellationToken);
  87. static async ValueTask<bool> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  88. {
  89. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  90. {
  91. if (await predicate(item, cancellationToken).ConfigureAwait(false))
  92. {
  93. return true;
  94. }
  95. }
  96. return false;
  97. }
  98. }
  99. #endif
  100. }
  101. }