CompletedTask<T, TConstant>
using DotNext.Generic;
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace DotNext.Threading.Tasks
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public static class CompletedTask<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(0)] TConstant> where TConstant : Constant<T>, new
{
private static readonly T Value = (Constant<T>)new TConstant();
public static readonly Task<T> Task = System.Threading.Tasks.Task.FromResult<T>(Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsFaulted(Task<T> task, [System.Runtime.CompilerServices.Nullable(2)] object predicate)
{
if (task.IsFaulted)
return Unsafe.As<Predicate<AggregateException>>(predicate)(task.Exception);
return false;
}
internal static T WhenFaulted(Task<T> task, [System.Runtime.CompilerServices.Nullable(2)] object predicate)
{
if (!IsFaulted(task, predicate))
return task.GetAwaiter().GetResult();
return Value;
}
internal static T WhenCanceled(Task<T> task)
{
if (!task.IsCanceled)
return task.GetAwaiter().GetResult();
return Value;
}
internal static T WhenFaultedOrCanceled(Task<T> task, [System.Runtime.CompilerServices.Nullable(2)] object predicate)
{
if (!IsFaulted(task, predicate) && !task.IsCanceled)
return task.GetAwaiter().GetResult();
return Value;
}
}
}