pull/73/head
neuecc 2020-05-10 23:26:25 +09:00
parent 3f082f1923
commit 5db5beab29
3 changed files with 114 additions and 782 deletions

View File

@ -1,775 +1,76 @@
namespace Cysharp.Threading.Tasks.Linq using Cysharp.Threading.Tasks.Internal;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Cysharp.Threading.Tasks.Linq
{ {
internal sealed class Reverse public static partial class UniTaskAsyncEnumerable
{ {
public static IUniTaskAsyncEnumerable<TSource> Reverse<TSource>(this IUniTaskAsyncEnumerable<TSource> source)
{
Error.ThrowArgumentNullException(source, nameof(source));
return new Reverse<TSource>(source);
}
} }
internal sealed class Reverse<TSource> : IUniTaskAsyncEnumerable<TSource>
} {
readonly IUniTaskAsyncEnumerable<TSource> source;
public Reverse(IUniTaskAsyncEnumerable<TSource> source)
{
this.source = source;
}
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
return new Enumerator(source, cancellationToken);
}
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
{
readonly IUniTaskAsyncEnumerable<TSource> source;
CancellationToken cancellationToken;
TSource[] array;
int index;
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
{
this.source = source;
this.cancellationToken = cancellationToken;
}
public TSource Current { get; private set; }
// after consumed array, don't use await so allow async(not require UniTaskCompletionSourceCore).
public async UniTask<bool> MoveNextAsync()
{
cancellationToken.ThrowIfCancellationRequested();
if (array == null)
{
array = await source.ToArrayAsync(cancellationToken);
index = array.Length - 1;
}
if (index != -1)
{
Current = array[index];
--index;
return true;
}
else
{
return false;
}
}
public UniTask DisposeAsync()
{
return default;
}
}
}
}

View File

@ -39,17 +39,8 @@ namespace NetCoreSandbox
static async Task Main(string[] args) static async Task Main(string[] args)
{ {
Console.WriteLine("YEAH"); var foo = Enumerable.Range(1, 10).ToArray().AsEnumerable().GetEnumerator() as IEnumerator<int>;
try Console.WriteLine(foo.GetType().FullName);
{
var xs = await UniTaskAsyncEnumerable.Range(1, 10).Concat(UniTaskAsyncEnumerable.Throw<int>(new InvalidOperationException("something")))
.SumAsync();
}
catch (Exception ex)
{
Console.WriteLine("EX");
Console.WriteLine(ex);
}
} }

View File

@ -0,0 +1,40 @@
using Cysharp.Threading.Tasks;
using Cysharp.Threading.Tasks.Linq;
using FluentAssertions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Xunit;
namespace NetCoreTests.Linq
{
public class Projection
{
[Theory]
[InlineData(0, 0)]
[InlineData(0, 1)]
[InlineData(0, 2)]
[InlineData(0, 10)]
public async Task Reverse(int start, int count)
{
var xs = await Enumerable.Range(start, count).ToUniTaskAsyncEnumerable().Reverse().ToArrayAsync();
var ys = Enumerable.Range(start, count).Reverse().ToArray();
xs.Should().BeEquivalentTo(ys);
}
[Fact]
public async Task ReverseException()
{
foreach (var item in UniTaskTestException.Throws())
{
var xs = item.Reverse().ToArrayAsync();
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
}
}
}
}