ToList.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using Cysharp.Threading.Tasks.Internal;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. namespace Cysharp.Threading.Tasks.Linq
  5. {
  6. public static partial class UniTaskAsyncEnumerable
  7. {
  8. public static UniTask<List<TSource>> ToListAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  9. {
  10. Error.ThrowArgumentNullException(source, nameof(source));
  11. return Cysharp.Threading.Tasks.Linq.ToList.ToListAsync(source, cancellationToken);
  12. }
  13. }
  14. internal static class ToList
  15. {
  16. internal static async UniTask<List<TSource>> ToListAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  17. {
  18. var list = new List<TSource>();
  19. var e = source.GetAsyncEnumerator(cancellationToken);
  20. try
  21. {
  22. while (await e.MoveNextAsync())
  23. {
  24. list.Add(e.Current);
  25. }
  26. }
  27. finally
  28. {
  29. if (e != null)
  30. {
  31. await e.DisposeAsync();
  32. }
  33. }
  34. return list;
  35. }
  36. }
  37. }