Skip to content

Commit

Permalink
Introduce a single place where all public async overloads are maintai…
Browse files Browse the repository at this point in the history
…ned to evaluate design options using a single SUPPORT_FLAT_ASYNC_API flag.
  • Loading branch information
bartdesmet committed Mar 1, 2019
1 parent 895cae3 commit b8c42e1
Show file tree
Hide file tree
Showing 34 changed files with 672 additions and 432 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Reflection" #>
<#@ output extension=".cs" #>
#if SUPPORT_FLAT_ASYNC_API

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace System.Linq
{
partial class AsyncEnumerable
{
<#
Func<Type, string> toCSharp = null;
toCSharp = t =>
Expand Down Expand Up @@ -75,9 +65,21 @@ toCSharp = t =>
return t.Name;
}
};
#>
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

var methods = typeof(AsyncEnumerable).GetMethods(BindingFlags.Public | BindingFlags.Static);
var asyncMethods = methods.Where(m => m.Name.Contains("Await")).OrderBy(m => m.Name).ThenBy(m => m.GetParameters().Length);
namespace System.Linq
{
partial class AsyncEnumerable
{
<#
var methods = typeof(AsyncEnumerable).GetMethods(BindingFlags.NonPublic | BindingFlags.Static);
var asyncMethods = methods.Where(m => m.Name.Contains("Await") && m.Name.EndsWith("Core")).OrderBy(m => m.Name).ThenBy(m => m.GetParameters().Length);
#>
#if SUPPORT_FLAT_ASYNC_API
<#
foreach (var g in asyncMethods.GroupBy(m => m.Name.Contains("WithCancellation")))
{
if (g.Key)
Expand All @@ -91,7 +93,7 @@ foreach (var g in asyncMethods.GroupBy(m => m.Name.Contains("WithCancellation"))
foreach (var m in g)
{
var longName = m.Name;
var shortName = m.Name.Replace("WithCancellation", "").Replace("Await", "");
var shortName = m.Name.Replace("WithCancellation", "").Replace("Await", "").Replace("Core", "");
var genArgs = m.IsGenericMethod ? "<" + string.Join(", ", m.GetGenericArguments().Select(t => t.Name)) + ">" : "";
var returnType = toCSharp(m.ReturnType);
var isExtension = m.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), true);
Expand All @@ -110,7 +112,40 @@ foreach (var g in asyncMethods.GroupBy(m => m.Name.Contains("WithCancellation"))
}
}
#>
#else
<#
foreach (var g in asyncMethods.GroupBy(m => m.Name.Contains("WithCancellation")))
{
if (g.Key)
{
#>

#if !NO_DEEP_CANCELLATION
<#
}
}

foreach (var m in g)
{
var longName = m.Name;
var shortName = m.Name.Replace("Core", "");
var genArgs = m.IsGenericMethod ? "<" + string.Join(", ", m.GetGenericArguments().Select(t => t.Name)) + ">" : "";
var returnType = toCSharp(m.ReturnType);
var isExtension = m.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), true);
var pars = (isExtension ? "this " : "") + string.Join(", ", m.GetParameters().Select(p => toCSharp(p.ParameterType) + " " + p.Name));
var args = string.Join(", ", m.GetParameters().Select(p => p.Name));
#>
public static <#=returnType#> <#=shortName#><#=genArgs#>(<#=pars#>) => <#=longName#><#=genArgs#>(<#=args#>);
<#
}

if (g.Key)
{
#>
#endif
<#
}
}
#>
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> _source, Func<TSo
}
}

public static ValueTask<TSource> AggregateAwaitAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, ValueTask<TSource>> accumulator, CancellationToken cancellationToken = default)
internal static ValueTask<TSource> AggregateAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, ValueTask<TSource>> accumulator, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
Expand Down Expand Up @@ -71,7 +71,7 @@ static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> _source, Func<TSo
}

#if !NO_DEEP_CANCELLATION
public static ValueTask<TSource> AggregateAwaitWithCancellationAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, CancellationToken, ValueTask<TSource>> accumulator, CancellationToken cancellationToken = default)
internal static ValueTask<TSource> AggregateAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, CancellationToken, ValueTask<TSource>> accumulator, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
Expand Down Expand Up @@ -124,7 +124,7 @@ static async ValueTask<TAccumulate> Core(IAsyncEnumerable<TSource> _source, TAcc
}
}

public static ValueTask<TAccumulate> AggregateAwaitAsync<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> accumulator, CancellationToken cancellationToken = default)
internal static ValueTask<TAccumulate> AggregateAwaitAsyncCore<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> accumulator, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
Expand All @@ -147,7 +147,7 @@ static async ValueTask<TAccumulate> Core(IAsyncEnumerable<TSource> _source, TAcc
}

#if !NO_DEEP_CANCELLATION
public static ValueTask<TAccumulate> AggregateAwaitWithCancellationAsync<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> accumulator, CancellationToken cancellationToken = default)
internal static ValueTask<TAccumulate> AggregateAwaitWithCancellationAsyncCore<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> accumulator, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
Expand Down Expand Up @@ -194,7 +194,7 @@ static async ValueTask<TResult> Core(IAsyncEnumerable<TSource> _source, TAccumul
}
}

public static ValueTask<TResult> AggregateAwaitAsync<TSource, TAccumulate, TResult>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> accumulator, Func<TAccumulate, ValueTask<TResult>> resultSelector, CancellationToken cancellationToken = default)
internal static ValueTask<TResult> AggregateAwaitAsyncCore<TSource, TAccumulate, TResult>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> accumulator, Func<TAccumulate, ValueTask<TResult>> resultSelector, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
Expand All @@ -219,7 +219,7 @@ static async ValueTask<TResult> Core(IAsyncEnumerable<TSource> _source, TAccumul
}

#if !NO_DEEP_CANCELLATION
public static ValueTask<TResult> AggregateAwaitWithCancellationAsync<TSource, TAccumulate, TResult>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> accumulator, Func<TAccumulate, CancellationToken, ValueTask<TResult>> resultSelector, CancellationToken cancellationToken = default)
internal static ValueTask<TResult> AggregateAwaitWithCancellationAsyncCore<TSource, TAccumulate, TResult>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> accumulator, Func<TAccumulate, CancellationToken, ValueTask<TResult>> resultSelector, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
Expand Down
4 changes: 2 additions & 2 deletions Ix.NET/Source/System.Linq.Async/System/Linq/Operators/All.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static async ValueTask<bool> Core(IAsyncEnumerable<TSource> _source, Func<TSourc
}
}

public static ValueTask<bool> AllAwaitAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
internal static ValueTask<bool> AllAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
Expand All @@ -57,7 +57,7 @@ static async ValueTask<bool> Core(IAsyncEnumerable<TSource> _source, Func<TSourc
}

#if !NO_DEEP_CANCELLATION
public static ValueTask<bool> AllAwaitWithCancellationAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
internal static ValueTask<bool> AllAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
Expand Down
4 changes: 2 additions & 2 deletions Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Any.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static async ValueTask<bool> Core(IAsyncEnumerable<TSource> _source, Func<TSourc
}
}

public static ValueTask<bool> AnyAwaitAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
internal static ValueTask<bool> AnyAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
Expand All @@ -73,7 +73,7 @@ static async ValueTask<bool> Core(IAsyncEnumerable<TSource> _source, Func<TSourc
}

#if !NO_DEEP_CANCELLATION
public static ValueTask<bool> AnyAwaitWithCancellationAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
internal static ValueTask<bool> AnyAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
Expand Down
Loading

0 comments on commit b8c42e1

Please sign in to comment.