Single.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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<TSource> SingleAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  9. {
  10. Error.ThrowArgumentNullException(source, nameof(source));
  11. return SingleOperator.SingleAsync(source, cancellationToken, false);
  12. }
  13. public static UniTask<TSource> SingleAsync<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 SingleOperator.SingleAsync(source, predicate, cancellationToken, false);
  18. }
  19. public static UniTask<TSource> SingleAwaitAsync<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 SingleOperator.SingleAwaitAsync(source, predicate, cancellationToken, false);
  24. }
  25. public static UniTask<TSource> SingleAwaitWithCancellationAsync<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 SingleOperator.SingleAwaitWithCancellationAsync(source, predicate, cancellationToken, false);
  30. }
  31. public static UniTask<TSource> SingleOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  32. {
  33. Error.ThrowArgumentNullException(source, nameof(source));
  34. return SingleOperator.SingleAsync(source, cancellationToken, true);
  35. }
  36. public static UniTask<TSource> SingleOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
  37. {
  38. Error.ThrowArgumentNullException(source, nameof(source));
  39. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  40. return SingleOperator.SingleAsync(source, predicate, cancellationToken, true);
  41. }
  42. public static UniTask<TSource> SingleOrDefaultAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
  43. {
  44. Error.ThrowArgumentNullException(source, nameof(source));
  45. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  46. return SingleOperator.SingleAwaitAsync(source, predicate, cancellationToken, true);
  47. }
  48. public static UniTask<TSource> SingleOrDefaultAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
  49. {
  50. Error.ThrowArgumentNullException(source, nameof(source));
  51. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  52. return SingleOperator.SingleAwaitWithCancellationAsync(source, predicate, cancellationToken, true);
  53. }
  54. }
  55. internal static class SingleOperator
  56. {
  57. public static async UniTask<TSource> SingleAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken, bool defaultIfEmpty)
  58. {
  59. var e = source.GetAsyncEnumerator(cancellationToken);
  60. try
  61. {
  62. if (await e.MoveNextAsync())
  63. {
  64. var v = e.Current;
  65. if (!await e.MoveNextAsync())
  66. {
  67. return v;
  68. }
  69. throw Error.MoreThanOneElement();
  70. }
  71. else
  72. {
  73. if (defaultIfEmpty)
  74. {
  75. return default;
  76. }
  77. else
  78. {
  79. throw Error.NoElements();
  80. }
  81. }
  82. }
  83. finally
  84. {
  85. if (e != null)
  86. {
  87. await e.DisposeAsync();
  88. }
  89. }
  90. }
  91. public static async UniTask<TSource> SingleAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken, bool defaultIfEmpty)
  92. {
  93. var e = source.GetAsyncEnumerator(cancellationToken);
  94. try
  95. {
  96. TSource value = default;
  97. bool found = false;
  98. while (await e.MoveNextAsync())
  99. {
  100. var v = e.Current;
  101. if (predicate(v))
  102. {
  103. if (found)
  104. {
  105. throw Error.MoreThanOneElement();
  106. }
  107. else
  108. {
  109. found = true;
  110. value = v;
  111. }
  112. }
  113. }
  114. if (found || defaultIfEmpty)
  115. {
  116. return value;
  117. }
  118. throw Error.NoElements();
  119. }
  120. finally
  121. {
  122. if (e != null)
  123. {
  124. await e.DisposeAsync();
  125. }
  126. }
  127. }
  128. public static async UniTask<TSource> SingleAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken, bool defaultIfEmpty)
  129. {
  130. var e = source.GetAsyncEnumerator(cancellationToken);
  131. try
  132. {
  133. TSource value = default;
  134. bool found = false;
  135. while (await e.MoveNextAsync())
  136. {
  137. var v = e.Current;
  138. if (await predicate(v))
  139. {
  140. if (found)
  141. {
  142. throw Error.MoreThanOneElement();
  143. }
  144. else
  145. {
  146. found = true;
  147. value = v;
  148. }
  149. }
  150. }
  151. if (found || defaultIfEmpty)
  152. {
  153. return value;
  154. }
  155. throw Error.NoElements();
  156. }
  157. finally
  158. {
  159. if (e != null)
  160. {
  161. await e.DisposeAsync();
  162. }
  163. }
  164. }
  165. public static async UniTask<TSource> SingleAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken, bool defaultIfEmpty)
  166. {
  167. var e = source.GetAsyncEnumerator(cancellationToken);
  168. try
  169. {
  170. TSource value = default;
  171. bool found = false;
  172. while (await e.MoveNextAsync())
  173. {
  174. var v = e.Current;
  175. if (await predicate(v, cancellationToken))
  176. {
  177. if (found)
  178. {
  179. throw Error.MoreThanOneElement();
  180. }
  181. else
  182. {
  183. found = true;
  184. value = v;
  185. }
  186. }
  187. }
  188. if (found || defaultIfEmpty)
  189. {
  190. return value;
  191. }
  192. throw Error.NoElements();
  193. }
  194. finally
  195. {
  196. if (e != null)
  197. {
  198. await e.DisposeAsync();
  199. }
  200. }
  201. }
  202. }
  203. }