using Cysharp.Threading.Tasks.Internal; using System; using System.Threading; namespace Cysharp.Threading.Tasks.Linq { public static partial class UniTaskAsyncEnumerable { public static IUniTaskAsyncEnumerable OfType(this IUniTaskAsyncEnumerable source) { Error.ThrowArgumentNullException(source, nameof(source)); return new OfType(source); } } internal sealed class OfType : IUniTaskAsyncEnumerable { readonly IUniTaskAsyncEnumerable source; public OfType(IUniTaskAsyncEnumerable source) { this.source = source; } public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) { return new _OfType(source, cancellationToken); } class _OfType : AsyncEnumeratorBase { public _OfType(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) : base(source, cancellationToken) { } protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result) { if (sourceHasCurrent) { if (SourceCurrent is TResult castCurent) { Current = castCurent; result = true; return true; } else { result = default; return false; } } result = false; return true; } } } }