All.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  21. {
  22. if (!_predicate(item))
  23. {
  24. return false;
  25. }
  26. }
  27. return true;
  28. }
  29. }
  30. public static Task<bool> AllAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  31. {
  32. if (source == null)
  33. throw Error.ArgumentNull(nameof(source));
  34. if (predicate == null)
  35. throw Error.ArgumentNull(nameof(predicate));
  36. return Core(source, predicate, cancellationToken);
  37. static async Task<bool> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  38. {
  39. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  40. {
  41. if (!await _predicate(item).ConfigureAwait(false))
  42. {
  43. return false;
  44. }
  45. }
  46. return true;
  47. }
  48. }
  49. #if !NO_DEEP_CANCELLATION
  50. public static Task<bool> AllAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  51. {
  52. if (source == null)
  53. throw Error.ArgumentNull(nameof(source));
  54. if (predicate == null)
  55. throw Error.ArgumentNull(nameof(predicate));
  56. return Core(source, predicate, cancellationToken);
  57. static async Task<bool> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  58. {
  59. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  60. {
  61. if (!await _predicate(item, _cancellationToken).ConfigureAwait(false))
  62. {
  63. return false;
  64. }
  65. }
  66. return true;
  67. }
  68. }
  69. #endif
  70. }
  71. }