ValueTask support
mikasoukhov opened this issue · 1 comments
mikasoukhov commented
Here is some my own extensions (maybe will be interested to join with Nito's code, like you proj and use it):
public static async ValueTask AsValueTask<T>(this ValueTask<T> valueTask)
=> await valueTask;
public static ValueTask<T> AsValueTask<T>(this Task<T> task)
=> new(task);
public static ValueTask AsValueTask<T>(this Task task)
=> new(task);
public static async ValueTask<T[]> WhenAll<T>(this IEnumerable<ValueTask<T>> tasks)
{
if (tasks is null)
throw new ArgumentNullException(nameof(tasks));
List<Exception> exceptions = null;
var source = tasks.ToArray();
var results = new T[source.Length];
for (var i = 0; i < source.Length; i++)
{
try
{
results[i] = await source[i].ConfigureAwait(false);
}
catch (Exception ex)
{
exceptions ??= new List<Exception>(source.Length);
exceptions.Add(ex);
}
}
if (exceptions is not null)
throw new AggregateException(exceptions);
return results;
}
public static async ValueTask WhenAll(this IEnumerable<ValueTask> tasks)
{
if (tasks is null)
throw new ArgumentNullException(nameof(tasks));
List<Exception> exceptions = null;
var source = tasks.ToArray();
for (var i = 0; i < source.Length; i++)
{
try
{
await source[i].ConfigureAwait(false);
}
catch (Exception ex)
{
exceptions ??= new List<Exception>(source.Length);
exceptions.Add(ex);
}
}
if (exceptions is not null)
throw new AggregateException(exceptions);
}
public static void Run(Func<ValueTask> getTask)
{
if (getTask is null)
throw new ArgumentNullException(nameof(getTask));
AsyncContext.Run(() => getTask().AsTask());
}
public static T Run<T>(Func<ValueTask<T>> getTask)
{
if (getTask is null)
throw new ArgumentNullException(nameof(getTask));
return AsyncContext.Run(() => getTask().AsTask());
}
BalassaMarton commented
It would be quite inconvenient to have this name for a static utility class now that there's void ValueTask
.