Average, MinMax, and others

master
neuecc 2020-05-09 15:33:46 +09:00
parent c16433e0fe
commit d4511c0f67
20 changed files with 8208 additions and 1459 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,237 @@
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
<#
var types = new[]
{
(typeof(int), "double"),
(typeof(long), "double"),
(typeof(float),"float"),
(typeof(double),"double"),
(typeof(decimal),"decimal"),
(typeof(int?),"double?"),
(typeof(long?),"double?"),
(typeof(float?),"float?"),
(typeof(double?),"double?"),
(typeof(decimal?),"decimal?"),
};
Func<Type, bool> IsNullable = x => x.IsGenericType;
Func<Type, Type> ElementType = x => IsNullable(x) ? x.GetGenericArguments()[0] : x;
Func<Type, string> TypeName = x => IsNullable(x) ? x.GetGenericArguments()[0].Name + "?" : x.Name;
Func<Type, string> WithSuffix = x => IsNullable(x) ? ".GetValueOrDefault()" : "";
Func<Type, string> CalcResult = x => { var e = ElementType(x); return (e == typeof(int) || e == typeof(long)) ? "(double)sum / count" : (e == typeof(float)) ? "(float)(sum / count)" : "sum / count"; };
#>
using System;
using System.Threading;
using Cysharp.Threading.Tasks.Internal;
namespace Cysharp.Threading.Tasks.Linq
{
public static partial class UniTaskAsyncEnumerable
{
<# foreach(var (t, ret) in types) { #>
public static UniTask<<#= ret #>> AverageAsync(this IUniTaskAsyncEnumerable<<#= TypeName(t) #>> source, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
return Average.InvokeAsync(source, cancellationToken);
}
public static UniTask<<#= ret #>> AverageAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, <#= TypeName(t) #>> selector, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(source, nameof(selector));
return Average.InvokeAsync(source, selector, cancellationToken);
}
public static UniTask<<#= ret #>> AverageAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(source, nameof(selector));
return Average.InvokeAsync(source, selector, cancellationToken);
}
public static UniTask<<#= ret #>> AverageAwaitCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(source, nameof(selector));
return Average.InvokeAsync(source, selector, cancellationToken);
}
<# } #>
}
internal static class Average
{
<# foreach(var (t, ret) in types) { #>
public static async UniTask<<#= ret #>> InvokeAsync(IUniTaskAsyncEnumerable<<#= TypeName(t) #>> source, CancellationToken cancellationToken)
{
long count = 0;
<#= TypeName(t) #> sum = 0;
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
while (await e.MoveNextAsync())
{
<# if (IsNullable(t)) { #>
var v = e.Current;
if (v.HasValue)
{
checked
{
sum += v.Value;
count++;
}
}
<# } else { #>
checked
{
sum += e.Current;
count++;
}
<# } #>
}
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
return <#= CalcResult(t) #>;
}
public static async UniTask<<#= ret #>> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, <#= TypeName(t) #>> selector, CancellationToken cancellationToken)
{
long count = 0;
<#= TypeName(t) #> sum = 0;
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
while (await e.MoveNextAsync())
{
<# if (IsNullable(t)) { #>
var v = selector(e.Current);
if (v.HasValue)
{
checked
{
sum += v.Value;
count++;
}
}
<# } else { #>
checked
{
sum += selector(e.Current);
count++;
}
<# } #>
}
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
return <#= CalcResult(t) #>;
}
public static async UniTask<<#= ret #>> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken)
{
long count = 0;
<#= TypeName(t) #> sum = 0;
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
while (await e.MoveNextAsync())
{
<# if (IsNullable(t)) { #>
var v = await selector(e.Current);
if (v.HasValue)
{
checked
{
sum += v.Value;
count++;
}
}
<# } else { #>
checked
{
sum += await selector(e.Current);
count++;
}
<# } #>
}
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
return <#= CalcResult(t) #>;
}
public static async UniTask<<#= ret #>> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken)
{
long count = 0;
<#= TypeName(t) #> sum = 0;
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
while (await e.MoveNextAsync())
{
<# if (IsNullable(t)) { #>
var v = await selector(e.Current, cancellationToken);
if (v.HasValue)
{
checked
{
sum += v.Value;
count++;
}
}
<# } else { #>
checked
{
sum += await selector(e.Current, cancellationToken);
count++;
}
<# } #>
}
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
return <#= CalcResult(t) #>;
}
<# } #>
}
}

