|
4 | 4 |
|
5 | 5 | using System.Collections.Generic; |
6 | 6 | using System.Runtime.CompilerServices; |
| 7 | +using System.Runtime.InteropServices; |
7 | 8 |
|
8 | 9 | namespace System.Threading.Tasks |
9 | 10 | { |
@@ -31,12 +32,66 @@ public static ConfiguredCancelableAsyncEnumerable<T> WithCancellation<T>( |
31 | 32 |
|
32 | 33 | #endif |
33 | 34 |
|
| 35 | + // |
| 36 | + // REVIEW: `await using (var e = xs.GetAsyncEnumerator().ConfigureAwait(false)) { ... }` leads to the following error when using BCL types. |
| 37 | + // |
| 38 | + // error CS8410: 'ConfiguredCancelableAsyncEnumerable<TSource>.Enumerator': type used in an async using statement must be implicitly convertible to 'System.IAsyncDisposable' |
| 39 | + // |
| 40 | + // See https://github.com/dotnet/csharplang/blob/master/meetings/2019/LDM-2019-01-16.md#pattern-based-disposal-in-await-foreach for the issues with |
| 41 | + // `await foreach` (but not `await using`). This should be reviewed with the LDM. Also see https://github.com/dotnet/csharplang/issues/1623. |
| 42 | + // |
| 43 | +#if BCL_HAS_CONFIGUREAWAIT && AWAIT_USING_REQUIRES_IASYNCDISPOSABLE |
| 44 | + public static ConfiguredAsyncEnumerator<T> ConfigureAwait<T>(this IAsyncEnumerator<T> enumerator, bool continueOnCapturedContext) |
| 45 | + { |
| 46 | + if (enumerator == null) |
| 47 | + throw Error.ArgumentNull(nameof(enumerator)); |
| 48 | + |
| 49 | + return new ConfiguredAsyncEnumerator<T>(enumerator, continueOnCapturedContext); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary>Provides an awaitable async enumerator that enables cancelable iteration and configured awaits.</summary> |
| 53 | + [StructLayout(LayoutKind.Auto)] |
| 54 | + public readonly struct ConfiguredAsyncEnumerator<T> : IAsyncDisposable |
| 55 | + { |
| 56 | + private readonly IAsyncEnumerator<T> _enumerator; |
| 57 | + private readonly bool _continueOnCapturedContext; |
| 58 | + |
| 59 | + internal ConfiguredAsyncEnumerator(IAsyncEnumerator<T> enumerator, bool continueOnCapturedContext) |
| 60 | + { |
| 61 | + _enumerator = enumerator; |
| 62 | + _continueOnCapturedContext = continueOnCapturedContext; |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary>Advances the enumerator asynchronously to the next element of the collection.</summary> |
| 66 | + /// <returns> |
| 67 | + /// A <see cref="ConfiguredValueTaskAwaitable{Boolean}"/> that will complete with a result of <c>true</c> |
| 68 | + /// if the enumerator was successfully advanced to the next element, or <c>false</c> if the enumerator has |
| 69 | + /// passed the end of the collection. |
| 70 | + /// </returns> |
| 71 | + public ConfiguredValueTaskAwaitable<bool> MoveNextAsync() => |
| 72 | + _enumerator.MoveNextAsync().ConfigureAwait(_continueOnCapturedContext); |
| 73 | + |
| 74 | + /// <summary>Gets the element in the collection at the current position of the enumerator.</summary> |
| 75 | + public T Current => _enumerator.Current; |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Performs application-defined tasks associated with freeing, releasing, or |
| 79 | + /// resetting unmanaged resources asynchronously. |
| 80 | + /// </summary> |
| 81 | + public ConfiguredValueTaskAwaitable DisposeAsync() => |
| 82 | + _enumerator.DisposeAsync().ConfigureAwait(_continueOnCapturedContext); |
| 83 | + |
| 84 | + async ValueTask IAsyncDisposable.DisposeAsync() => |
| 85 | + await _enumerator.DisposeAsync().ConfigureAwait(_continueOnCapturedContext); |
| 86 | + } |
| 87 | +#else |
34 | 88 | public static ConfiguredCancelableAsyncEnumerable<T>.Enumerator ConfigureAwait<T>(this IAsyncEnumerator<T> enumerator, bool continueOnCapturedContext) |
35 | 89 | { |
36 | 90 | if (enumerator == null) |
37 | 91 | throw Error.ArgumentNull(nameof(enumerator)); |
38 | 92 |
|
39 | 93 | return new ConfiguredCancelableAsyncEnumerable<T>.Enumerator(enumerator, continueOnCapturedContext); |
40 | 94 | } |
| 95 | +#endif |
41 | 96 | } |
42 | 97 | } |
0 commit comments