Single.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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<TSource> SingleAsync<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. static async Task<TSource> Core(IAsyncEnumerable<TSource> _source, CancellationToken _cancellationToken)
  17. {
  18. if (_source is IList<TSource> list)
  19. {
  20. switch (list.Count)
  21. {
  22. case 0: throw Error.NoElements();
  23. case 1: return list[0];
  24. }
  25. throw Error.MoreThanOneElement();
  26. }
  27. #if USE_AWAIT_USING
  28. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  29. {
  30. if (!await e.MoveNextAsync())
  31. {
  32. throw Error.NoElements();
  33. }
  34. var result = e.Current;
  35. if (await e.MoveNextAsync())
  36. {
  37. throw Error.MoreThanOneElement();
  38. }
  39. return result;
  40. }
  41. #else
  42. var e = _source.GetAsyncEnumerator(_cancellationToken);
  43. try
  44. {
  45. if (!await e.MoveNextAsync().ConfigureAwait(false))
  46. {
  47. throw Error.NoElements();
  48. }
  49. var result = e.Current;
  50. if (await e.MoveNextAsync().ConfigureAwait(false))
  51. {
  52. throw Error.MoreThanOneElement();
  53. }
  54. return result;
  55. }
  56. finally
  57. {
  58. await e.DisposeAsync().ConfigureAwait(false);
  59. }
  60. #endif
  61. }
  62. }
  63. public static Task<TSource> SingleAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
  64. {
  65. if (source == null)
  66. throw Error.ArgumentNull(nameof(source));
  67. if (predicate == null)
  68. throw Error.ArgumentNull(nameof(predicate));
  69. return Core(source, predicate, cancellationToken);
  70. static async Task<TSource> Core(IAsyncEnumerable<TSource> _source, Func<TSource, bool> _predicate, CancellationToken _cancellationToken)
  71. {
  72. #if USE_AWAIT_USING
  73. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  74. {
  75. while (await e.MoveNextAsync())
  76. {
  77. var result = e.Current;
  78. if (_predicate(result))
  79. {
  80. while (await e.MoveNextAsync())
  81. {
  82. if (_predicate(e.Current))
  83. {
  84. throw Error.MoreThanOneElement();
  85. }
  86. }
  87. return result;
  88. }
  89. }
  90. }
  91. #else
  92. var e = _source.GetAsyncEnumerator(_cancellationToken);
  93. try
  94. {
  95. while (await e.MoveNextAsync().ConfigureAwait(false))
  96. {
  97. var result = e.Current;
  98. if (_predicate(result))
  99. {
  100. while (await e.MoveNextAsync().ConfigureAwait(false))
  101. {
  102. if (_predicate(e.Current))
  103. {
  104. throw Error.MoreThanOneElement();
  105. }
  106. }
  107. return result;
  108. }
  109. }
  110. }
  111. finally
  112. {
  113. await e.DisposeAsync().ConfigureAwait(false);
  114. }
  115. #endif
  116. throw Error.NoElements();
  117. }
  118. }
  119. public static Task<TSource> SingleAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  120. {
  121. if (source == null)
  122. throw Error.ArgumentNull(nameof(source));
  123. if (predicate == null)
  124. throw Error.ArgumentNull(nameof(predicate));
  125. return Core(source, predicate, cancellationToken);
  126. static async Task<TSource> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  127. {
  128. #if USE_AWAIT_USING
  129. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  130. {
  131. while (await e.MoveNextAsync())
  132. {
  133. var result = e.Current;
  134. if (await _predicate(result).ConfigureAwait(false))
  135. {
  136. while (await e.MoveNextAsync())
  137. {
  138. if (await _predicate(e.Current).ConfigureAwait(false))
  139. {
  140. throw Error.MoreThanOneElement();
  141. }
  142. }
  143. return result;
  144. }
  145. }
  146. }
  147. #else
  148. var e = _source.GetAsyncEnumerator(_cancellationToken);
  149. try
  150. {
  151. while (await e.MoveNextAsync().ConfigureAwait(false))
  152. {
  153. var result = e.Current;
  154. if (await _predicate(result).ConfigureAwait(false))
  155. {
  156. while (await e.MoveNextAsync().ConfigureAwait(false))
  157. {
  158. if (await _predicate(e.Current).ConfigureAwait(false))
  159. {
  160. throw Error.MoreThanOneElement();
  161. }
  162. }
  163. return result;
  164. }
  165. }
  166. }
  167. finally
  168. {
  169. await e.DisposeAsync().ConfigureAwait(false);
  170. }
  171. #endif
  172. throw Error.NoElements();
  173. }
  174. }
  175. #if !NO_DEEP_CANCELLATION
  176. public static Task<TSource> SingleAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  177. {
  178. if (source == null)
  179. throw Error.ArgumentNull(nameof(source));
  180. if (predicate == null)
  181. throw Error.ArgumentNull(nameof(predicate));
  182. return Core(source, predicate, cancellationToken);
  183. static async Task<TSource> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  184. {
  185. #if USE_AWAIT_USING
  186. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  187. {
  188. while (await e.MoveNextAsync())
  189. {
  190. var result = e.Current;
  191. if (await _predicate(result, _cancellationToken).ConfigureAwait(false))
  192. {
  193. while (await e.MoveNextAsync())
  194. {
  195. if (await _predicate(e.Current, _cancellationToken).ConfigureAwait(false))
  196. {
  197. throw Error.MoreThanOneElement();
  198. }
  199. }
  200. return result;
  201. }
  202. }
  203. }
  204. #else
  205. var e = _source.GetAsyncEnumerator(_cancellationToken);
  206. try
  207. {
  208. while (await e.MoveNextAsync().ConfigureAwait(false))
  209. {
  210. var result = e.Current;
  211. if (await _predicate(result, _cancellationToken).ConfigureAwait(false))
  212. {
  213. while (await e.MoveNextAsync().ConfigureAwait(false))
  214. {
  215. if (await _predicate(e.Current, _cancellationToken).ConfigureAwait(false))
  216. {
  217. throw Error.MoreThanOneElement();
  218. }
  219. }
  220. return result;
  221. }
  222. }
  223. }
  224. finally
  225. {
  226. await e.DisposeAsync().ConfigureAwait(false);
  227. }
  228. #endif
  229. throw Error.NoElements();
  230. }
  231. }
  232. #endif
  233. }
  234. }