Skip to content

Commit aeb6598

Browse files
committed
Added async singleton collection
1 parent 7c97196 commit aeb6598

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/DotNext.Tests/Collections/Generic/AsyncEnumerableTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,11 @@ public static async Task SkipNullsTestAsync()
9797
True(Array.Exists(array, "a".Equals));
9898
True(Array.Exists(array, "b".Equals));
9999
}
100+
101+
[Fact]
102+
public static void Singleton()
103+
{
104+
var enumerable = AsyncEnumerable.Singleton(42);
105+
Equal(42, Single(enumerable));
106+
}
100107
}

src/DotNext/Collections/Generic/AsyncEnumerable.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,13 @@ public static IAsyncEnumerable<T> Throw<T>(Exception e)
219219

220220
return new ThrowingEnumerator<T>(e);
221221
}
222+
223+
/// <summary>
224+
/// Constructs read-only sequence with a single item in it.
225+
/// </summary>
226+
/// <param name="item">An item to be placed into list.</param>
227+
/// <typeparam name="T">Type of list items.</typeparam>
228+
/// <returns>Read-only list containing single item.</returns>
229+
public static IAsyncEnumerable<T> Singleton<T>(T item)
230+
=> new Specialized.SingletonList<T> { Item = item };
222231
}

src/DotNext/Collections/Specialized/SingletonList.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace DotNext.Collections.Specialized;
1212
/// </summary>
1313
/// <typeparam name="T">The type of the element in the list.</typeparam>
1414
[StructLayout(LayoutKind.Auto)]
15-
public struct SingletonList<T> : IReadOnlyList<T>, IList<T>, ITuple, IReadOnlySet<T>
15+
public struct SingletonList<T> : IReadOnlyList<T>, IList<T>, ITuple, IReadOnlySet<T>, IAsyncEnumerable<T>
1616
{
1717
/// <summary>
1818
/// Represents an enumerator over the collection containing a single element.
@@ -145,6 +145,10 @@ readonly IEnumerator<T> IEnumerable<T>.GetEnumerator()
145145
readonly IEnumerator IEnumerable.GetEnumerator()
146146
=> GetEnumerator().ToClassicEnumerator<Enumerator, T>();
147147

148+
/// <inheritdoc />
149+
readonly IAsyncEnumerator<T> IAsyncEnumerable<T>.GetAsyncEnumerator(CancellationToken token)
150+
=> GetEnumerator().ToAsyncEnumerator<Enumerator, T>(token);
151+
148152
/// <summary>
149153
/// Converts a value to the read-only list.
150154
/// </summary>

0 commit comments

Comments
 (0)