Skip to content

Commit e7186a4

Browse files
committed
Add AWAIT_USING_REQUIRES_IASYNCDISPOSABLE to work around issue in .NET Core 3.0 Preview 2.
1 parent e3080a1 commit e7186a4

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

Ix.NET/Source/System.Linq.Async/System/Threading/Tasks/AsyncEnumerableExtensions.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System.Collections.Generic;
66
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
78

89
namespace System.Threading.Tasks
910
{
@@ -31,12 +32,66 @@ public static ConfiguredCancelableAsyncEnumerable<T> WithCancellation<T>(
3132

3233
#endif
3334

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
3488
public static ConfiguredCancelableAsyncEnumerable<T>.Enumerator ConfigureAwait<T>(this IAsyncEnumerator<T> enumerator, bool continueOnCapturedContext)
3589
{
3690
if (enumerator == null)
3791
throw Error.ArgumentNull(nameof(enumerator));
3892

3993
return new ConfiguredCancelableAsyncEnumerable<T>.Enumerator(enumerator, continueOnCapturedContext);
4094
}
95+
#endif
4196
}
4297
}

0 commit comments

Comments
 (0)