Any.cs 4.5 KB

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