Skip.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using Cysharp.Threading.Tasks.Internal;
  2. using System;
  3. using System.Threading;
  4. namespace Cysharp.Threading.Tasks.Linq
  5. {
  6. public static partial class UniTaskAsyncEnumerable
  7. {
  8. public static IUniTaskAsyncEnumerable<TSource> Skip<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Int32 count)
  9. {
  10. Error.ThrowArgumentNullException(source, nameof(source));
  11. return new Skip<TSource>(source, count);
  12. }
  13. }
  14. internal sealed class Skip<TSource> : IUniTaskAsyncEnumerable<TSource>
  15. {
  16. readonly IUniTaskAsyncEnumerable<TSource> source;
  17. readonly int count;
  18. public Skip(IUniTaskAsyncEnumerable<TSource> source, int count)
  19. {
  20. this.source = source;
  21. this.count = count;
  22. }
  23. public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  24. {
  25. return new _Skip(source, count, cancellationToken);
  26. }
  27. sealed class _Skip : AsyncEnumeratorBase<TSource, TSource>
  28. {
  29. readonly int count;
  30. int index;
  31. public _Skip(IUniTaskAsyncEnumerable<TSource> source, int count, CancellationToken cancellationToken)
  32. : base(source, cancellationToken)
  33. {
  34. this.count = count;
  35. }
  36. protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
  37. {
  38. if (sourceHasCurrent)
  39. {
  40. if (count <= checked(index++))
  41. {
  42. Current = SourceCurrent;
  43. result = true;
  44. return true;
  45. }
  46. else
  47. {
  48. result = default;
  49. return false;
  50. }
  51. }
  52. else
  53. {
  54. result = false;
  55. return true;
  56. }
  57. }
  58. }
  59. }
  60. }