Skip to content
Next Next commit
Annotate ConfiguredCancelableAsyncEnumerable T with allows ref struct…
… and update extensions
  • Loading branch information
nietras committed Jan 29, 2025
commit d421945e65c04e66d4d77554361f9b7be8c16256
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace System.Runtime.CompilerServices
/// <summary>Provides an awaitable async enumerable that enables cancelable iteration and configured awaits.</summary>
[StructLayout(LayoutKind.Auto)]
public readonly struct ConfiguredCancelableAsyncEnumerable<T>
#if NET9_0_OR_GREATER
Comment thread
nietras marked this conversation as resolved.
Outdated
where T : allows ref struct
#endif
{
private readonly IAsyncEnumerable<T> _enumerable;
private readonly CancellationToken _cancellationToken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@ public static ConfiguredAsyncDisposable ConfigureAwait(this IAsyncDisposable sou
/// <param name="continueOnCapturedContext"><see langword="true" /> to capture and marshal back to the current context; otherwise, <see langword="false" />.</param>
/// <returns>The configured enumerable.</returns>
public static ConfiguredCancelableAsyncEnumerable<T> ConfigureAwait<T>(
this IAsyncEnumerable<T> source, bool continueOnCapturedContext) =>
new ConfiguredCancelableAsyncEnumerable<T>(source, continueOnCapturedContext, cancellationToken: default);
this IAsyncEnumerable<T> source, bool continueOnCapturedContext)
#if NET9_0_OR_GREATER
where T : allows ref struct
#endif
=> new ConfiguredCancelableAsyncEnumerable<T>(source, continueOnCapturedContext, cancellationToken: default);

/// <summary>Sets the <see cref="CancellationToken"/> to be passed to <see cref="IAsyncEnumerable{T}.GetAsyncEnumerator(CancellationToken)"/> when iterating.</summary>
/// <typeparam name="T">The type of the objects being iterated.</typeparam>
/// <param name="source">The source enumerable being iterated.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
/// <returns>The configured enumerable.</returns>
public static ConfiguredCancelableAsyncEnumerable<T> WithCancellation<T>(
this IAsyncEnumerable<T> source, CancellationToken cancellationToken) =>
new ConfiguredCancelableAsyncEnumerable<T>(source, continueOnCapturedContext: true, cancellationToken);
this IAsyncEnumerable<T> source, CancellationToken cancellationToken)
#if NET9_0_OR_GREATER
where T : allows ref struct
#endif
=> new ConfiguredCancelableAsyncEnumerable<T>(source, continueOnCapturedContext: true, cancellationToken);
}
}