Skip to content

Commit 6cf33cf

Browse files
Merge pull request #326 from atc-net/feature/ToAsyncEnumerable
Feature/ToAsyncEnumerable
2 parents fadc252 + 61d75ee commit 6cf33cf

File tree

8 files changed

+97
-3
lines changed

8 files changed

+97
-3
lines changed

Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@
4343
<ItemGroup Label="Code Analyzers">
4444
<PackageReference Include="AsyncFixer" Version="1.6.0" PrivateAssets="All" />
4545
<PackageReference Include="Asyncify" Version="0.9.7" PrivateAssets="All" />
46-
<PackageReference Include="Meziantou.Analyzer" Version="2.0.160" PrivateAssets="All" />
46+
<PackageReference Include="Meziantou.Analyzer" Version="2.0.161" PrivateAssets="All" />
4747
<PackageReference Include="SecurityCodeScan.VS2019" Version="5.6.7" PrivateAssets="All" />
4848
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435" PrivateAssets="All" />
49-
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.29.0.95321" PrivateAssets="All" />
49+
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.30.0.95878" PrivateAssets="All" />
5050
</ItemGroup>
5151

5252
</Project>

docs/CodeDoc/Atc/Index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222

223223
## [System.Collections.Generic](System.Collections.Generic.md)
224224

225+
- [EnumerableExtensions](System.Collections.Generic.md#enumerableextensions)
225226
- [ReadOnlyListExtensions](System.Collections.Generic.md#readonlylistextensions)
226227

227228
## [System.ComponentModel.DataAnnotations](System.ComponentModel.DataAnnotations.md)

docs/CodeDoc/Atc/IndexExtended.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5284,6 +5284,9 @@
52845284

52855285
## [System.Collections.Generic](System.Collections.Generic.md)
52865286

5287+
- [EnumerableExtensions](System.Collections.Generic.md#enumerableextensions)
5288+
- Static Methods
5289+
- ToAsyncEnumerable(this IEnumerable&lt;T&gt; source, CancellationToken cancellationToken = null)
52875290
- [ReadOnlyListExtensions](System.Collections.Generic.md#readonlylistextensions)
52885291
- Static Methods
52895292
- GetPowerSet(this IReadOnlyList&lt;T&gt; list)

docs/CodeDoc/Atc/System.Collections.Generic.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,29 @@
77

88
<br />
99

10+
## EnumerableExtensions
11+
Provides extension methods for asynchronous enumeration of collections.
12+
13+
>```csharp
14+
>public static class EnumerableExtensions
15+
>```
16+
17+
### Static Methods
18+
19+
#### ToAsyncEnumerable
20+
>```csharp
21+
>IAsyncEnumerable<T> ToAsyncEnumerable(this IEnumerable<T> source, CancellationToken cancellationToken = null)
22+
>```
23+
><b>Summary:</b> Converts an `System.Collections.Generic.IEnumerable`1` to an `System.Collections.Generic.IAsyncEnumerable`1`.
24+
>
25+
><b>Parameters:</b><br>
26+
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`source`&nbsp;&nbsp;-&nbsp;&nbsp;The source sequence to convert.<br />
27+
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`cancellationToken`&nbsp;&nbsp;-&nbsp;&nbsp;A to observe while waiting for the asynchronous operation to complete.<br />
28+
>
29+
><b>Returns:</b> An `System.Collections.Generic.IAsyncEnumerable`1` that contains the elements from the input sequence.
30+
31+
<br />
32+
1033
## ReadOnlyListExtensions
1134
1235
>```csharp

src/Atc.XUnit/Atc.XUnit.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</PropertyGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="EPPlus" Version="7.2.1" />
15+
<PackageReference Include="EPPlus" Version="7.2.2" />
1616
<PackageReference Include="ICSharpCode.Decompiler" Version="8.2.0.7535" />
1717
<PackageReference Include="Mono.Reflection" Version="2.0.0">
1818
<NoWarn>NU1701</NoWarn>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// ReSharper disable once CheckNamespace
2+
namespace System.Collections.Generic;
3+
4+
/// <summary>
5+
/// Provides extension methods for asynchronous enumeration of collections.
6+
/// </summary>
7+
public static class EnumerableExtensions
8+
{
9+
/// <summary>
10+
/// Converts an <see cref="IEnumerable{T}"/> to an <see cref="IAsyncEnumerable{T}"/>.
11+
/// </summary>
12+
/// <typeparam name="T">The type of the elements in the source sequence.</typeparam>
13+
/// <param name="source">The source sequence to convert.</param>
14+
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the asynchronous operation to complete.</param>
15+
/// <returns>An <see cref="IAsyncEnumerable{T}"/> that contains the elements from the input sequence.</returns>
16+
/// <exception cref="ArgumentNullException">Thrown when the source sequence is null.</exception>
17+
[SuppressMessage("Design", "MA0050:Validate arguments correctly in iterator methods", Justification = "OK - False/Positive")]
18+
public static async IAsyncEnumerable<T> ToAsyncEnumerable<T>(
19+
this IEnumerable<T> source,
20+
[EnumeratorCancellation] CancellationToken cancellationToken = default)
21+
{
22+
if (source is null)
23+
{
24+
throw new ArgumentNullException(nameof(source));
25+
}
26+
27+
await foreach (var item in IterateAsync(source, cancellationToken).ConfigureAwait(false))
28+
{
29+
yield return item;
30+
}
31+
}
32+
33+
private static async IAsyncEnumerable<T> IterateAsync<T>(
34+
IEnumerable<T> source,
35+
[EnumeratorCancellation] CancellationToken cancellationToken)
36+
{
37+
foreach (var item in source)
38+
{
39+
cancellationToken.ThrowIfCancellationRequested();
40+
yield return item;
41+
await Task.Yield();
42+
}
43+
}
44+
}

test/Atc.Tests/CodeComplianceTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class CodeComplianceTests
4040
typeof(StackTraceHelper),
4141

4242
// UnitTests are made, but CodeCompliance test cannot detect this
43+
typeof(EnumerableExtensions),
4344
typeof(EnumAtcExtensions),
4445
typeof(DynamicJson),
4546
typeof(EnumHelper),
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// ReSharper disable PossibleMultipleEnumeration
2+
namespace Atc.Tests.Extensions;
3+
4+
public class EnumerableExtensionsTests
5+
{
6+
[Fact]
7+
public async Task ToAsyncEnumerable()
8+
{
9+
// Arrange
10+
var source = Enumerable.Range(1, 5);
11+
12+
// Act
13+
var result = new List<int>();
14+
await foreach (var item in source.ToAsyncEnumerable())
15+
{
16+
result.Add(item);
17+
}
18+
19+
// Assert
20+
Assert.Equal(source, result);
21+
}
22+
}

0 commit comments

Comments
 (0)