From 3e00735b3d20e3a70b39e24844aa205e8ed0e590 Mon Sep 17 00:00:00 2001 From: neuecc Date: Mon, 15 Jun 2020 15:23:25 +0900 Subject: [PATCH] Add AsUniTaskAsyncEnumerable/AsAsyncEnumerable in .NET Core --- .../NetCore/AsyncEnumerableExtensions.cs | 101 ++++++++++++++++++ .../NetCore/UniTask.AsValueTask.cs | 9 +- 2 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 src/UniTask.NetCore/NetCore/AsyncEnumerableExtensions.cs diff --git a/src/UniTask.NetCore/NetCore/AsyncEnumerableExtensions.cs b/src/UniTask.NetCore/NetCore/AsyncEnumerableExtensions.cs new file mode 100644 index 0000000..33e9674 --- /dev/null +++ b/src/UniTask.NetCore/NetCore/AsyncEnumerableExtensions.cs @@ -0,0 +1,101 @@ +#if !NETSTANDARD2_0 + +#pragma warning disable 0649 + +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using System.Threading.Tasks.Sources; + +namespace Cysharp.Threading.Tasks +{ + public static class AsyncEnumerableExtensions + { + public static IUniTaskAsyncEnumerable AsUniTaskAsyncEnumerable(this IAsyncEnumerable source) + { + return new AsyncEnumerableToUniTaskAsyncEnumerable(source); + } + + public static IAsyncEnumerable AsAsyncEnumerable(this IUniTaskAsyncEnumerable source) + { + return new UniTaskAsyncEnumerableToAsyncEnumerable(source); + } + + sealed class AsyncEnumerableToUniTaskAsyncEnumerable : IUniTaskAsyncEnumerable + { + readonly IAsyncEnumerable source; + + public AsyncEnumerableToUniTaskAsyncEnumerable(IAsyncEnumerable source) + { + this.source = source; + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new Enumerator(source.GetAsyncEnumerator(cancellationToken)); + } + + sealed class Enumerator : IUniTaskAsyncEnumerator + { + readonly IAsyncEnumerator enumerator; + + public Enumerator(IAsyncEnumerator enumerator) + { + this.enumerator = enumerator; + } + + public T Current => enumerator.Current; + + public async UniTask DisposeAsync() + { + await enumerator.DisposeAsync(); + } + + public async UniTask MoveNextAsync() + { + return await enumerator.MoveNextAsync(); + } + } + } + + sealed class UniTaskAsyncEnumerableToAsyncEnumerable : IAsyncEnumerable + { + readonly IUniTaskAsyncEnumerable source; + + public UniTaskAsyncEnumerableToAsyncEnumerable(IUniTaskAsyncEnumerable source) + { + this.source = source; + } + + public IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + return new Enumerator(source.GetAsyncEnumerator(cancellationToken)); + } + + sealed class Enumerator : IAsyncEnumerator + { + readonly IUniTaskAsyncEnumerator enumerator; + + public Enumerator(IUniTaskAsyncEnumerator enumerator) + { + this.enumerator = enumerator; + } + + public T Current => enumerator.Current; + + public ValueTask DisposeAsync() + { + return enumerator.DisposeAsync(); + } + + public ValueTask MoveNextAsync() + { + return enumerator.MoveNextAsync(); + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/UniTask.NetCore/NetCore/UniTask.AsValueTask.cs b/src/UniTask.NetCore/NetCore/UniTask.AsValueTask.cs index 103d129..c3c4b7a 100644 --- a/src/UniTask.NetCore/NetCore/UniTask.AsValueTask.cs +++ b/src/UniTask.NetCore/NetCore/UniTask.AsValueTask.cs @@ -26,15 +26,14 @@ namespace Cysharp.Threading.Tasks #endif } - public static UniTask AsUniTask(this ValueTask task, bool useCurrentSynchronizationContext = true) + public static async UniTask AsUniTask(this ValueTask task) { - // NOTE: get _obj and _token directly for low overhead conversion but not yet implemented. - return task.AsTask().AsUniTask(useCurrentSynchronizationContext); + return await task; } - public static UniTask AsUniTask(this ValueTask task, bool useCurrentSynchronizationContext = true) + public static async UniTask AsUniTask(this ValueTask task) { - return task.AsTask().AsUniTask(useCurrentSynchronizationContext); + await task; } #if NETSTANDARD2_0