All.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 all elements of an async-enumerable sequence satisfy a condition.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  15. /// <param name="source">An async-enumerable sequence whose elements to apply the predicate to.</param>
  16. /// <param name="predicate">A function to test each element for a condition.</param>
  17. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  18. /// <returns>An async-enumerable sequence containing a single element determining whether all elements in the source sequence pass the test in the specified predicate.</returns>
  19. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
  20. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  21. public static ValueTask<bool> AllAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
  22. {
  23. if (source == null)
  24. throw Error.ArgumentNull(nameof(source));
  25. if (predicate == null)
  26. throw Error.ArgumentNull(nameof(predicate));
  27. return Core(source, predicate, cancellationToken);
  28. static async ValueTask<bool> Core(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  29. {
  30. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  31. {
  32. if (!predicate(item))
  33. {
  34. return false;
  35. }
  36. }
  37. return true;
  38. }
  39. }
  40. /// <summary>
  41. /// Determines whether all elements in an async-enumerable sequence satisfy a condition.
  42. /// </summary>
  43. /// <typeparam name="TSource">The type of element in the sequence.</typeparam>
  44. /// <param name="source">An async-enumerable sequence whose elements to apply the predicate to.</param>
  45. /// <param name="predicate">An asynchronous predicate to apply to each element of the source sequence.</param>
  46. /// <param name="cancellationToken">An optional cancellation token to be used for cancelling the sequence at any time.</param>
  47. /// <returns>A ValueTask containing a value indicating whether all elements in the sequence pass the test in the specified predicate.</returns>
  48. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is <see langword="null"/>.</exception>
  49. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  50. [GenerateAsyncOverload]
  51. private static ValueTask<bool> AllAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  52. {
  53. if (source == null)
  54. throw Error.ArgumentNull(nameof(source));
  55. if (predicate == null)
  56. throw Error.ArgumentNull(nameof(predicate));
  57. return Core(source, predicate, cancellationToken);
  58. static async ValueTask<bool> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  59. {
  60. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  61. {
  62. if (!await predicate(item).ConfigureAwait(false))
  63. {
  64. return false;
  65. }
  66. }
  67. return true;
  68. }
  69. }
  70. #if !NO_DEEP_CANCELLATION
  71. [GenerateAsyncOverload]
  72. internal static ValueTask<bool> AllAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  73. {
  74. if (source == null)
  75. throw Error.ArgumentNull(nameof(source));
  76. if (predicate == null)
  77. throw Error.ArgumentNull(nameof(predicate));
  78. return Core(source, predicate, cancellationToken);
  79. static async ValueTask<bool> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  80. {
  81. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  82. {
  83. if (!await predicate(item, cancellationToken).ConfigureAwait(false))
  84. {
  85. return false;
  86. }
  87. }
  88. return true;
  89. }
  90. }
  91. #endif
  92. }
  93. }