Outcome<TResult>
Represents the outcome of an operation which could be a result of type TResult or an exception.
using Polly.Utils;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
namespace Polly
{
[System.Runtime.CompilerServices.NullableContext(2)]
[System.Runtime.CompilerServices.Nullable(0)]
public readonly struct Outcome<TResult>
{
public Exception Exception => ExceptionDispatchInfo?.SourceException;
internal ExceptionDispatchInfo ExceptionDispatchInfo { get; }
public TResult Result { get; }
public bool HasResult => ExceptionDispatchInfo == null;
public bool IsVoidResult => ((object)Result) is VoidResult;
[System.Runtime.CompilerServices.NullableContext(1)]
internal Outcome(Exception exception)
{
this = default(Outcome<TResult>);
ExceptionDispatchInfo = ExceptionDispatchInfo.Capture(Guard.NotNull<Exception>(exception, "exception"));
}
internal Outcome(TResult result)
{
this = default(Outcome<TResult>);
Result = result;
}
[System.Runtime.CompilerServices.NullableContext(1)]
private Outcome(ExceptionDispatchInfo exceptionDispatchInfo)
{
this = default(Outcome<TResult>);
ExceptionDispatchInfo = Guard.NotNull<ExceptionDispatchInfo>(exceptionDispatchInfo, "exceptionDispatchInfo");
}
public void EnsureSuccess()
{
ExceptionDispatchInfo?.Throw();
}
public bool TryGetResult(out TResult result)
{
if (HasResult && !IsVoidResult) {
result = Result;
return true;
}
result = default(TResult);
return false;
}
[System.Runtime.CompilerServices.NullableContext(1)]
public override string ToString()
{
object obj;
if (ExceptionDispatchInfo == null) {
TResult result = Result;
obj = result?.ToString();
if (obj == null)
return string.Empty;
} else
obj = Exception.Message;
return (string)obj;
}
[System.Runtime.CompilerServices.NullableContext(1)]
internal TResult GetResultOrRethrow()
{
ExceptionDispatchInfo?.Throw();
return Result;
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
internal Outcome<object> AsOutcome()
{
return AsOutcome<object>();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
internal Outcome<T> AsOutcome<T>()
{
if (this.ExceptionDispatchInfo != null)
return new Outcome<T>(this.ExceptionDispatchInfo);
if (this.Result == null)
return new Outcome<T>(default(T));
if (typeof(T) == typeof(TResult)) {
TResult source = this.Result;
return new Outcome<T>(Unsafe.As<TResult, T>(ref source));
}
return new Outcome<T>((T)(object)this.Result);
}
}
}