Any.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using Cysharp.Threading.Tasks.Internal;
  2. using System;
  3. using System.Threading;
  4. namespace Cysharp.Threading.Tasks.Linq
  5. {
  6. public static partial class UniTaskAsyncEnumerable
  7. {
  8. public static UniTask<Boolean> AnyAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  9. {
  10. Error.ThrowArgumentNullException(source, nameof(source));
  11. return Any.AnyAsync(source, cancellationToken);
  12. }
  13. public static UniTask<Boolean> AnyAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
  14. {
  15. Error.ThrowArgumentNullException(source, nameof(source));
  16. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  17. return Any.AnyAsync(source, predicate, cancellationToken);
  18. }
  19. public static UniTask<Boolean> AnyAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
  20. {
  21. Error.ThrowArgumentNullException(source, nameof(source));
  22. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  23. return Any.AnyAwaitAsync(source, predicate, cancellationToken);
  24. }
  25. public static UniTask<Boolean> AnyAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
  26. {
  27. Error.ThrowArgumentNullException(source, nameof(source));
  28. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  29. return Any.AnyAwaitWithCancellationAsync(source, predicate, cancellationToken);
  30. }
  31. }
  32. internal static class Any
  33. {
  34. internal static async UniTask<bool> AnyAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  35. {
  36. var e = source.GetAsyncEnumerator(cancellationToken);
  37. try
  38. {
  39. if (await e.MoveNextAsync())
  40. {
  41. return true;
  42. }
  43. return false;
  44. }
  45. finally
  46. {
  47. if (e != null)
  48. {
  49. await e.DisposeAsync();
  50. }
  51. }
  52. }
  53. internal static async UniTask<bool> AnyAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken)
  54. {
  55. var e = source.GetAsyncEnumerator(cancellationToken);
  56. try
  57. {
  58. while (await e.MoveNextAsync())
  59. {
  60. if (predicate(e.Current))
  61. {
  62. return true;
  63. }
  64. }
  65. return false;
  66. }
  67. finally
  68. {
  69. if (e != null)
  70. {
  71. await e.DisposeAsync();
  72. }
  73. }
  74. }
  75. internal static async UniTask<bool> AnyAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken)
  76. {
  77. var e = source.GetAsyncEnumerator(cancellationToken);
  78. try
  79. {
  80. while (await e.MoveNextAsync())
  81. {
  82. if (await predicate(e.Current))
  83. {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. finally
  90. {
  91. if (e != null)
  92. {
  93. await e.DisposeAsync();
  94. }
  95. }
  96. }
  97. internal static async UniTask<bool> AnyAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken)
  98. {
  99. var e = source.GetAsyncEnumerator(cancellationToken);
  100. try
  101. {
  102. while (await e.MoveNextAsync())
  103. {
  104. if (await predicate(e.Current, cancellationToken))
  105. {
  106. return true;
  107. }
  108. }
  109. return false;
  110. }
  111. finally
  112. {
  113. if (e != null)
  114. {
  115. await e.DisposeAsync();
  116. }
  117. }
  118. }
  119. }
  120. }