mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
更新Demo
更新Demo
This commit is contained in:
58
UnityProject/Packages/UniTask/Runtime/Linq/ElementAt.cs
Normal file
58
UnityProject/Packages/UniTask/Runtime/Linq/ElementAt.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user