-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumerableExtensions.cs
More file actions
64 lines (55 loc) · 2.57 KB
/
EnumerableExtensions.cs
File metadata and controls
64 lines (55 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
namespace Ramstack.FileSystem.Utilities;
/// <summary>
/// Provides extension methods for the <see cref="IEnumerable{T}"/>.
/// </summary>
internal static class EnumerableExtensions
{
/// <summary>
/// Converts an enumerable sequence to an async-enumerable sequence.
/// </summary>
/// <typeparam name="T">The type of the elements in the source sequence.</typeparam>
/// <param name="source">Th enumerable sequence to convert to an async-enumerable sequence.</param>
/// <returns>
/// The async-enumerable sequence whose elements are pulled from the given enumerable sequence.
/// </returns>
public static IAsyncEnumerable<T> ToAsyncEnumerable<T>(this IEnumerable<T> source) =>
new AsyncEnumerableAdapter<T>(source);
#region Inner type: AsyncEnumerableAdapter
/// <summary>
/// Wraps the <see cref="IEnumerable{T}"/> to the <see cref="IAsyncEnumerable{T}"/>.
/// </summary>
/// <typeparam name="T">The type of the elements in the collection.</typeparam>
/// <param name="source">The <see cref="IEnumerable{T}"/> to wrap.</param>
private sealed class AsyncEnumerableAdapter<T>(IEnumerable<T> source) : IAsyncEnumerable<T>
{
/// <inheritdoc cref="IAsyncEnumerable{T}.GetAsyncEnumerator(CancellationToken)" />
public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken) =>
new AsyncEnumeratorAdapter<T>(source.GetEnumerator(), cancellationToken);
}
#endregion
#region Inner type: AsyncEnumeratorAdapter
/// <summary>
/// Wraps the <see cref="IEnumerator{T}"/> to the <see cref="IAsyncEnumerator{T}"/>.
/// </summary>
/// <typeparam name="T">The type of the elements in the collection.</typeparam>
/// <param name="enumerator">The <see cref="IEnumerator{T}"/> to wrap.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
private sealed class AsyncEnumeratorAdapter<T>(IEnumerator<T> enumerator, CancellationToken cancellationToken) : IAsyncEnumerator<T>
{
/// <inheritdoc />
public T Current => enumerator.Current;
/// <inheritdoc />
public ValueTask<bool> MoveNextAsync()
{
var result = !cancellationToken.IsCancellationRequested && enumerator.MoveNext();
return new ValueTask<bool>(result);
}
/// <inheritdoc />
public ValueTask DisposeAsync()
{
enumerator.Dispose();
return default;
}
}
#endregion
}