using Cysharp.Threading.Tasks.Internal; using System; using System.Threading; namespace Cysharp.Threading.Tasks.Linq { public static partial class UniTaskAsyncEnumerable { public static UniTask SingleAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) { Error.ThrowArgumentNullException(source, nameof(source)); return SingleOperator.SingleAsync(source, cancellationToken, false); } public static UniTask SingleAsync(this IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken = default) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(predicate, nameof(predicate)); return SingleOperator.SingleAsync(source, predicate, cancellationToken, false); } public static UniTask SingleAwaitAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(predicate, nameof(predicate)); return SingleOperator.SingleAwaitAsync(source, predicate, cancellationToken, false); } public static UniTask SingleAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(predicate, nameof(predicate)); return SingleOperator.SingleAwaitWithCancellationAsync(source, predicate, cancellationToken, false); } public static UniTask SingleOrDefaultAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) { Error.ThrowArgumentNullException(source, nameof(source)); return SingleOperator.SingleAsync(source, cancellationToken, true); } public static UniTask SingleOrDefaultAsync(this IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken = default) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(predicate, nameof(predicate)); return SingleOperator.SingleAsync(source, predicate, cancellationToken, true); } public static UniTask SingleOrDefaultAwaitAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(predicate, nameof(predicate)); return SingleOperator.SingleAwaitAsync(source, predicate, cancellationToken, true); } public static UniTask SingleOrDefaultAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(predicate, nameof(predicate)); return SingleOperator.SingleAwaitWithCancellationAsync(source, predicate, cancellationToken, true); } } internal static class SingleOperator { public static async UniTask SingleAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken, bool defaultIfEmpty) { var e = source.GetAsyncEnumerator(cancellationToken); try { if (await e.MoveNextAsync()) { var v = e.Current; if (!await e.MoveNextAsync()) { return v; } throw Error.MoreThanOneElement(); } else { if (defaultIfEmpty) { return default; } else { throw Error.NoElements(); } } } finally { if (e != null) { await e.DisposeAsync(); } } } public static async UniTask SingleAsync(IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken, bool defaultIfEmpty) { var e = source.GetAsyncEnumerator(cancellationToken); try { TSource value = default; bool found = false; while (await e.MoveNextAsync()) { var v = e.Current; if (predicate(v)) { if (found) { throw Error.MoreThanOneElement(); } else { found = true; value = v; } } } if (found || defaultIfEmpty) { return value; } throw Error.NoElements(); } finally { if (e != null) { await e.DisposeAsync(); } } } public static async UniTask SingleAwaitAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken, bool defaultIfEmpty) { var e = source.GetAsyncEnumerator(cancellationToken); try { TSource value = default; bool found = false; while (await e.MoveNextAsync()) { var v = e.Current; if (await predicate(v)) { if (found) { throw Error.MoreThanOneElement(); } else { found = true; value = v; } } } if (found || defaultIfEmpty) { return value; } throw Error.NoElements(); } finally { if (e != null) { await e.DisposeAsync(); } } } public static async UniTask SingleAwaitWithCancellationAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken, bool defaultIfEmpty) { var e = source.GetAsyncEnumerator(cancellationToken); try { TSource value = default; bool found = false; while (await e.MoveNextAsync()) { var v = e.Current; if (await predicate(v, cancellationToken)) { if (found) { throw Error.MoreThanOneElement(); } else { found = true; value = v; } } } if (found || defaultIfEmpty) { return value; } throw Error.NoElements(); } finally { if (e != null) { await e.DisposeAsync(); } } } } }