Single.cs 6.0 KB

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