AsyncEnumerableExtensions.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #if BCL_HAS_CONFIGUREAWAIT
  30. public static ConfiguredAsyncEnumerator<T> ConfigureAwait<T>(this IAsyncEnumerator<T> enumerator, bool continueOnCapturedContext)
  31. {
  32. if (enumerator == null)
  33. throw Error.ArgumentNull(nameof(enumerator));
  34. // NB: We need our own copy of the struct to access the constructor.
  35. return new ConfiguredAsyncEnumerator<T>(enumerator, continueOnCapturedContext);
  36. }
  37. /// <summary>Provides an awaitable async enumerator that enables cancelable iteration and configured awaits.</summary>
  38. [StructLayout(LayoutKind.Auto)]
  39. public readonly struct ConfiguredAsyncEnumerator<T>
  40. {
  41. private readonly IAsyncEnumerator<T> _enumerator;
  42. private readonly bool _continueOnCapturedContext;
  43. internal ConfiguredAsyncEnumerator(IAsyncEnumerator<T> enumerator, bool continueOnCapturedContext)
  44. {
  45. _enumerator = enumerator;
  46. _continueOnCapturedContext = continueOnCapturedContext;
  47. }
  48. /// <summary>Advances the enumerator asynchronously to the next element of the collection.</summary>
  49. /// <returns>
  50. /// A <see cref="ConfiguredValueTaskAwaitable{Boolean}"/> that will complete with a result of <c>true</c>
  51. /// if the enumerator was successfully advanced to the next element, or <c>false</c> if the enumerator has
  52. /// passed the end of the collection.
  53. /// </returns>
  54. public ConfiguredValueTaskAwaitable<bool> MoveNextAsync() =>
  55. _enumerator.MoveNextAsync().ConfigureAwait(_continueOnCapturedContext);
  56. /// <summary>Gets the element in the collection at the current position of the enumerator.</summary>
  57. public T Current => _enumerator.Current;
  58. /// <summary>
  59. /// Performs application-defined tasks associated with freeing, releasing, or
  60. /// resetting unmanaged resources asynchronously.
  61. /// </summary>
  62. public ConfiguredValueTaskAwaitable DisposeAsync() =>
  63. _enumerator.DisposeAsync().ConfigureAwait(_continueOnCapturedContext);
  64. }
  65. #else
  66. public static ConfiguredCancelableAsyncEnumerable<T>.Enumerator ConfigureAwait<T>(this IAsyncEnumerator<T> enumerator, bool continueOnCapturedContext)
  67. {
  68. if (enumerator == null)
  69. throw Error.ArgumentNull(nameof(enumerator));
  70. return new ConfiguredCancelableAsyncEnumerable<T>.Enumerator(enumerator, continueOnCapturedContext);
  71. }
  72. #endif
  73. }
  74. }