|
| 1 | +// Copyright (C) DataStax Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#if NET8_0_OR_GREATER && !NET10_OR_GREATER // These methods are implemented in .NET 10. |
| 16 | +using System.Collections.Generic; |
| 17 | +using System.Threading; |
| 18 | +using System.Threading.Tasks; |
| 19 | + |
| 20 | +// ReSharper disable once CheckNamespace |
| 21 | +namespace System.Linq; |
| 22 | + |
| 23 | +internal static class AsyncEnumerable |
| 24 | +{ |
| 25 | + public static async ValueTask<TSource> FirstAsync<TSource>( |
| 26 | + this IAsyncEnumerable<TSource> source, |
| 27 | + CancellationToken cancellationToken = default) |
| 28 | + { |
| 29 | + await using var e = source.GetAsyncEnumerator(cancellationToken); |
| 30 | + |
| 31 | + if (!await e.MoveNextAsync()) |
| 32 | + { |
| 33 | + throw new InvalidOperationException("Sequence contains no elements"); |
| 34 | + } |
| 35 | + |
| 36 | + return e.Current; |
| 37 | + } |
| 38 | + |
| 39 | + public static async ValueTask<TSource> FirstOrDefaultAsync<TSource>( |
| 40 | + this IAsyncEnumerable<TSource> source, |
| 41 | + CancellationToken cancellationToken = default) |
| 42 | + { |
| 43 | + await using var e = source.GetAsyncEnumerator(cancellationToken); |
| 44 | + return await e.MoveNextAsync() ? e.Current : default; |
| 45 | + } |
| 46 | + |
| 47 | + public static async ValueTask<TSource> SingleAsync<TSource>( |
| 48 | + this IAsyncEnumerable<TSource> source, |
| 49 | + CancellationToken cancellationToken = default) |
| 50 | + { |
| 51 | + await using var e = source.GetAsyncEnumerator(cancellationToken); |
| 52 | + |
| 53 | + if (!await e.MoveNextAsync()) |
| 54 | + { |
| 55 | + throw new InvalidOperationException("Sequence contains no elements"); |
| 56 | + } |
| 57 | + |
| 58 | + TSource result = e.Current; |
| 59 | + if (await e.MoveNextAsync()) |
| 60 | + { |
| 61 | + throw new InvalidOperationException("Sequence contains more than one element"); |
| 62 | + } |
| 63 | + |
| 64 | + return result; |
| 65 | + } |
| 66 | + |
| 67 | + public static async ValueTask<TSource> SingleOrDefaultAsync<TSource>( |
| 68 | + this IAsyncEnumerable<TSource> source, |
| 69 | + CancellationToken cancellationToken = default) |
| 70 | + { |
| 71 | + await using var e = source.GetAsyncEnumerator(cancellationToken); |
| 72 | + |
| 73 | + if (!await e.MoveNextAsync()) |
| 74 | + { |
| 75 | + return default; |
| 76 | + } |
| 77 | + |
| 78 | + TSource result = e.Current; |
| 79 | + if (await e.MoveNextAsync()) |
| 80 | + { |
| 81 | + throw new InvalidOperationException("Sequence contains more than one element"); |
| 82 | + } |
| 83 | + |
| 84 | + return result; |
| 85 | + } |
| 86 | + |
| 87 | + public static async IAsyncEnumerable<TResult> Select<TSource, TResult>( |
| 88 | + this IAsyncEnumerable<TSource> source, |
| 89 | + Func<TSource, TResult> selector) |
| 90 | + { |
| 91 | + await foreach (TSource element in source) |
| 92 | + { |
| 93 | + yield return selector(element); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public static async ValueTask<List<TSource>> ToListAsync<TSource>( |
| 98 | + this IAsyncEnumerable<TSource> source, |
| 99 | + CancellationToken cancellationToken = default) |
| 100 | + { |
| 101 | + List<TSource> list = []; |
| 102 | + await foreach (TSource element in source.WithCancellation(cancellationToken)) |
| 103 | + { |
| 104 | + list.Add(element); |
| 105 | + } |
| 106 | + |
| 107 | + return list; |
| 108 | + } |
| 109 | +} |
| 110 | +#endif |
0 commit comments