View File

@ -1,6 +1,6 @@
namespace Cysharp.Threading.Tasks.Linq namespace Cysharp.Threading.Tasks.Linq
{ {
internal sealed class Max internal static partial class Max
{ {
} }

View File

@ -1,6 +1,6 @@
namespace Cysharp.Threading.Tasks.Linq namespace Cysharp.Threading.Tasks.Linq
{ {
internal sealed class Min internal static partial class Min
{ {
} }

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,261 @@
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
<#
var types = new[]
{
typeof(int),
typeof(long),
typeof(float),
typeof(double),
typeof(decimal),
typeof(int?),
typeof(long?),
typeof(float?),
typeof(double?),
typeof(decimal?),
};
Func<Type, bool> IsNullable = x => x.IsGenericType;
Func<Type, string> TypeName = x => IsNullable(x) ? x.GetGenericArguments()[0].Name + "?" : x.Name;
Func<Type, string> WithSuffix = x => IsNullable(x) ? ".GetValueOrDefault()" : "";
#>
using System;
using System.Threading;
using Cysharp.Threading.Tasks.Internal;
namespace Cysharp.Threading.Tasks.Linq
{
public static partial class UniTaskAsyncEnumerable
{
<# foreach(var (minMax, op) in new[]{("Min","<"), ("Max", ">")}) { #>
<# foreach(var t in types) { #>
public static UniTask<<#= TypeName(t) #>> <#= minMax #>Async(this IUniTaskAsyncEnumerable<<#= TypeName(t) #>> source, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
return <#= minMax #>.InvokeAsync(source, cancellationToken);
}
public static UniTask<<#= TypeName(t) #>> <#= minMax #>Async<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, <#= TypeName(t) #>> selector, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(source, nameof(selector));
return <#= minMax #>.InvokeAsync(source, selector, cancellationToken);
}
public static UniTask<<#= TypeName(t) #>> <#= minMax #>AwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(source, nameof(selector));
return <#= minMax #>.InvokeAsync(source, selector, cancellationToken);
}
public static UniTask<<#= TypeName(t) #>> <#= minMax #>AwaitCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(source, nameof(selector));
return <#= minMax #>.InvokeAsync(source, selector, cancellationToken);
}
<# } #>
}
internal static partial class <#= minMax #>
{
<# foreach(var t in types) { #>
public static async UniTask<<#= TypeName(t) #>> InvokeAsync(IUniTaskAsyncEnumerable<<#= TypeName(t) #>> source, CancellationToken cancellationToken)
{
<#= TypeName(t) #> value = default;
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
while (await e.MoveNextAsync())
{
value = e.Current;
<# if (IsNullable(t)) { #>
if(value == null) continue;
<# } #>
goto NEXT_LOOP;
}
<# if (IsNullable(t)) { #>
return default;
<# } else { #>
throw Error.NoElements();
<# } #>
NEXT_LOOP:
while (await e.MoveNextAsync())
{
var x = e.Current;
<# if (IsNullable(t)) { #>
if( x == null) continue;
<# } #>
if (value <#= op #> x)
{
value = x;
}
}
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
return value;
}
public static async UniTask<<#= TypeName(t) #>> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, <#= TypeName(t) #>> selector, CancellationToken cancellationToken)
{
<#= TypeName(t) #> value = default;
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
while (await e.MoveNextAsync())
{
value = selector(e.Current);
<# if (IsNullable(t)) { #>
if(value == null) continue;
<# } #>
goto NEXT_LOOP;
}
<# if (IsNullable(t)) { #>
return default;
<# } else { #>
throw Error.NoElements();
<# } #>
NEXT_LOOP:
while (await e.MoveNextAsync())
{
var x = selector(e.Current);
<# if (IsNullable(t)) { #>
if( x == null) continue;
<# } #>
if (value <#= op #> x)
{
value = x;
}
}
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
return value;
}
public static async UniTask<<#= TypeName(t) #>> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken)
{
<#= TypeName(t) #> value = default;
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
while (await e.MoveNextAsync())
{
value = await selector(e.Current);
<# if (IsNullable(t)) { #>
if(value == null) continue;
<# } #>
goto NEXT_LOOP;
}
<# if (IsNullable(t)) { #>
return default;
<# } else { #>
throw Error.NoElements();
<# } #>
NEXT_LOOP:
while (await e.MoveNextAsync())
{
var x = await selector(e.Current);
<# if (IsNullable(t)) { #>
if( x == null) continue;
<# } #>
if (value <#= op #> x)
{
value = x;
}
}
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
return value;
}
public static async UniTask<<#= TypeName(t) #>> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken)
{
<#= TypeName(t) #> value = default;
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
while (await e.MoveNextAsync())
{
value = await selector(e.Current, cancellationToken);
<# if (IsNullable(t)) { #>
if(value == null) continue;
<# } #>
goto NEXT_LOOP;
}
<# if (IsNullable(t)) { #>
return default;
<# } else { #>
throw Error.NoElements();
<# } #>
NEXT_LOOP:
while (await e.MoveNextAsync())
{
var x = await selector(e.Current, cancellationToken);
<# if (IsNullable(t)) { #>
if( x == null) continue;
<# } #>
if (value <#= op #> x)
{
value = x;
}
}
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
return value;
}
<# } #>
<# } #>
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,775 @@
namespace Cysharp.Threading.Tasks.Linq
{
internal sealed class _Aggregate
{
}
}

View File

@ -111,205 +111,6 @@ namespace ___Dummy
throw new NotImplementedException(); throw new NotImplementedException();
} }
public static UniTask<Double> AverageAsync(this IUniTaskAsyncEnumerable<Int32> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Double> AverageAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Double> AverageAsync(this IUniTaskAsyncEnumerable<Int64> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Double> AverageAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int64> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Single> AverageAsync(this IUniTaskAsyncEnumerable<Single> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Single> AverageAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Single> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Double> AverageAsync(this IUniTaskAsyncEnumerable<Double> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Double> AverageAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Double> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Decimal> AverageAsync(this IUniTaskAsyncEnumerable<Decimal> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Decimal> AverageAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Decimal> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<double?> AverageAsync(this IUniTaskAsyncEnumerable<Nullable<Int32>> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<double?> AverageAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Nullable<Int32>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<double?> AverageAsync(this IUniTaskAsyncEnumerable<Nullable<Int64>> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<double?> AverageAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Nullable<Int64>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Single>> AverageAsync(this IUniTaskAsyncEnumerable<Nullable<Single>> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Single>> AverageAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Nullable<Single>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<double?> AverageAsync(this IUniTaskAsyncEnumerable<double?> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<double?> AverageAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Decimal>> AverageAsync(this IUniTaskAsyncEnumerable<Nullable<Decimal>> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Decimal>> AverageAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Nullable<Decimal>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Double> AverageAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Int32>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Double> AverageAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Int64>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Single> AverageAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Single>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Double> AverageAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Double>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Decimal> AverageAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Decimal>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<double?> AverageAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Nullable<Int32>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<double?> AverageAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Nullable<Int64>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Single>> AverageAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Nullable<Single>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<double?> AverageAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<double?>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Decimal>> AverageAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Nullable<Decimal>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Double> AverageAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Int32>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Double> AverageAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Int64>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Single> AverageAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Single>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Double> AverageAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Double>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Decimal> AverageAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Decimal>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<double?> AverageAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Nullable<Int32>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<double?> AverageAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Nullable<Int64>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Single>> AverageAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Nullable<Single>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<double?> AverageAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<double?>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Decimal>> AverageAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Nullable<Decimal>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> Cast<TResult>(this IUniTaskAsyncEnumerable<Object> source) public static IUniTaskAsyncEnumerable<TResult> Cast<TResult>(this IUniTaskAsyncEnumerable<Object> source)
{ {
@ -662,445 +463,6 @@ namespace ___Dummy
throw new NotImplementedException(); throw new NotImplementedException();
} }
public static UniTask<Int64> MaxAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int64> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Int64>> MaxAsync(this IUniTaskAsyncEnumerable<Nullable<Int64>> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Int64>> MaxAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Nullable<Int64>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Single> MaxAsync(this IUniTaskAsyncEnumerable<Single> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Single> MaxAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Single> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Single>> MaxAsync(this IUniTaskAsyncEnumerable<Nullable<Single>> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Single>> MaxAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Nullable<Single>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Double> MaxAsync(this IUniTaskAsyncEnumerable<Double> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Double> MaxAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Double> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<double?> MaxAsync(this IUniTaskAsyncEnumerable<double?> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<double?> MaxAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Decimal> MaxAsync(this IUniTaskAsyncEnumerable<Decimal> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Decimal> MaxAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Decimal> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Decimal>> MaxAsync(this IUniTaskAsyncEnumerable<Nullable<Decimal>> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Decimal>> MaxAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Nullable<Decimal>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> MaxAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TResult> MaxAsync<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Int32> MaxAsync(this IUniTaskAsyncEnumerable<Int32> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Int32> MaxAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Int32>> MaxAsync(this IUniTaskAsyncEnumerable<Nullable<Int32>> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Int32>> MaxAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Nullable<Int32>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Int64> MaxAsync(this IUniTaskAsyncEnumerable<Int64> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Int64> MaxAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Int64>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Int64>> MaxAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Nullable<Int64>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Single> MaxAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Single>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Single>> MaxAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Nullable<Single>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Double> MaxAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Double>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<double?> MaxAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<double?>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Decimal> MaxAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Decimal>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Decimal>> MaxAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Nullable<Decimal>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TResult> MaxAwaitAsync<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TResult>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Int32> MaxAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Int32>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Int32>> MaxAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Nullable<Int32>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Int64> MaxAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Int64>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Int64>> MaxAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Nullable<Int64>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Single> MaxAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Single>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Single>> MaxAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Nullable<Single>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Double> MaxAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Double>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<double?> MaxAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<double?>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Decimal> MaxAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Decimal>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Decimal>> MaxAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Nullable<Decimal>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TResult> MaxAwaitWithCancellationAsync<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TResult>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Int32> MaxAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Int32>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Int32>> MaxAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Nullable<Int32>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Double> MinAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Double> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<double?> MinAsync(this IUniTaskAsyncEnumerable<double?> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<double?> MinAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Decimal> MinAsync(this IUniTaskAsyncEnumerable<Decimal> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Decimal> MinAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Decimal> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Decimal>> MinAsync(this IUniTaskAsyncEnumerable<Nullable<Decimal>> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Decimal>> MinAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Nullable<Decimal>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Int32> MinAsync(this IUniTaskAsyncEnumerable<Int32> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Int32> MinAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Int32>> MinAsync(this IUniTaskAsyncEnumerable<Nullable<Int32>> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Int32>> MinAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Nullable<Int32>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Int64> MinAsync(this IUniTaskAsyncEnumerable<Int64> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Int64> MinAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int64> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Int64>> MinAsync(this IUniTaskAsyncEnumerable<Nullable<Int64>> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Int64>> MinAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Nullable<Int64>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Single> MinAsync(this IUniTaskAsyncEnumerable<Single> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Single> MinAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Single> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Single>> MinAsync(this IUniTaskAsyncEnumerable<Nullable<Single>> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Single>> MinAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Nullable<Single>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Double> MinAsync(this IUniTaskAsyncEnumerable<Double> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> MinAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TResult> MinAsync<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Int32>> MinAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Nullable<Int32>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Int64> MinAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Int64>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Int64>> MinAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Nullable<Int64>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Single> MinAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Single>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Single>> MinAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Nullable<Single>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TResult> MinAwaitAsync<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TResult>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Double> MinAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Double>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<double?> MinAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<double?>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Decimal> MinAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Decimal>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Decimal>> MinAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Nullable<Decimal>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Int32> MinAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Int32>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Single>> MinAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Nullable<Single>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TResult> MinAwaitWithCancellationAsync<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TResult>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Double> MinAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Double>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<double?> MinAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<double?>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Decimal> MinAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Decimal>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Decimal>> MinAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Nullable<Decimal>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Int32> MinAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Int32>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Int32>> MinAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Nullable<Int32>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Int64> MinAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Int64>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Nullable<Int64>> MinAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Nullable<Int64>>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Single> MinAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Single>> selector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> OfType<TResult>(this IUniTaskAsyncEnumerable<Object> source) public static IUniTaskAsyncEnumerable<TResult> OfType<TResult>(this IUniTaskAsyncEnumerable<Object> source)
{ {

View File

@ -0,0 +1,122 @@
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Threading.Tasks.Sources;
namespace Cysharp.Threading.Tasks
{
public static class UniTaskValueTaskExtensions
{
public static ValueTask AsValueTask(this UniTask task)
{
ref var core = ref Unsafe.As<UniTask, UniTaskToValueTask>(ref task);
if (core.source == null)
{
return default;
}
return new ValueTask(new UniTaskValueTaskSource(core.source), core.token);
}
public static ValueTask<T> AsValueTask<T>(this UniTask<T> task)
{
ref var core = ref Unsafe.As<UniTask<T>, UniTaskToValueTask<T>>(ref task);
if (core.source == null)
{
return new ValueTask<T>(core.result);
}
return new ValueTask<T>(new UniTaskValueTaskSource<T>(core.source), core.token);
}
struct UniTaskToValueTask
{
public IUniTaskSource source;
public short token;
}
class UniTaskValueTaskSource : IValueTaskSource
{
readonly IUniTaskSource source;
public UniTaskValueTaskSource(IUniTaskSource source)
{
this.source = source;
}
public void GetResult(short token)
{
source.GetResult(token);
}
public ValueTaskSourceStatus GetStatus(short token)
{
var status = source.GetStatus(token);
switch (status)
{
case UniTaskStatus.Pending:
return ValueTaskSourceStatus.Pending;
case UniTaskStatus.Succeeded:
return ValueTaskSourceStatus.Succeeded;
case UniTaskStatus.Faulted:
return ValueTaskSourceStatus.Faulted;
case UniTaskStatus.Canceled:
return ValueTaskSourceStatus.Canceled;
default:
return (ValueTaskSourceStatus)status;
}
}
public void OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags)
{
source.OnCompleted(continuation, state, token);
}
}
struct UniTaskToValueTask<T>
{
public IUniTaskSource<T> source;
public T result;
public short token;
}
class UniTaskValueTaskSource<T> : IValueTaskSource<T>
{
readonly IUniTaskSource<T> source;
public UniTaskValueTaskSource(IUniTaskSource<T> source)
{
this.source = source;
}
public T GetResult(short token)
{
return source.GetResult(token);
}
public ValueTaskSourceStatus GetStatus(short token)
{
var status = source.GetStatus(token);
switch (status)
{
case UniTaskStatus.Pending:
return ValueTaskSourceStatus.Pending;
case UniTaskStatus.Succeeded:
return ValueTaskSourceStatus.Succeeded;
case UniTaskStatus.Faulted:
return ValueTaskSourceStatus.Faulted;
case UniTaskStatus.Canceled:
return ValueTaskSourceStatus.Canceled;
default:
return (ValueTaskSourceStatus)status;
}
}
public void OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags)
{
source.OnCompleted(continuation, state, token);
}
}
}
}

View File

@ -0,0 +1,112 @@
using System;
using System.Threading;
namespace Cysharp.Threading.Tasks
{
public partial struct UniTask
{
/// <summary>Run action on the threadPool and return to current SynchronizationContext if configureAwait = true.</summary>
public static async UniTask Run(Action action, bool configureAwait = true)
{
if (configureAwait)
{
var current = SynchronizationContext.Current;
await UniTask.SwitchToThreadPool();
try
{
action();
}
finally
{
if (current != null)
{
await UniTask.SwitchToSynchronizationContext(current);
}
}
}
else
{
await UniTask.SwitchToThreadPool();
action();
}
}
/// <summary>Run action on the threadPool and return to current SynchronizationContext if configureAwait = true.</summary>
public static async UniTask Run(Action<object> action, object state, bool configureAwait = true)
{
if (configureAwait)
{
var current = SynchronizationContext.Current;
await UniTask.SwitchToThreadPool();
try
{
action(state);
}
finally
{
if (current != null)
{
await UniTask.SwitchToSynchronizationContext(current);
}
}
}
else
{
await UniTask.SwitchToThreadPool();
action(state);
}
}
/// <summary>Run action on the threadPool and return to current SynchronizationContext if configureAwait = true.</summary>
public static async UniTask<T> Run<T>(Func<T> func, bool configureAwait = true)
{
if (configureAwait)
{
var current = SynchronizationContext.Current;
await UniTask.SwitchToThreadPool();
try
{
return func();
}
finally
{
if (current != null)
{
await UniTask.SwitchToSynchronizationContext(current);
}
}
}
else
{
await UniTask.SwitchToThreadPool();
return func();
}
}
/// <summary>Run action on the threadPool and return to current SynchronizationContext if configureAwait = true.</summary>
public static async UniTask<T> Run<T>(Func<object, T> func, object state, bool configureAwait = true)
{
if (configureAwait)
{
var current = SynchronizationContext.Current;
await UniTask.SwitchToThreadPool();
try
{
return func(state);
}
finally
{
if (current != null)
{
await UniTask.SwitchToSynchronizationContext(current);
}
}
}
else
{
await UniTask.SwitchToThreadPool();
return func(state);
}
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Runtime.CompilerServices;
using System.Threading;
namespace Cysharp.Threading.Tasks
{
public partial struct UniTask
{
public static UniTask.YieldAwaitable Yield()
{
return default;
}
public readonly struct YieldAwaitable
{
public Awaiter GetAwaiter()
{
return default;
}
public readonly struct Awaiter : ICriticalNotifyCompletion
{
static readonly SendOrPostCallback SendOrPostCallbackDelegate = Continuation;
static readonly WaitCallback WaitCallbackDelegate = Continuation;
public bool IsCompleted => false;
public void GetResult() { }
public void OnCompleted(Action continuation)
{
UnsafeOnCompleted(continuation);
}
public void UnsafeOnCompleted(Action continuation)
{
var syncContext = SynchronizationContext.Current;
if (syncContext != null)
{
syncContext.Post(SendOrPostCallbackDelegate, continuation);
}
else
{
ThreadPool.UnsafeQueueUserWorkItem(WaitCallbackDelegate, continuation);
}
}
static void Continuation(object state)
{
((Action)state).Invoke();
}
}
}
}
}

View File

@ -7,34 +7,61 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="..\UniTask\Assets\Plugins\UniTask\**\*.cs" Exclude="..\UniTask\Assets\Plugins\UniTask\Triggers\*.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\Editor\*.cs;&#xD;&#xA; &#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\Internal\UnityEqualityComparer.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\Internal\DiagnosticsExtensions.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\Internal\PlayerLoopRunner.cs;&#xD;&#xA; &#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\CancellationTokenSourceExtensions.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\EnumeratorAsyncExtensions.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\PlayerLoopHelper.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\UniTask.Delay.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\UniTask.Run.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\UniTask.Bridge.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\UniTask.WaitUntil.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\UnityAsyncExtensions.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\UnityAsyncExtensions.uGUI.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\UnityAsyncExtensions.MonoBehaviour.cs;&#xD;&#xA; " /> <Compile Include="..\UniTask\Assets\Plugins\UniTask\**\*.cs" Exclude="..\UniTask\Assets\Plugins\UniTask\Triggers\*.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\Editor\*.cs;&#xD;&#xA; &#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\Internal\UnityEqualityComparer.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\Internal\DiagnosticsExtensions.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\Internal\PlayerLoopRunner.cs;&#xD;&#xA; &#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\CancellationTokenSourceExtensions.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\EnumeratorAsyncExtensions.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\PlayerLoopHelper.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\UniTask.Delay.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\UniTask.Run.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\UniTask.Bridge.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\UniTask.WaitUntil.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\UnityAsyncExtensions.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\UnityAsyncExtensions.uGUI.cs;&#xD;&#xA; ..\UniTask\Assets\Plugins\UniTask\UnityAsyncExtensions.MonoBehaviour.cs;&#xD;&#xA; " />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="Linq\Sum.cs"> <PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Sum.tt</DependentUpon>
</None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Update="Linq\Sum.tt"> <None Include="Linq\MinMax.cs">
<Generator>TextTemplatingFileGenerator</Generator> <DesignTime>True</DesignTime>
<LastGenOutput>Sum.cs</LastGenOutput> <AutoGen>True</AutoGen>
</None> <DependentUpon>MinMax.tt</DependentUpon>
</None>
<None Include="Linq\Sum.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Sum.tt</DependentUpon>
</None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" /> <None Update="Linq\Average.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>Average.cs</LastGenOutput>
</None>
<None Update="Linq\MinMax.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>MinMax.cs</LastGenOutput>
</None>
<None Update="Linq\Sum.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>Sum.cs</LastGenOutput>
</None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Update="Linq\Sum.cs"> <Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
<DesignTime>True</DesignTime> </ItemGroup>
<AutoGen>True</AutoGen>
<DependentUpon>Sum.tt</DependentUpon> <ItemGroup>
</Compile> <Compile Update="Linq\Average.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Average.tt</DependentUpon>
</Compile>
<Compile Update="Linq\MinMax.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>MinMax.tt</DependentUpon>
</Compile>
<Compile Update="Linq\Sum.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Sum.tt</DependentUpon>
</Compile>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -10,6 +10,8 @@ using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Cysharp.Threading.Tasks.Linq; using Cysharp.Threading.Tasks.Linq;
using System.Reactive.Linq;
using System.Reactive.Concurrency;
namespace NetCoreSandbox namespace NetCoreSandbox
{ {
@ -32,41 +34,18 @@ namespace NetCoreSandbox
static async Task Main(string[] args) static async Task Main(string[] args)
{ {
var xs = await UniTaskAsyncEnumerable.Range(0, 0).ToArrayAsync();
Console.WriteLine(xs);
var cts = new CancellationTokenSource();
// Enumerable.Sum(
//await foreach (var item in UniTaskAsyncEnumerable.Range(1, 3).WithCancellation(cts.Token))
//{
// Console.WriteLine(item);
// cts.Cancel();
//}
//AsyncEnumerable.SumAsync(
var a = await UniTaskAsyncEnumerable.Range(1, 100).SumAsync();
Console.WriteLine(a);
/*
.ForEachAsync(x =>
{
if (x == 2) throw new Exception();
Console.WriteLine(x);
});
*/
int? foo = null;
if (foo > 100)
{
Console.WriteLine("Under Foo");
}
else
{
Console.WriteLine("??");
}
} }

View File

@ -8,6 +8,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="System.Interactive.Async" Version="4.1.1" /> <PackageReference Include="System.Interactive.Async" Version="4.1.1" />
<PackageReference Include="System.Reactive" Version="4.4.1" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -0,0 +1,122 @@
using Cysharp.Threading.Tasks;
using Cysharp.Threading.Tasks.Linq;
using FluentAssertions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace NetCoreTests.Linq
{
public class Aggregate
{
[Theory]
[InlineData(0, 10)]
[InlineData(0, 1)]
[InlineData(10, 0)]
[InlineData(1, 11)]
public async Task Sum(int start, int count)
{
{
var xs = await UniTaskAsyncEnumerable.Range(start, count).SumAsync();
var ys = Enumerable.Range(start, count).Sum();
xs.Should().Be(ys);
}
{
var xs = await UniTaskAsyncEnumerable.Range(start, count).SumAsync(x => x * 2);
var ys = Enumerable.Range(start, count).Sum(x => x * 2);
xs.Should().Be(ys);
}
{
var xs = await UniTaskAsyncEnumerable.Range(start, count).SumAwaitAsync(x => UniTask.Run(() => x));
var ys = Enumerable.Range(start, count).Sum(x => x);
xs.Should().Be(ys);
}
{
var xs = await UniTaskAsyncEnumerable.Range(start, count).SumAwaitCancellationAsync((x, _) => UniTask.Run(() => x));
var ys = Enumerable.Range(start, count).Sum(x => x);
xs.Should().Be(ys);
}
}
public static IEnumerable<object[]> array1 = new object[][]
{
new object[]{new int[] { 1, 10, 100 } },
new object[]{new int?[] { 1, null, 100 } },
new object[]{new float[] { 1, 10, 100 } },
new object[]{new float?[] { 1, null, 100 } },
new object[]{new double[] { 1, 10, 100 } },
new object[]{new double?[] { 1, null, 100 } },
new object[]{new decimal[] { 1, 10, 100 } },
new object[]{new decimal?[] { 1, null, 100 } },
};
[Theory]
[MemberData(nameof(array1))]
public async Task Average<T>(T arr)
{
switch (arr)
{
case int[] array:
{
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
var ys = array.Average();
xs.Should().Be(ys);
}
break;
case int?[] array:
{
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
var ys = array.Average();
xs.Should().Be(ys);
}
break;
case float[] array:
{
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
var ys = array.Average();
xs.Should().Be(ys);
}
break;
case float?[] array:
{
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
var ys = array.Average();
xs.Should().Be(ys);
}
break;
case double[] array:
{
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
var ys = array.Average();
xs.Should().Be(ys);
}
break;
case double?[] array:
{
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
var ys = array.Average();
xs.Should().Be(ys);
}
break;
case decimal[] array:
{
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
var ys = array.Average();
xs.Should().Be(ys);
}
break;
case decimal?[] array:
{
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
var ys = array.Average();
xs.Should().Be(ys);
}
break;
default:
break;
}
}
}
}

View File

@ -0,0 +1,79 @@
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 Convert
{
[Fact]
public async Task ToAsyncEnumerable()
{
{
var xs = await Enumerable.Range(1, 100).ToUniTaskAsyncEnumerable().ToArrayAsync();
xs.Length.Should().Be(100);
}
{
var xs = await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().ToArrayAsync();
xs.Length.Should().Be(0);
}
}
[Fact]
public async Task ToAsyncEnumerableTask()
{
var t = Task.FromResult(100);
var xs = await t.ToUniTaskAsyncEnumerable().ToArrayAsync();
xs.Length.Should().Be(1);
xs[0].Should().Be(100);
}
[Fact]
public async Task ToAsyncEnumerableUniTask()
{
var t = UniTask.FromResult(100);
var xs = await t.ToUniTaskAsyncEnumerable().ToArrayAsync();
xs.Length.Should().Be(1);
xs[0].Should().Be(100);
}
[Fact]
public async Task ToAsyncEnumerableObservable()
{
{
var xs = await Observable.Range(1, 100).ToUniTaskAsyncEnumerable().ToArrayAsync();
var ys = await Observable.Range(1, 100).ToArray();
xs.Should().BeEquivalentTo(ys);
}
{
var xs = await Observable.Range(1, 100, ThreadPoolScheduler.Instance).ToUniTaskAsyncEnumerable().ToArrayAsync();
var ys = await Observable.Range(1, 100, ThreadPoolScheduler.Instance).ToArray();
xs.Should().BeEquivalentTo(ys);
}
{
var xs = await Observable.Empty<int>(ThreadPoolScheduler.Instance).ToUniTaskAsyncEnumerable().ToArrayAsync();
var ys = await Observable.Empty<int>(ThreadPoolScheduler.Instance).ToArray();
xs.Should().BeEquivalentTo(ys);
}
}
}
}

View File

@ -6,7 +6,7 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Xunit; using Xunit;
namespace UniTask.NetCoreTests.Linq namespace NetCoreTests.Linq
{ {
public class Factory public class Factory
{ {

View File

@ -11,6 +11,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" /> <PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="System.Reactive" Version="4.4.1" />
<PackageReference Include="xunit" Version="2.4.1" /> <PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1"> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>

View File

@ -23,7 +23,13 @@ namespace Cysharp.Threading.Tasks.Internal
[MethodImpl(MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoInlining)]
internal static Exception ArgumentOutOfRange(string paramName) internal static Exception ArgumentOutOfRange(string paramName)
{ {
throw new ArgumentOutOfRangeException(paramName); return new ArgumentOutOfRangeException(paramName);
}
[MethodImpl(MethodImplOptions.NoInlining)]
internal static Exception NoElements()
{
throw new InvalidOperationException("Source sequence doesn't contain any elements.");
} }
[MethodImpl(MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoInlining)]