AsyncEnumerableAdapter
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Threading;
namespace NUnit.Framework.Internal
{
internal static class AsyncEnumerableAdapter
{
private class AsyncEnumerableWrapper : IEnumerable<object>, IEnumerable
{
[Nullable(new byte[] {
1,
2
})]
private readonly IAsyncEnumerable<object> _asyncEnumerable;
public AsyncEnumerableWrapper([Nullable(new byte[] {
1,
2
})] IAsyncEnumerable<object> asyncEnumerable)
{
_asyncEnumerable = asyncEnumerable;
}
[return: Nullable(new byte[] {
1,
2
})]
public IEnumerator<object> GetEnumerator()
{
return new AsyncEnumeratorWrapper(_asyncEnumerable.GetAsyncEnumerator(default(CancellationToken)));
}
[NullableContext(1)]
IEnumerator IEnumerable.GetEnumerator()
{
return new AsyncEnumeratorWrapper(_asyncEnumerable.GetAsyncEnumerator(default(CancellationToken)));
}
}
private class AsyncEnumeratorWrapper : IEnumerator<object>, IEnumerator, IDisposable
{
[Nullable(new byte[] {
1,
2
})]
private readonly IAsyncEnumerator<object> _asyncEnumerator;
[Nullable(2)]
public object Current {
[NullableContext(2)]
get {
return _asyncEnumerator.Current;
}
}
public AsyncEnumeratorWrapper([Nullable(new byte[] {
1,
2
})] IAsyncEnumerator<object> asyncEnumerator)
{
_asyncEnumerator = asyncEnumerator;
}
public void Dispose()
{
AsyncToSyncAdapter.Await(null, () => _asyncEnumerator.DisposeAsync());
}
public bool MoveNext()
{
return AsyncToSyncAdapter.Await<bool>(null, () => _asyncEnumerator.MoveNextAsync());
}
public void Reset()
{
throw new InvalidOperationException("Can not reset an async enumerable.");
}
}
[NullableContext(2)]
public static IEnumerable CoalesceToEnumerable(object enumerable)
{
if (enumerable == null)
return null;
IEnumerable enumerable2 = enumerable as IEnumerable;
if (enumerable2 != null)
return enumerable2;
if (TryGetAsyncBlockingEnumerable(enumerable, out IEnumerable<object> result))
return result;
throw new ArgumentException("Argument can not be converted to an IEnumerable.", "enumerable");
}
[NullableContext(1)]
private static bool TryGetAsyncBlockingEnumerable(object enumerable, [Nullable(2)] [NotNullWhen(true)] out IEnumerable<object> result)
{
IAsyncEnumerable<object> asyncEnumerable = enumerable as IAsyncEnumerable<object>;
if (asyncEnumerable != null) {
result = new AsyncEnumerableWrapper(asyncEnumerable);
return true;
}
result = null;
return false;
}
}
}