From: Stephen Toub Date: Fri, 26 Oct 2018 04:19:26 +0000 (-0700) Subject: Add ConfigureAwait extension method for IAsyncEnumerable X-Git-Tag: submit/tizen/20210909.063632~11030^2~3600^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a7a1b9d0b7eba27b399a7f3ac4fbc6ea806c985e;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Add ConfigureAwait extension method for IAsyncEnumerable Commit migrated from https://github.com/dotnet/coreclr/commit/9e16ad51dd0f5e77b2dbaa6300dc2da1aafe21f3 --- diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index 52e9a5a..078444f 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -438,6 +438,7 @@ + diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/ConfiguredAsyncEnumerable.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/ConfiguredAsyncEnumerable.cs new file mode 100644 index 0000000..57fd481 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/ConfiguredAsyncEnumerable.cs @@ -0,0 +1,47 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Threading.Tasks; +using System.Threading.Tasks.Sources; + +namespace System.Runtime.CompilerServices +{ + [StructLayout(LayoutKind.Auto)] + public readonly struct ConfiguredAsyncEnumerable + { + private readonly IAsyncEnumerable _enumerable; + private readonly bool _continueOnCapturedContext; + + internal ConfiguredAsyncEnumerable(IAsyncEnumerable enumerable, bool continueOnCapturedContext) + { + _enumerable = enumerable; + _continueOnCapturedContext = continueOnCapturedContext; + } + + public Enumerator GetAsyncEnumerator() => + new Enumerator(_enumerable.GetAsyncEnumerator(), _continueOnCapturedContext); + + public readonly struct Enumerator + { + private readonly IAsyncEnumerator _enumerator; + private readonly bool _continueOnCapturedContext; + + internal Enumerator(IAsyncEnumerator enumerator, bool continueOnCapturedContext) + { + _enumerator = enumerator; + _continueOnCapturedContext = continueOnCapturedContext; + } + + public ConfiguredValueTaskAwaitable MoveNextAsync() => + _enumerator.MoveNextAsync().ConfigureAwait(_continueOnCapturedContext); + + public T Current => _enumerator.Current; + + public ConfiguredValueTaskAwaitable DisposeAsync() => + _enumerator.DisposeAsync().ConfigureAwait(_continueOnCapturedContext); + } + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/TaskExtensions.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/TaskExtensions.cs index 97f2013..a07033d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/TaskExtensions.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/TaskExtensions.cs @@ -2,6 +2,9 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Collections.Generic; +using System.Runtime.CompilerServices; + namespace System.Threading.Tasks { /// Provides a set of static methods for working with specific kinds of instances. @@ -44,5 +47,14 @@ namespace System.Threading.Tasks task.Result ?? Task.FromCanceled(new CancellationToken(true)); } + + /// Configures how awaits on the tasks returned from an async iteration will be performed. + /// The type of the objects being iterated. + /// The source enumerable being iterated. + /// Whether to capture and marshal back to the current context. + /// The configured enumerable. + public static ConfiguredAsyncEnumerable ConfigureAwait( + this IAsyncEnumerable source, bool continueOnCapturedContext) => + new ConfiguredAsyncEnumerable(source, continueOnCapturedContext); } }