All.cs 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 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. internal static ValueTask<bool> AllAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  41. {
  42. if (source == null)
  43. throw Error.ArgumentNull(nameof(source));
  44. if (predicate == null)
  45. throw Error.ArgumentNull(nameof(predicate));
  46. return Core(source, predicate, cancellationToken);
  47. static async ValueTask<bool> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  48. {
  49. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  50. {
  51. if (!await predicate(item).ConfigureAwait(false))
  52. {
  53. return false;
  54. }
  55. }
  56. return true;
  57. }
  58. }
  59. #if !NO_DEEP_CANCELLATION
  60. internal static ValueTask<bool> AllAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, 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, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  68. {
  69. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  70. {
  71. if (!await predicate(item, cancellationToken).ConfigureAwait(false))
  72. {
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. }
  79. #endif
  80. }
  81. }