Cast.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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<TResult> Cast<TResult>(this IUniTaskAsyncEnumerable<Object> source)
  9. {
  10. Error.ThrowArgumentNullException(source, nameof(source));
  11. return new Cast<TResult>(source);
  12. }
  13. }
  14. internal sealed class Cast<TResult> : IUniTaskAsyncEnumerable<TResult>
  15. {
  16. readonly IUniTaskAsyncEnumerable<object> source;
  17. public Cast(IUniTaskAsyncEnumerable<object> source)
  18. {
  19. this.source = source;
  20. }
  21. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  22. {
  23. return new _Cast(source, cancellationToken);
  24. }
  25. class _Cast : AsyncEnumeratorBase<object, TResult>
  26. {
  27. public _Cast(IUniTaskAsyncEnumerable<object> source, CancellationToken cancellationToken)
  28. : base(source, cancellationToken)
  29. {
  30. }
  31. protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
  32. {
  33. if (sourceHasCurrent)
  34. {
  35. Current = (TResult)SourceCurrent;
  36. result = true;
  37. return true;
  38. }
  39. result = false;
  40. return true;
  41. }
  42. }
  43. }
  44. }