1
0

AsyncEnumerableExtensions.cs 5.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. namespace System.Threading.Tasks
  8. {
  9. public static class AsyncEnumerableExtensions
  10. {
  11. #if !BCL_HAS_CONFIGUREAWAIT // https://github.com/dotnet/coreclr/pull/21939
  12. /// <summary>Configures how awaits on the tasks returned from an async iteration will be performed.</summary>
  13. /// <typeparam name="T">The type of the objects being iterated.</typeparam>
  14. /// <param name="source">The source enumerable being iterated.</param>
  15. /// <param name="continueOnCapturedContext">Whether to capture and marshal back to the current context.</param>
  16. /// <returns>The configured enumerable.</returns>
  17. public static ConfiguredCancelableAsyncEnumerable<T> ConfigureAwait<T>(
  18. this IAsyncEnumerable<T> source, bool continueOnCapturedContext) =>
  19. new ConfiguredCancelableAsyncEnumerable<T>(source, continueOnCapturedContext, cancellationToken: default);
  20. /// <summary>Sets the <see cref="CancellationToken"/> to be passed to <see cref="IAsyncEnumerable{T}.GetAsyncEnumerator(CancellationToken)"/> when iterating.</summary>
  21. /// <typeparam name="T">The type of the objects being iterated.</typeparam>
  22. /// <param name="source">The source enumerable being iterated.</param>
  23. /// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
  24. /// <returns>The configured enumerable.</returns>
  25. public static ConfiguredCancelableAsyncEnumerable<T> WithCancellation<T>(
  26. this IAsyncEnumerable<T> source, CancellationToken cancellationToken) =>
  27. new ConfiguredCancelableAsyncEnumerable<T>(source, continueOnCapturedContext: true, cancellationToken);
  28. #endif
  29. //
  30. // REVIEW: `await using (var e = xs.GetAsyncEnumerator().ConfigureAwait(false)) { ... }` leads to the following error when using BCL types.
  31. //
  32. // error CS8410: 'ConfiguredCancelableAsyncEnumerable<TSource>.Enumerator': type used in an async using statement must be implicitly convertible to 'System.IAsyncDisposable'
  33. //
  34. // 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
  35. // `await foreach` (but not `await using`). This should be reviewed with the LDM. Also see https://github.com/dotnet/csharplang/issues/1623.
  36. //
  37. #if BCL_HAS_CONFIGUREAWAIT && AWAIT_USING_REQUIRES_IASYNCDISPOSABLE
  38. public static ConfiguredAsyncEnumerator<T> ConfigureAwait<T>(this IAsyncEnumerator<T> enumerator, bool continueOnCapturedContext)
  39. {
  40. if (enumerator == null)
  41. throw Error.ArgumentNull(nameof(enumerator));
  42. return new ConfiguredAsyncEnumerator<T>(enumerator, continueOnCapturedContext);
  43. }
  44. /// <summary>Provides an awaitable async enumerator that enables cancelable iteration and configured awaits.</summary>
  45. [StructLayout(LayoutKind.Auto)]
  46. public readonly struct ConfiguredAsyncEnumerator<T> : IAsyncDisposable
  47. {
  48. private readonly IAsyncEnumerator<T> _enumerator;
  49. private readonly bool _continueOnCapturedContext;
  50. internal ConfiguredAsyncEnumerator(IAsyncEnumerator<T> enumerator, bool continueOnCapturedContext)
  51. {
  52. _enumerator = enumerator;
  53. _continueOnCapturedContext = continueOnCapturedContext;
  54. }
  55. /// <summary>Advances the enumerator asynchronously to the next element of the collection.</summary>
  56. /// <returns>
  57. /// A <see cref="ConfiguredValueTaskAwaitable{Boolean}"/> that will complete with a result of <c>true</c>
  58. /// if the enumerator was successfully advanced to the next element, or <c>false</c> if the enumerator has
  59. /// passed the end of the collection.
  60. /// </returns>
  61. public ConfiguredValueTaskAwaitable<bool> MoveNextAsync() =>
  62. _enumerator.MoveNextAsync().ConfigureAwait(_continueOnCapturedContext);
  63. /// <summary>Gets the element in the collection at the current position of the enumerator.</summary>
  64. public T Current => _enumerator.Current;
  65. /// <summary>
  66. /// Performs application-defined tasks associated with freeing, releasing, or
  67. /// resetting unmanaged resources asynchronously.
  68. /// </summary>
  69. public ConfiguredValueTaskAwaitable DisposeAsync() =>
  70. _enumerator.DisposeAsync().ConfigureAwait(_continueOnCapturedContext);
  71. async ValueTask IAsyncDisposable.DisposeAsync() =>
  72. await _enumerator.DisposeAsync().ConfigureAwait(_continueOnCapturedContext);
  73. }
  74. #else
  75. public static ConfiguredCancelableAsyncEnumerable<T>.Enumerator ConfigureAwait<T>(this IAsyncEnumerator<T> enumerator, bool continueOnCapturedContext)
  76. {
  77. if (enumerator == null)
  78. throw Error.ArgumentNull(nameof(enumerator));
  79. return new ConfiguredCancelableAsyncEnumerable<T>.Enumerator(enumerator, continueOnCapturedContext);
  80. }
  81. #endif
  82. }
  83. }