// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information. using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace System.Linq { public static partial class AsyncEnumerable { public static ValueTask SingleAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); static async ValueTask Core(IAsyncEnumerable _source, CancellationToken _cancellationToken) { if (_source is IList list) { switch (list.Count) { case 0: throw Error.NoElements(); case 1: return list[0]; } throw Error.MoreThanOneElement(); } var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false); try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } var result = e.Current; if (await e.MoveNextAsync()) { throw Error.MoreThanOneElement(); } return result; } finally { await e.DisposeAsync(); } } } public static ValueTask SingleAsync(this IAsyncEnumerable source, Func predicate, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (predicate == null) throw Error.ArgumentNull(nameof(predicate)); return Core(source, predicate, cancellationToken); static async ValueTask Core(IAsyncEnumerable _source, Func _predicate, CancellationToken _cancellationToken) { var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false); try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731) { while (await e.MoveNextAsync()) { var result = e.Current; if (_predicate(result)) { while (await e.MoveNextAsync()) { if (_predicate(e.Current)) { throw Error.MoreThanOneElement(); } } return result; } } } finally { await e.DisposeAsync(); } throw Error.NoElements(); } } public static ValueTask SingleAsync(this IAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (predicate == null) throw Error.ArgumentNull(nameof(predicate)); return Core(source, predicate, cancellationToken); static async ValueTask Core(IAsyncEnumerable _source, Func> _predicate, CancellationToken _cancellationToken) { var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false); try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731) { while (await e.MoveNextAsync()) { var result = e.Current; if (await _predicate(result).ConfigureAwait(false)) { while (await e.MoveNextAsync()) { if (await _predicate(e.Current).ConfigureAwait(false)) { throw Error.MoreThanOneElement(); } } return result; } } } finally { await e.DisposeAsync(); } throw Error.NoElements(); } } #if !NO_DEEP_CANCELLATION public static ValueTask SingleAsync(this IAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (predicate == null) throw Error.ArgumentNull(nameof(predicate)); return Core(source, predicate, cancellationToken); static async ValueTask Core(IAsyncEnumerable _source, Func> _predicate, CancellationToken _cancellationToken) { var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false); try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731) { while (await e.MoveNextAsync()) { var result = e.Current; if (await _predicate(result, _cancellationToken).ConfigureAwait(false)) { while (await e.MoveNextAsync()) { if (await _predicate(e.Current, _cancellationToken).ConfigureAwait(false)) { throw Error.MoreThanOneElement(); } } return result; } } } finally { await e.DisposeAsync(); } throw Error.NoElements(); } } #endif } }