All.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. async Task<bool> Core(IAsyncEnumerable<TSource> _source, Func<TSource, bool> _predicate, CancellationToken _cancellationToken)
  19. {
  20. var e = _source.GetAsyncEnumerator(_cancellationToken);
  21. try
  22. {
  23. while (await e.MoveNextAsync().ConfigureAwait(false))
  24. {
  25. if (!_predicate(e.Current))
  26. return false;
  27. }
  28. }
  29. finally
  30. {
  31. await e.DisposeAsync().ConfigureAwait(false);
  32. }
  33. return true;
  34. }
  35. }
  36. public static Task<bool> AllAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  37. {
  38. if (source == null)
  39. throw Error.ArgumentNull(nameof(source));
  40. if (predicate == null)
  41. throw Error.ArgumentNull(nameof(predicate));
  42. return Core(source, predicate, cancellationToken);
  43. async Task<bool> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  44. {
  45. var e = _source.GetAsyncEnumerator(_cancellationToken);
  46. try
  47. {
  48. while (await e.MoveNextAsync().ConfigureAwait(false))
  49. {
  50. if (!await _predicate(e.Current).ConfigureAwait(false))
  51. return false;
  52. }
  53. }
  54. finally
  55. {
  56. await e.DisposeAsync().ConfigureAwait(false);
  57. }
  58. return true;
  59. }
  60. }
  61. #if !NO_DEEP_CANCELLATION
  62. public static Task<bool> AllAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  63. {
  64. if (source == null)
  65. throw Error.ArgumentNull(nameof(source));
  66. if (predicate == null)
  67. throw Error.ArgumentNull(nameof(predicate));
  68. return Core(source, predicate, cancellationToken);
  69. async Task<bool> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  70. {
  71. var e = _source.GetAsyncEnumerator(_cancellationToken);
  72. try
  73. {
  74. while (await e.MoveNextAsync().ConfigureAwait(false))
  75. {
  76. if (!await _predicate(e.Current, _cancellationToken).ConfigureAwait(false))
  77. return false;
  78. }
  79. }
  80. finally
  81. {
  82. await e.DisposeAsync().ConfigureAwait(false);
  83. }
  84. return true;
  85. }
  86. }
  87. #endif
  88. }
  89. }