Single.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 ValueTask<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 ValueTask<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. await using (var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false))
  28. {
  29. if (!await e.MoveNextAsync())
  30. {
  31. throw Error.NoElements();
  32. }
  33. var result = e.Current;
  34. if (await e.MoveNextAsync())
  35. {
  36. throw Error.MoreThanOneElement();
  37. }
  38. return result;
  39. }
  40. }
  41. }
  42. public static ValueTask<TSource> SingleAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
  43. {
  44. if (source == null)
  45. throw Error.ArgumentNull(nameof(source));
  46. if (predicate == null)
  47. throw Error.ArgumentNull(nameof(predicate));
  48. return Core(source, predicate, cancellationToken);
  49. static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> _source, Func<TSource, bool> _predicate, CancellationToken _cancellationToken)
  50. {
  51. await using (var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false))
  52. {
  53. while (await e.MoveNextAsync())
  54. {
  55. var result = e.Current;
  56. if (_predicate(result))
  57. {
  58. while (await e.MoveNextAsync())
  59. {
  60. if (_predicate(e.Current))
  61. {
  62. throw Error.MoreThanOneElement();
  63. }
  64. }
  65. return result;
  66. }
  67. }
  68. }
  69. throw Error.NoElements();
  70. }
  71. }
  72. public static ValueTask<TSource> SingleAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  73. {
  74. if (source == null)
  75. throw Error.ArgumentNull(nameof(source));
  76. if (predicate == null)
  77. throw Error.ArgumentNull(nameof(predicate));
  78. return Core(source, predicate, cancellationToken);
  79. static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  80. {
  81. await using (var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false))
  82. {
  83. while (await e.MoveNextAsync())
  84. {
  85. var result = e.Current;
  86. if (await _predicate(result).ConfigureAwait(false))
  87. {
  88. while (await e.MoveNextAsync())
  89. {
  90. if (await _predicate(e.Current).ConfigureAwait(false))
  91. {
  92. throw Error.MoreThanOneElement();
  93. }
  94. }
  95. return result;
  96. }
  97. }
  98. }
  99. throw Error.NoElements();
  100. }
  101. }
  102. #if !NO_DEEP_CANCELLATION
  103. public static ValueTask<TSource> SingleAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  104. {
  105. if (source == null)
  106. throw Error.ArgumentNull(nameof(source));
  107. if (predicate == null)
  108. throw Error.ArgumentNull(nameof(predicate));
  109. return Core(source, predicate, cancellationToken);
  110. static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  111. {
  112. await using (var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false))
  113. {
  114. while (await e.MoveNextAsync())
  115. {
  116. var result = e.Current;
  117. if (await _predicate(result, _cancellationToken).ConfigureAwait(false))
  118. {
  119. while (await e.MoveNextAsync())
  120. {
  121. if (await _predicate(e.Current, _cancellationToken).ConfigureAwait(false))
  122. {
  123. throw Error.MoreThanOneElement();
  124. }
  125. }
  126. return result;
  127. }
  128. }
  129. }
  130. throw Error.NoElements();
  131. }
  132. }
  133. #endif
  134. }
  135. }