All.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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> AllAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
  12. {
  13. if (source == null)
  14. throw Error.ArgumentNull(nameof(source));
  15. if (predicate == null)
  16. throw Error.ArgumentNull(nameof(predicate));
  17. return Core(source, predicate, cancellationToken);
  18. static async Task<bool> Core(IAsyncEnumerable<TSource> _source, Func<TSource, bool> _predicate, CancellationToken _cancellationToken)
  19. {
  20. #if CSHARP8
  21. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  22. {
  23. if (!_predicate(item))
  24. {
  25. return false;
  26. }
  27. }
  28. #else
  29. var e = _source.GetAsyncEnumerator(_cancellationToken);
  30. try
  31. {
  32. while (await e.MoveNextAsync().ConfigureAwait(false))
  33. {
  34. if (!_predicate(e.Current))
  35. return false;
  36. }
  37. }
  38. finally
  39. {
  40. await e.DisposeAsync().ConfigureAwait(false);
  41. }
  42. #endif
  43. return true;
  44. }
  45. }
  46. public static Task<bool> AllAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  47. {
  48. if (source == null)
  49. throw Error.ArgumentNull(nameof(source));
  50. if (predicate == null)
  51. throw Error.ArgumentNull(nameof(predicate));
  52. return Core(source, predicate, cancellationToken);
  53. static async Task<bool> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  54. {
  55. #if CSHARP8
  56. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  57. {
  58. if (!await _predicate(item).ConfigureAwait(false))
  59. {
  60. return false;
  61. }
  62. }
  63. #else
  64. var e = _source.GetAsyncEnumerator(_cancellationToken);
  65. try
  66. {
  67. while (await e.MoveNextAsync().ConfigureAwait(false))
  68. {
  69. if (!await _predicate(e.Current).ConfigureAwait(false))
  70. return false;
  71. }
  72. }
  73. finally
  74. {
  75. await e.DisposeAsync().ConfigureAwait(false);
  76. }
  77. #endif
  78. return true;
  79. }
  80. }
  81. #if !NO_DEEP_CANCELLATION
  82. public static Task<bool> AllAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  83. {
  84. if (source == null)
  85. throw Error.ArgumentNull(nameof(source));
  86. if (predicate == null)
  87. throw Error.ArgumentNull(nameof(predicate));
  88. return Core(source, predicate, cancellationToken);
  89. static async Task<bool> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  90. {
  91. #if CSHARP8
  92. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  93. {
  94. if (!await _predicate(item, _cancellationToken).ConfigureAwait(false))
  95. {
  96. return false;
  97. }
  98. }
  99. #else
  100. var e = _source.GetAsyncEnumerator(_cancellationToken);
  101. try
  102. {
  103. while (await e.MoveNextAsync().ConfigureAwait(false))
  104. {
  105. if (!await _predicate(e.Current, _cancellationToken).ConfigureAwait(false))
  106. return false;
  107. }
  108. }
  109. finally
  110. {
  111. await e.DisposeAsync().ConfigureAwait(false);
  112. }
  113. #endif
  114. return true;
  115. }
  116. }
  117. #endif
  118. }
  119. }