-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #326 from atc-net/feature/ToAsyncEnumerable
Feature/ToAsyncEnumerable
- Loading branch information
Showing
8 changed files
with
97 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// ReSharper disable once CheckNamespace | ||
namespace System.Collections.Generic; | ||
|
||
/// <summary> | ||
/// Provides extension methods for asynchronous enumeration of collections. | ||
/// </summary> | ||
public static class EnumerableExtensions | ||
{ | ||
/// <summary> | ||
/// Converts an <see cref="IEnumerable{T}"/> to an <see cref="IAsyncEnumerable{T}"/>. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the elements in the source sequence.</typeparam> | ||
/// <param name="source">The source sequence to convert.</param> | ||
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the asynchronous operation to complete.</param> | ||
/// <returns>An <see cref="IAsyncEnumerable{T}"/> that contains the elements from the input sequence.</returns> | ||
/// <exception cref="ArgumentNullException">Thrown when the source sequence is null.</exception> | ||
[SuppressMessage("Design", "MA0050:Validate arguments correctly in iterator methods", Justification = "OK - False/Positive")] | ||
public static async IAsyncEnumerable<T> ToAsyncEnumerable<T>( | ||
this IEnumerable<T> source, | ||
[EnumeratorCancellation] CancellationToken cancellationToken = default) | ||
{ | ||
if (source is null) | ||
{ | ||
throw new ArgumentNullException(nameof(source)); | ||
} | ||
|
||
await foreach (var item in IterateAsync(source, cancellationToken).ConfigureAwait(false)) | ||
{ | ||
yield return item; | ||
} | ||
} | ||
|
||
private static async IAsyncEnumerable<T> IterateAsync<T>( | ||
IEnumerable<T> source, | ||
[EnumeratorCancellation] CancellationToken cancellationToken) | ||
{ | ||
foreach (var item in source) | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
yield return item; | ||
await Task.Yield(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// ReSharper disable PossibleMultipleEnumeration | ||
namespace Atc.Tests.Extensions; | ||
|
||
public class EnumerableExtensionsTests | ||
{ | ||
[Fact] | ||
public async Task ToAsyncEnumerable() | ||
{ | ||
// Arrange | ||
var source = Enumerable.Range(1, 5); | ||
|
||
// Act | ||
var result = new List<int>(); | ||
await foreach (var item in source.ToAsyncEnumerable()) | ||
{ | ||
result.Add(item); | ||
} | ||
|
||
// Assert | ||
Assert.Equal(source, result); | ||
} | ||
} |