/
GeekNerd
/
ServerModelingProject
Обзор
Документация
Войти
/
GeekNerd
/
ServerModelingProject
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Assets/Plugins/UniTask/Runtime/Linq/ElementAt.cs
58 строк
2 KB
Gorney-Alex
Init
30 май 2026, 19:05
30 май 2026, 19:05
3cdcc49
Код
Авторство
О чём код?
using Cysharp.Threading.Tasks.Internal; using System; using System.Threading; namespace Cysharp.Threading.Tasks.Linq { public static partial class UniTaskAsyncEnumerable { public static UniTask<TSource> ElementAtAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken = default) { Error.ThrowArgumentNullException(source, nameof(source)); return ElementAt.ElementAtAsync(source, index, cancellationToken, false); } public static UniTask<TSource> ElementAtOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken = default) { Error.ThrowArgumentNullException(source, nameof(source)); return ElementAt.ElementAtAsync(source, index, cancellationToken, true); } } internal static class ElementAt { public static async UniTask<TSource> ElementAtAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken, bool defaultIfEmpty) { var e = source.GetAsyncEnumerator(cancellationToken); try { int i = 0; while (await e.MoveNextAsync()) { if (i++ == index) { return e.Current; } } if (defaultIfEmpty) { return default; } else { throw Error.ArgumentOutOfRange(nameof(index)); } } finally { if (e != null) { await e.DisposeAsync(); } } } } }