feat: add none generic result type
This adds a none generic result type and refactors the result to be a class. The Result has the same inheritance like `Task` and `Task<T>`
This commit is contained in:
parent
f77c1ff29b
commit
cb2628c615
18 changed files with 2745 additions and 41 deletions
20
src/request.result/IResultFactory.cs
Normal file
20
src/request.result/IResultFactory.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
// Copyright (c) The Geekeey Authors
|
||||
// SPDX-License-Identifier: EUPL-1.2
|
||||
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Geekeey.Request.Result;
|
||||
|
||||
/// <summary>
|
||||
/// An interface for a result.
|
||||
/// </summary>
|
||||
[SuppressMessage("Naming", "CA1716:Identifiers should not match keywords")]
|
||||
public interface IResultFactory<out TSelf> where TSelf : IResultFactory<TSelf>
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new result with a failure value.
|
||||
/// </summary>
|
||||
/// <param name="error">The error of the result.</param>
|
||||
/// <returns>A new result with a failure value.</returns>
|
||||
static abstract TSelf Failure(Error error);
|
||||
}
|
||||
|
|
@ -14,6 +14,25 @@ namespace Geekeey.Request.Result;
|
|||
/// </remarks>
|
||||
public static class Prelude
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a result containing a success.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
public static Result Success()
|
||||
{
|
||||
return new Result();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a result containing a failure.
|
||||
/// </summary>
|
||||
/// <param name="error">The failure value to create the result from.</param>
|
||||
[Pure]
|
||||
public static Result Failure(Error error)
|
||||
{
|
||||
return new Result(error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a result containing a success value.
|
||||
/// </summary>
|
||||
|
|
@ -36,6 +55,69 @@ public static class Prelude
|
|||
return new Result<T>(error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to execute an action and return the result. If the action throws an exception, the exception will be
|
||||
/// returned wrapped in an <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <param name="function">The action to try to execute.</param>
|
||||
/// <returns>A result containing success or an <see cref="ExceptionError"/> containing the exception thrown by the
|
||||
/// action.</returns>
|
||||
[Pure]
|
||||
public static Result Try(Action function)
|
||||
{
|
||||
try
|
||||
{
|
||||
function();
|
||||
return new Result();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new Result(new ExceptionError(exception));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to execute an asynchronous function and return the result. If the function throws an exception, the
|
||||
/// exception will be returned wrapped in an <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <param name="function">The function to try to execute.</param>
|
||||
/// <returns>A result containing success or an <see cref="ExceptionError"/> containing the exception thrown by the
|
||||
/// function.</returns>
|
||||
[Pure]
|
||||
public static async ValueTask<Result> TryAsync(Func<ValueTask> function)
|
||||
{
|
||||
try
|
||||
{
|
||||
await function();
|
||||
return new Result();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new Result(new ExceptionError(exception));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to execute an asynchronous function and return the result. If the function throws an exception, the
|
||||
/// exception will be returned wrapped in an <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <param name="function">The function to try to execute.</param>
|
||||
/// <returns>A result containing success or an <see cref="ExceptionError"/> containing the exception thrown by the
|
||||
/// function.</returns>
|
||||
[Pure]
|
||||
public static async Task<Result> TryAsync(Func<Task> function)
|
||||
{
|
||||
try
|
||||
{
|
||||
await function();
|
||||
return new Result();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new Result(new ExceptionError(exception));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to execute a function and return the result. If the function throws an exception, the exception will be
|
||||
/// returned wrapped in an <see cref="ExceptionError"/>.
|
||||
|
|
|
|||
|
|
@ -5,7 +5,20 @@ using System.Diagnostics.Contracts;
|
|||
|
||||
namespace Geekeey.Request.Result;
|
||||
|
||||
public readonly partial struct Result<T>
|
||||
public partial class Result
|
||||
{
|
||||
/// <summary>
|
||||
/// Implicitly constructs a result from a failure value.
|
||||
/// </summary>
|
||||
/// <param name="error">The error to construct the result from.</param>
|
||||
[Pure]
|
||||
public static implicit operator Result(Error error)
|
||||
{
|
||||
return new Result(error);
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Implicitly constructs a result from a success value.
|
||||
|
|
|
|||
|
|
@ -7,7 +7,73 @@ using System.Numerics;
|
|||
|
||||
namespace Geekeey.Request.Result;
|
||||
|
||||
public readonly partial struct Result<T> : IEquatable<Result<T>>, IEquatable<T>
|
||||
public partial class Result : IEquatable<Result>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
[Pure]
|
||||
public bool Equals(Result? other)
|
||||
{
|
||||
return Equals(this, other);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
[Pure]
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
return obj is Result r && Equals(r);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
[Pure]
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return GetHashCode(this);
|
||||
}
|
||||
|
||||
internal static bool Equals(Result? a, Result? b)
|
||||
{
|
||||
if (a is null || b is null)
|
||||
{
|
||||
return a is null && b is null;
|
||||
}
|
||||
|
||||
return a.IsSuccess == b.IsSuccess;
|
||||
}
|
||||
|
||||
internal static int GetHashCode(Result? result)
|
||||
{
|
||||
return result?.IsSuccess.GetHashCode() ?? 0;
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result : IEqualityOperators<Result, Result, bool>
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks whether two results are equal. Results are equal if they are both success or both failure.
|
||||
/// </summary>
|
||||
/// <param name="a">The first result to compare.</param>
|
||||
/// <param name="b">The second result to compare.</param>
|
||||
[Pure]
|
||||
[ExcludeFromCodeCoverage]
|
||||
public static bool operator ==(Result? a, Result? b)
|
||||
{
|
||||
return Equals(a, b);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether two results are not equal. Results are equal if they are both success or both failure.
|
||||
/// </summary>
|
||||
/// <param name="a">The first result to compare.</param>
|
||||
/// <param name="b">The second result to compare.</param>
|
||||
[Pure]
|
||||
[ExcludeFromCodeCoverage]
|
||||
public static bool operator !=(Result? a, Result? b)
|
||||
{
|
||||
return !Equals(a, b);
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result<T> : IEquatable<Result<T>>, IEquatable<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks whether the result is equal to another result. Results are equal if both results are success values and
|
||||
|
|
@ -15,7 +81,7 @@ public readonly partial struct Result<T> : IEquatable<Result<T>>, IEquatable<T>
|
|||
/// </summary>
|
||||
/// <param name="other">The result to check for equality with the current result.</param>
|
||||
[Pure]
|
||||
public bool Equals(Result<T> other)
|
||||
public bool Equals(Result<T>? other)
|
||||
{
|
||||
return Equals(this, other, EqualityComparer<T>.Default);
|
||||
}
|
||||
|
|
@ -68,8 +134,13 @@ public readonly partial struct Result<T> : IEquatable<Result<T>>, IEquatable<T>
|
|||
return GetHashCode(this, EqualityComparer<T>.Default);
|
||||
}
|
||||
|
||||
internal static bool Equals(Result<T> a, Result<T> b, IEqualityComparer<T> comparer)
|
||||
internal static bool Equals(Result<T>? a, Result<T>? b, IEqualityComparer<T> comparer)
|
||||
{
|
||||
if (a is null || b is null)
|
||||
{
|
||||
return a is null && b is null;
|
||||
}
|
||||
|
||||
if (!a.IsSuccess || !b.IsSuccess)
|
||||
{
|
||||
return !a.IsSuccess && !b.IsSuccess;
|
||||
|
|
@ -83,8 +154,13 @@ public readonly partial struct Result<T> : IEquatable<Result<T>>, IEquatable<T>
|
|||
return comparer.Equals(a.Value, b.Value);
|
||||
}
|
||||
|
||||
internal static bool Equals(Result<T> a, T? b, IEqualityComparer<T> comparer)
|
||||
internal static bool Equals(Result<T>? a, T? b, IEqualityComparer<T> comparer)
|
||||
{
|
||||
if (a is null)
|
||||
{
|
||||
return b is null;
|
||||
}
|
||||
|
||||
if (!a.IsSuccess)
|
||||
{
|
||||
return false;
|
||||
|
|
@ -109,7 +185,7 @@ public readonly partial struct Result<T> : IEquatable<Result<T>>, IEquatable<T>
|
|||
}
|
||||
}
|
||||
|
||||
public readonly partial struct Result<T> : IEqualityOperators<Result<T>, Result<T>, bool>, IEqualityOperators<Result<T>, T, bool>
|
||||
public partial class Result<T> : IEqualityOperators<Result<T>, Result<T>, bool>, IEqualityOperators<Result<T>, T, bool>
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks whether two results are equal. Results are equal if both results are success values and the success
|
||||
|
|
@ -119,7 +195,7 @@ public readonly partial struct Result<T> : IEqualityOperators<Result<T>, Result<
|
|||
/// <param name="b">The second result to compare.</param>
|
||||
[Pure]
|
||||
[ExcludeFromCodeCoverage]
|
||||
public static bool operator ==(Result<T> a, Result<T> b)
|
||||
public static bool operator ==(Result<T>? a, Result<T>? b)
|
||||
{
|
||||
return Equals(a, b, EqualityComparer<T>.Default);
|
||||
}
|
||||
|
|
@ -132,7 +208,7 @@ public readonly partial struct Result<T> : IEqualityOperators<Result<T>, Result<
|
|||
/// <param name="b">The second result to compare.</param>
|
||||
[Pure]
|
||||
[ExcludeFromCodeCoverage]
|
||||
public static bool operator !=(Result<T> a, Result<T> b)
|
||||
public static bool operator !=(Result<T>? a, Result<T>? b)
|
||||
{
|
||||
return !Equals(a, b, EqualityComparer<T>.Default);
|
||||
}
|
||||
|
|
@ -144,7 +220,7 @@ public readonly partial struct Result<T> : IEqualityOperators<Result<T>, Result<
|
|||
/// <param name="b">The value to check for equality with the success value in the result.</param>
|
||||
[Pure]
|
||||
[ExcludeFromCodeCoverage]
|
||||
public static bool operator ==(Result<T> a, T? b)
|
||||
public static bool operator ==(Result<T>? a, T? b)
|
||||
{
|
||||
return Equals(a, b, EqualityComparer<T>.Default);
|
||||
}
|
||||
|
|
@ -156,7 +232,7 @@ public readonly partial struct Result<T> : IEqualityOperators<Result<T>, Result<
|
|||
/// <param name="b">The value to check for inequality with the success value in the result.</param>
|
||||
[Pure]
|
||||
[ExcludeFromCodeCoverage]
|
||||
public static bool operator !=(Result<T> a, T? b)
|
||||
public static bool operator !=(Result<T>? a, T? b)
|
||||
{
|
||||
return !Equals(a, b, EqualityComparer<T>.Default);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,102 @@ using System.Diagnostics.Contracts;
|
|||
|
||||
namespace Geekeey.Request.Result;
|
||||
|
||||
public readonly partial struct Result<T>
|
||||
public partial class Result
|
||||
{
|
||||
/// <summary>
|
||||
/// Matches over the success or failure of the result and returns another value. Can be conceptualized as an
|
||||
/// exhaustive <c>switch</c> expression matching all possible states of the type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">The type to return from the match.</typeparam>
|
||||
/// <param name="success">The function to invoke if the result is a success.</param>
|
||||
/// <param name="failure">The function to apply to the failure value of the result if the result is a failure.</param>
|
||||
/// <returns>The result of applying either <paramref name="success"/> or <paramref name="failure"/> on the result.</returns>
|
||||
[Pure]
|
||||
public TResult Match<TResult>(Func<TResult> success, Func<Error, TResult> failure)
|
||||
{
|
||||
return IsSuccess ? success() : failure(Error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Matches over the success or failure of the result and invokes an effectful action. Can be conceptualized as an
|
||||
/// exhaustive <c>switch</c> statement matching all possible states of the type.
|
||||
/// </summary>
|
||||
/// <param name="success">The function to call if the result is a success.</param>
|
||||
/// <param name="failure">The function to call with the failure value of the result if the result is a failure.</param>
|
||||
public void Switch(Action success, Action<Error> failure)
|
||||
{
|
||||
if (IsSuccess)
|
||||
{
|
||||
success();
|
||||
}
|
||||
else
|
||||
{
|
||||
failure(Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result
|
||||
{
|
||||
/// <summary>
|
||||
/// Asynchronously matches over the success or failure of the result and returns another value. Can be
|
||||
/// conceptualized as an exhaustive <c>switch</c> expression matching all possible states of the type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">The type to return from the match.</typeparam>
|
||||
/// <param name="success">The function to invoke if the result is a success.</param>
|
||||
/// <param name="failure">The function to apply to the failure value of the result if the result is a failure.</param>
|
||||
/// <returns>A task completing with the result of applying either <paramref name="success"/> or
|
||||
/// <paramref name="failure"/> on the failure value of the result.</returns>
|
||||
[Pure]
|
||||
public async Task<TResult> MatchAsync<TResult>(Func<Task<TResult>> success, Func<Error, Task<TResult>> failure)
|
||||
{
|
||||
return IsSuccess ? await success() : await failure(Error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously matches over the success or failure of the result and invokes an effectful action onto the
|
||||
/// failure value. Can be conceptualized as an exhaustive <c>switch</c> statement matching all possible states of
|
||||
/// the type.
|
||||
/// </summary>
|
||||
/// <param name="success">The function to call if the result is a success.</param>
|
||||
/// <param name="failure">The function to call with the failure value of the result if the result is a failure.</param>
|
||||
public Task SwitchAsync(Func<Task> success, Func<Error, Task> failure)
|
||||
{
|
||||
return IsSuccess ? success() : failure(Error);
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result
|
||||
{
|
||||
/// <summary>
|
||||
/// Asynchronously matches over the success or failure of the result and returns another value. Can be
|
||||
/// conceptualized as an exhaustive <c>switch</c> expression matching all possible states of the type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">The type to return from the match.</typeparam>
|
||||
/// <param name="success">The function to invoke if the result is a success.</param>
|
||||
/// <param name="failure">The function to apply to the failure value of the result if the result is a failure.</param>
|
||||
/// <returns>A task completing with the result of applying either <paramref name="success"/> or
|
||||
/// <paramref name="failure"/> on the failure value of the result.</returns>
|
||||
[Pure]
|
||||
public ValueTask<TResult> MatchAsync<TResult>(Func<ValueTask<TResult>> success, Func<Error, ValueTask<TResult>> failure)
|
||||
{
|
||||
return IsSuccess ? success() : failure(Error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously matches over the success or failure of the result and invokes an effectful action onto the
|
||||
/// failure value. Can be conceptualized as an exhaustive <c>switch</c> statement matching all possible states of
|
||||
/// the type.
|
||||
/// </summary>
|
||||
/// <param name="success">The function to call if the result is a success.</param>
|
||||
/// <param name="failure">The function to call with the failure value of the result if the result is a failure.</param>
|
||||
public ValueTask SwitchAsync(Func<ValueTask> success, Func<Error, ValueTask> failure)
|
||||
{
|
||||
return IsSuccess ? success() : failure(Error);
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Matches over the success value or failure value of the result and returns another value. Can be conceptualized
|
||||
|
|
@ -42,7 +137,7 @@ public readonly partial struct Result<T>
|
|||
}
|
||||
}
|
||||
|
||||
public readonly partial struct Result<T>
|
||||
public partial class Result<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Asynchronously matches over the success value or failure value of the result and returns another value. Can be
|
||||
|
|
@ -73,7 +168,7 @@ public readonly partial struct Result<T>
|
|||
}
|
||||
}
|
||||
|
||||
public readonly partial struct Result<T>
|
||||
public partial class Result<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Asynchronously matches over the success value or failure value of the result and returns another value. Can be
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ using System.Diagnostics.Contracts;
|
|||
|
||||
namespace Geekeey.Request.Result;
|
||||
|
||||
public readonly partial struct Result<T>
|
||||
public partial class Result<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Maps the success value of the result using a mapping function, or does nothing if the result is a failure.
|
||||
|
|
@ -79,7 +79,7 @@ public readonly partial struct Result<T>
|
|||
}
|
||||
}
|
||||
|
||||
public readonly partial struct Result<T>
|
||||
public partial class Result<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Maps the success value of the result using an asynchronous mapping function, or does nothing if the result is
|
||||
|
|
@ -222,7 +222,7 @@ public readonly partial struct Result<T>
|
|||
}
|
||||
}
|
||||
|
||||
public readonly partial struct Result<T>
|
||||
public partial class Result<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Maps the success value of the result using an asynchronous mapping function, or does nothing if the result is
|
||||
|
|
@ -364,3 +364,696 @@ public readonly partial struct Result<T>
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Chains a non-generic result to this result if it is a success, or does nothing if the result is a failure.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to create the next result.</param>
|
||||
/// <returns>A result which is either the mapped result or a new result containing the failure value of the original
|
||||
/// result.</returns>
|
||||
[Pure]
|
||||
public Result Then(Func<T, Result> func)
|
||||
{
|
||||
return IsSuccess ? func(Value) : new Result(Error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to chain a non-generic result to this result if it is a success, or does nothing if the result is a
|
||||
/// failure. If the function throws an exception, the exception will be returned wrapped in an
|
||||
/// <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to create the next result.</param>
|
||||
/// <returns>A result which is either the mapped result, the exception thrown by <paramref name="func"/> wrapped in
|
||||
/// an <see cref="ExceptionError"/>, or a new result containing the failure value of the original result.</returns>
|
||||
[Pure]
|
||||
public Result ThenTry(Func<T, Result> func)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Then(func);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new Result(new ExceptionError(exception));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Chains a non-generic result to this result using an asynchronous mapping function, or does nothing if the result is
|
||||
/// a failure.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to create the next result.</param>
|
||||
/// <returns>A <see cref="Task{T}"/> which either completes asynchronously by invoking the mapping function, or
|
||||
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||
[Pure]
|
||||
public Task<Result> ThenAsync(Func<T, Task<Result>> func)
|
||||
{
|
||||
if (!IsSuccess)
|
||||
{
|
||||
return Task.FromResult(new Result(Error));
|
||||
}
|
||||
|
||||
var task = func(Value);
|
||||
return CreateResult(task);
|
||||
|
||||
static async Task<Result> CreateResult(Task<Result> task)
|
||||
{
|
||||
var result = await task;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to chain a non-generic result to this result using an asynchronous mapping function, or does nothing if the
|
||||
/// result is a failure. If the mapping function throws an exception, the exception will be returned wrapped in
|
||||
/// an <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to create the next result.</param>
|
||||
/// <returns>A <see cref="Task{T}"/> which either completes asynchronously by invoking the mapping function,
|
||||
/// returning any exception thrown by <paramref name="func"/> wrapped in an <see cref="ExceptionError"/>, or
|
||||
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||
[Pure]
|
||||
public Task<Result> ThenTryAsync(Func<T, Task<Result>> func)
|
||||
{
|
||||
if (!IsSuccess)
|
||||
{
|
||||
return Task.FromResult(new Result(Error));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var task = func(Value);
|
||||
return CreateResult(task);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return Task.FromResult(new Result(new ExceptionError(exception)));
|
||||
}
|
||||
|
||||
static async Task<Result> CreateResult(Task<Result> task)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value = await task;
|
||||
return value;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new Result(new ExceptionError(exception));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Chains a non-generic result to this result using an asynchronous mapping function, or does nothing if the result is
|
||||
/// a failure.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to create the next result.</param>
|
||||
/// <returns>A <see cref="ValueTask{T}"/> which either completes asynchronously by invoking the mapping function, or
|
||||
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||
[Pure]
|
||||
public ValueTask<Result> ThenAsync(Func<T, ValueTask<Result>> func)
|
||||
{
|
||||
if (!IsSuccess)
|
||||
{
|
||||
return ValueTask.FromResult(new Result(Error));
|
||||
}
|
||||
|
||||
var task = func(Value);
|
||||
return CreateResult(task);
|
||||
|
||||
static async ValueTask<Result> CreateResult(ValueTask<Result> task)
|
||||
{
|
||||
var result = await task;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to chain a non-generic result to this result using an asynchronous mapping function, or does nothing if the
|
||||
/// result is a failure. If the mapping function throws an exception, the exception will be returned wrapped in
|
||||
/// an <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to create the next result.</param>
|
||||
/// <returns>A <see cref="ValueTask{T}"/> which either completes asynchronously by invoking the mapping function,
|
||||
/// returning any exception thrown by <paramref name="func"/> wrapped in an <see cref="ExceptionError"/>, or
|
||||
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||
[Pure]
|
||||
public ValueTask<Result> ThenTryAsync(Func<T, ValueTask<Result>> func)
|
||||
{
|
||||
if (!IsSuccess)
|
||||
{
|
||||
return ValueTask.FromResult(new Result(Error));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var task = func(Value);
|
||||
return CreateResult(task);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return ValueTask.FromResult(new Result(new ExceptionError(exception)));
|
||||
}
|
||||
|
||||
static async ValueTask<Result> CreateResult(ValueTask<Result> task)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value = await task;
|
||||
return value;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new Result(new ExceptionError(exception));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result
|
||||
{
|
||||
/// <summary>
|
||||
/// Maps the success of the result to a new success value using a mapping function, or does nothing if the result is
|
||||
/// a failure.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to map the success to a new value.</param>
|
||||
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||
/// <returns>A new result containing either the mapped success value or the failure value of the original
|
||||
/// result.</returns>
|
||||
[Pure]
|
||||
public Result<TNew> Map<TNew>(Func<TNew> func)
|
||||
{
|
||||
return IsSuccess ? new Result<TNew>(func()) : new Result<TNew>(Error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to map the success of the result to a new success value using a mapping function, or does nothing if the
|
||||
/// result is a failure. If the mapping function throws an exception, the exception will be returned wrapped in an
|
||||
/// <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to map the success to a new value.</param>
|
||||
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||
/// <returns>A new result containing either the mapped value, the exception thrown by <paramref name="func"/>
|
||||
/// wrapped in an <see cref="ExceptionError"/>, or the failure value of the original result.</returns>
|
||||
[Pure]
|
||||
public Result<TNew> TryMap<TNew>(Func<TNew> func)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Map(func);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new Result<TNew>(new ExceptionError(exception));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Chains another result to this result if it is a success, or does nothing if the result is a failure.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to create the next result.</param>
|
||||
/// <returns>A result which is either the mapped result or a new result containing the failure value of the original
|
||||
/// result.</returns>
|
||||
[Pure]
|
||||
public Result Then(Func<Result> func)
|
||||
{
|
||||
return IsSuccess ? func() : new Result(Error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to chain another result to this result if it is a success, or does nothing if the result is a failure. If
|
||||
/// the function throws an exception, the exception will be returned wrapped in an <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to create the next result.</param>
|
||||
/// <returns>A result which is either the mapped result, the exception thrown by <paramref name="func"/> wrapped in
|
||||
/// an <see cref="ExceptionError"/>, or a new result containing the failure value of the original result.</returns>
|
||||
[Pure]
|
||||
public Result ThenTry(Func<Result> func)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Then(func);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new Result(new ExceptionError(exception));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Chains a generic result to this result if it is a success, or does nothing if the result is a failure.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to create the next generic result.</param>
|
||||
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||
/// <returns>A result which is either the mapped result or a new result containing the failure value of the original
|
||||
/// result.</returns>
|
||||
[Pure]
|
||||
public Result<TNew> Then<TNew>(Func<Result<TNew>> func)
|
||||
{
|
||||
return IsSuccess ? func() : new Result<TNew>(Error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to chain a generic result to this result if it is a success, or does nothing if the result is a failure. If
|
||||
/// the function throws an exception, the exception will be returned wrapped in an <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to create the next generic result.</param>
|
||||
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||
/// <returns>A result which is either the mapped result, the exception thrown by <paramref name="func"/> wrapped in
|
||||
/// an <see cref="ExceptionError"/>, or a new result containing the failure value of the original result.</returns>
|
||||
[Pure]
|
||||
public Result<TNew> ThenTry<TNew>(Func<Result<TNew>> func)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Then(func);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new Result<TNew>(new ExceptionError(exception));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result
|
||||
{
|
||||
/// <summary>
|
||||
/// Maps the success of the result using an asynchronous mapping function, or does nothing if the result is
|
||||
/// a failure.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to map the success.</param>
|
||||
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||
/// <returns>A <see cref="Task{T}"/> which either completes asynchronously by invoking the mapping function and
|
||||
/// constructing a new result containing the mapped value, or completes synchronously by returning a new result
|
||||
/// containing the failure value of the original result.</returns>
|
||||
[Pure]
|
||||
public Task<Result<TNew>> MapAsync<TNew>(Func<Task<TNew>> func)
|
||||
{
|
||||
if (!IsSuccess)
|
||||
{
|
||||
return Task.FromResult(new Result<TNew>(Error));
|
||||
}
|
||||
|
||||
var task = func();
|
||||
return CreateResult(task);
|
||||
|
||||
static async Task<Result<TNew>> CreateResult(Task<TNew> task)
|
||||
{
|
||||
var value = await task;
|
||||
return new Result<TNew>(value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps the success of the result using an asynchronous mapping function, or does nothing if the result is a
|
||||
/// failure. If the mapping function throws an exception, the exception will be returned wrapped in an
|
||||
/// <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to map the success.</param>
|
||||
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||
/// <returns>A <see cref="Task{T}"/> which either completes asynchronously by invoking the mapping function and
|
||||
/// constructing a new result containing the mapped value, returning any exception thrown by <paramref name="func"/>
|
||||
/// wrapped in an <see cref="ExceptionError"/> or completes synchronously by returning a new result containing the
|
||||
/// failure value of the original result.</returns>
|
||||
[Pure]
|
||||
public Task<Result<TNew>> TryMapAsync<TNew>(Func<Task<TNew>> func)
|
||||
{
|
||||
if (!IsSuccess)
|
||||
{
|
||||
return Task.FromResult(new Result<TNew>(Error));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var task = func();
|
||||
return CreateResult(task);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return Task.FromResult(new Result<TNew>(new ExceptionError(exception)));
|
||||
}
|
||||
|
||||
static async Task<Result<TNew>> CreateResult(Task<TNew> task)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value = await task;
|
||||
return new Result<TNew>(value);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new Result<TNew>(new ExceptionError(exception));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Chains another result to this result using an asynchronous mapping function, or does nothing if the result is
|
||||
/// a failure.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to create the next result.</param>
|
||||
/// <returns>A <see cref="Task{T}"/> which either completes asynchronously by invoking the mapping function, or
|
||||
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||
[Pure]
|
||||
public Task<Result> ThenAsync(Func<Task<Result>> func)
|
||||
{
|
||||
if (!IsSuccess)
|
||||
{
|
||||
return Task.FromResult(new Result(Error));
|
||||
}
|
||||
|
||||
var task = func();
|
||||
return CreateResult(task);
|
||||
|
||||
static async Task<Result> CreateResult(Task<Result> task)
|
||||
{
|
||||
var result = await task;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to chain another result to this result using an asynchronous mapping function, or does nothing if the
|
||||
/// result is a failure. If the mapping function throws an exception, the exception will be returned wrapped in
|
||||
/// an <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to create the next result.</param>
|
||||
/// <returns>A <see cref="Task{T}"/> which either completes asynchronously by invoking the mapping function,
|
||||
/// returning any exception thrown by <paramref name="func"/> wrapped in an <see cref="ExceptionError"/>, or
|
||||
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||
[Pure]
|
||||
public Task<Result> ThenTryAsync(Func<Task<Result>> func)
|
||||
{
|
||||
if (!IsSuccess)
|
||||
{
|
||||
return Task.FromResult(new Result(Error));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var task = func();
|
||||
return CreateResult(task);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return Task.FromResult(new Result(new ExceptionError(exception)));
|
||||
}
|
||||
|
||||
static async Task<Result> CreateResult(Task<Result> task)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value = await task;
|
||||
return value;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new Result(new ExceptionError(exception));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Chains a generic result to this result using an asynchronous mapping function, or does nothing if the result is
|
||||
/// a failure.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to create the next result.</param>
|
||||
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||
/// <returns>A <see cref="Task{T}"/> which either completes asynchronously by invoking the mapping function, or
|
||||
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||
[Pure]
|
||||
public Task<Result<TNew>> ThenAsync<TNew>(Func<Task<Result<TNew>>> func)
|
||||
{
|
||||
if (!IsSuccess)
|
||||
{
|
||||
return Task.FromResult(new Result<TNew>(Error));
|
||||
}
|
||||
|
||||
var task = func();
|
||||
return CreateResult(task);
|
||||
|
||||
static async Task<Result<TNew>> CreateResult(Task<Result<TNew>> task)
|
||||
{
|
||||
var result = await task;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to chain a generic result to this result using an asynchronous mapping function, or does nothing if the
|
||||
/// result is a failure. If the mapping function throws an exception, the exception will be returned wrapped in
|
||||
/// an <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to create the next result.</param>
|
||||
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||
/// <returns>A <see cref="Task{T}"/> which either completes asynchronously by invoking the mapping function,
|
||||
/// returning any exception thrown by <paramref name="func"/> wrapped in an <see cref="ExceptionError"/>, or
|
||||
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||
[Pure]
|
||||
public Task<Result<TNew>> ThenTryAsync<TNew>(Func<Task<Result<TNew>>> func)
|
||||
{
|
||||
if (!IsSuccess)
|
||||
{
|
||||
return Task.FromResult(new Result<TNew>(Error));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var task = func();
|
||||
return CreateResult(task);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return Task.FromResult(new Result<TNew>(new ExceptionError(exception)));
|
||||
}
|
||||
|
||||
static async Task<Result<TNew>> CreateResult(Task<Result<TNew>> task)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value = await task;
|
||||
return value;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new Result<TNew>(new ExceptionError(exception));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result
|
||||
{
|
||||
/// <summary>
|
||||
/// Maps the success of the result using an asynchronous mapping function, or does nothing if the result is
|
||||
/// a failure.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to map the success.</param>
|
||||
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||
/// <returns>A <see cref="ValueTask{T}"/> which either completes asynchronously by invoking the mapping function and
|
||||
/// constructing a new result containing the mapped value, or completes synchronously by returning a new result
|
||||
/// containing the failure value of the original result.</returns>
|
||||
[Pure]
|
||||
public ValueTask<Result<TNew>> MapAsync<TNew>(Func<ValueTask<TNew>> func)
|
||||
{
|
||||
if (!IsSuccess)
|
||||
{
|
||||
return ValueTask.FromResult(new Result<TNew>(Error));
|
||||
}
|
||||
|
||||
var task = func();
|
||||
return CreateResult(task);
|
||||
|
||||
static async ValueTask<Result<TNew>> CreateResult(ValueTask<TNew> task)
|
||||
{
|
||||
var value = await task;
|
||||
return new Result<TNew>(value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps the success of the result using an asynchronous mapping function, or does nothing if the result is a
|
||||
/// failure. If the mapping function throws an exception, the exception will be returned wrapped in an
|
||||
/// <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to map the success.</param>
|
||||
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||
/// <returns>A <see cref="ValueTask{T}"/> which either completes asynchronously by invoking the mapping function and
|
||||
/// constructing a new result containing the mapped value, returning any exception thrown by <paramref name="func"/>
|
||||
/// wrapped in an <see cref="ExceptionError"/> or completes synchronously by returning a new result containing the
|
||||
/// failure value of the original result.</returns>
|
||||
[Pure]
|
||||
public ValueTask<Result<TNew>> TryMapAsync<TNew>(Func<ValueTask<TNew>> func)
|
||||
{
|
||||
if (!IsSuccess)
|
||||
{
|
||||
return ValueTask.FromResult(new Result<TNew>(Error));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var task = func();
|
||||
return CreateResult(task);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return ValueTask.FromResult(new Result<TNew>(new ExceptionError(exception)));
|
||||
}
|
||||
|
||||
static async ValueTask<Result<TNew>> CreateResult(ValueTask<TNew> task)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value = await task;
|
||||
return new Result<TNew>(value);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new Result<TNew>(new ExceptionError(exception));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Chains another result to this result using an asynchronous mapping function, or does nothing if the result is
|
||||
/// a failure.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to create the next result.</param>
|
||||
/// <returns>A <see cref="ValueTask{T}"/> which either completes asynchronously by invoking the mapping function, or
|
||||
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||
[Pure]
|
||||
public ValueTask<Result> ThenAsync(Func<ValueTask<Result>> func)
|
||||
{
|
||||
if (!IsSuccess)
|
||||
{
|
||||
return ValueTask.FromResult(new Result(Error));
|
||||
}
|
||||
|
||||
var task = func();
|
||||
return CreateResult(task);
|
||||
|
||||
static async ValueTask<Result> CreateResult(ValueTask<Result> task)
|
||||
{
|
||||
var result = await task;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to chain another result to this result using an asynchronous mapping function, or does nothing if the
|
||||
/// result is a failure. If the mapping function throws an exception, the exception will be returned wrapped in
|
||||
/// an <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to create the next result.</param>
|
||||
/// <returns>A <see cref="ValueTask{T}"/> which either completes asynchronously by invoking the mapping function,
|
||||
/// returning any exception thrown by <paramref name="func"/> wrapped in an <see cref="ExceptionError"/>, or
|
||||
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||
[Pure]
|
||||
public ValueTask<Result> ThenTryAsync(Func<ValueTask<Result>> func)
|
||||
{
|
||||
if (!IsSuccess)
|
||||
{
|
||||
return ValueTask.FromResult(new Result(Error));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var task = func();
|
||||
return CreateResult(task);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return ValueTask.FromResult(new Result(new ExceptionError(exception)));
|
||||
}
|
||||
|
||||
static async ValueTask<Result> CreateResult(ValueTask<Result> task)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value = await task;
|
||||
return value;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new Result(new ExceptionError(exception));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Chains a generic result to this result using an asynchronous mapping function, or does nothing if the result is
|
||||
/// a failure.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to create the next result.</param>
|
||||
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||
/// <returns>A <see cref="ValueTask{T}"/> which either completes asynchronously by invoking the mapping function, or
|
||||
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||
[Pure]
|
||||
public ValueTask<Result<TNew>> ThenAsync<TNew>(Func<ValueTask<Result<TNew>>> func)
|
||||
{
|
||||
if (!IsSuccess)
|
||||
{
|
||||
return ValueTask.FromResult(new Result<TNew>(Error));
|
||||
}
|
||||
|
||||
var task = func();
|
||||
return CreateResult(task);
|
||||
|
||||
static async ValueTask<Result<TNew>> CreateResult(ValueTask<Result<TNew>> task)
|
||||
{
|
||||
var result = await task;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to chain a generic result to this result using an asynchronous mapping function, or does nothing if the
|
||||
/// result is a failure. If the mapping function throws an exception, the exception will be returned wrapped in
|
||||
/// an <see cref="ExceptionError"/>.
|
||||
/// </summary>
|
||||
/// <param name="func">The function used to create the next result.</param>
|
||||
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||
/// <returns>A <see cref="ValueTask{T}"/> which either completes asynchronously by invoking the mapping function,
|
||||
/// returning any exception thrown by <paramref name="func"/> wrapped in an <see cref="ExceptionError"/>, or
|
||||
/// completes synchronously by returning a new result containing the failure value of the original result.</returns>
|
||||
[Pure]
|
||||
public ValueTask<Result<TNew>> ThenTryAsync<TNew>(Func<ValueTask<Result<TNew>>> func)
|
||||
{
|
||||
if (!IsSuccess)
|
||||
{
|
||||
return ValueTask.FromResult(new Result<TNew>(Error));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var task = func();
|
||||
return CreateResult(task);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return ValueTask.FromResult(new Result<TNew>(new ExceptionError(exception)));
|
||||
}
|
||||
|
||||
static async ValueTask<Result<TNew>> CreateResult(ValueTask<Result<TNew>> task)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value = await task;
|
||||
return value;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new Result<TNew>(new ExceptionError(exception));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ using System.Diagnostics.Contracts;
|
|||
|
||||
namespace Geekeey.Request.Result;
|
||||
|
||||
public readonly partial struct Result<T>
|
||||
public partial class Result<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Tries to get the success value from the result.
|
||||
|
|
|
|||
|
|
@ -10,18 +10,13 @@ namespace Geekeey.Request.Result;
|
|||
/// <summary>
|
||||
/// A type which contains either a success value or a failure value, which is represented by an <see cref="Error"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the success value.</typeparam>
|
||||
[DebuggerTypeProxy(typeof(Result<>.ResultDebugProxy))]
|
||||
public readonly partial struct Result<T>
|
||||
public partial class Result : IResultFactory<Result>
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new result with a success value.
|
||||
/// </summary>
|
||||
/// <param name="value">The success value.</param>
|
||||
public Result(T value)
|
||||
public Result()
|
||||
{
|
||||
IsSuccess = true;
|
||||
Value = value;
|
||||
Error = default;
|
||||
}
|
||||
|
||||
|
|
@ -31,14 +26,17 @@ public readonly partial struct Result<T>
|
|||
/// <param name="error">The error of the result.</param>
|
||||
public Result(Error error)
|
||||
{
|
||||
IsSuccess = false;
|
||||
Value = default;
|
||||
Error = error;
|
||||
}
|
||||
|
||||
internal T? Value { get; }
|
||||
|
||||
internal Error? Error => IsSuccess ? null : (field ?? Error.DefaultValueError);
|
||||
/// <summary>
|
||||
/// Represents the error associated with a failed result.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// When a result is unsuccessful, this property will hold an instance of a type derived from <see cref="Error"/>.
|
||||
/// It is null when the result is successful.
|
||||
/// </remarks>
|
||||
public Error? Error { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Whether the result is a success.
|
||||
|
|
@ -46,9 +44,8 @@ public readonly partial struct Result<T>
|
|||
/// <remarks>
|
||||
/// This is always the inverse of <see cref="IsFailure"/> but is more specific about intent.
|
||||
/// </remarks>
|
||||
[MemberNotNullWhen(true, nameof(Value))]
|
||||
[MemberNotNullWhen(false, nameof(Error))]
|
||||
public bool IsSuccess { get; }
|
||||
public virtual bool IsSuccess => Error is null;
|
||||
|
||||
/// <summary>
|
||||
/// Whether the result is a failure.
|
||||
|
|
@ -57,8 +54,74 @@ public readonly partial struct Result<T>
|
|||
/// This is always the inverse of <see cref="IsSuccess"/> but is more specific about intent.
|
||||
/// </remarks>
|
||||
[MemberNotNullWhen(true, nameof(Error))]
|
||||
public virtual bool IsFailure => !IsSuccess;
|
||||
|
||||
/// <inheritdoc/>
|
||||
[Pure]
|
||||
static Result IResultFactory<Result>.Failure(Error error)
|
||||
{
|
||||
return new Result(error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a string representation of the result.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
public override string ToString()
|
||||
{
|
||||
return IsSuccess ? $"Success" : $"Failure {{ {Error} }}";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A type which contains either a success value or a failure value, which is represented by an <see cref="Error"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the success value.</typeparam>
|
||||
[DebuggerTypeProxy(typeof(Result<>.ResultDebugProxy))]
|
||||
public partial class Result<T> : Result, IResultFactory<Result<T>>
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new result with a success value.
|
||||
/// </summary>
|
||||
/// <param name="value">The success value.</param>
|
||||
public Result(T value) : base()
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new result with a failure value.
|
||||
/// </summary>
|
||||
/// <param name="error">The error of the result.</param>
|
||||
public Result(Error error) : base(error)
|
||||
{
|
||||
Value = default;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the success value of the result if the operation was successful.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This property contains the value of type <typeparamref name="T"/> when the result is successful.
|
||||
/// If the result is unsuccessful, this property will be null or default, depending on the type.
|
||||
/// Accessing this property when the result is unsuccessful may raise an exception if not properly handled.
|
||||
/// </remarks>
|
||||
public T? Value { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
[MemberNotNullWhen(true, nameof(Value))]
|
||||
public override bool IsSuccess => base.IsSuccess;
|
||||
|
||||
/// <inheritdoc/>
|
||||
[MemberNotNullWhen(false, nameof(Value))]
|
||||
public bool IsFailure => !IsSuccess;
|
||||
public override bool IsFailure => base.IsFailure;
|
||||
|
||||
/// <inheritdoc/>
|
||||
[Pure]
|
||||
static Result<T> IResultFactory<Result<T>>.Failure(Error error)
|
||||
{
|
||||
return new Result<T>(error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a string representation of the result.
|
||||
|
|
|
|||
|
|
@ -31,6 +31,21 @@ public static partial class Extensions
|
|||
return (await result).Map(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},Func{T,TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async Task<Result<TNew>> TryMap<T, TNew>(this Task<Result<T>> result, Func<T, TNew> func)
|
||||
{
|
||||
return (await result).TryMap(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},Func{T,TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> MapAsync<T, TNew>(this Task<Result<T>> result, Func<T, Task<TNew>> func)
|
||||
{
|
||||
return await (await result).MapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},Func{T,TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> MapAsync<T, TNew>(this Task<Result<T>> result, Func<T, ValueTask<TNew>> func)
|
||||
|
|
@ -38,6 +53,20 @@ public static partial class Extensions
|
|||
return await (await result).MapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},Func{T,TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> TryMapAsync<T, TNew>(this Task<Result<T>> result, Func<T, Task<TNew>> func)
|
||||
{
|
||||
return await (await result).TryMapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},Func{T,TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> TryMapAsync<T, TNew>(this Task<Result<T>> result, Func<T, ValueTask<TNew>> func)
|
||||
{
|
||||
return await (await result).TryMapAsync(func);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps the success value of the result object of the completed task to a new result using a mapping function, or
|
||||
/// does nothing if the result object of the completed task is a failure.
|
||||
|
|
@ -55,6 +84,21 @@ public static partial class Extensions
|
|||
return (await result).Then(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async Task<Result<TNew>> ThenTry<T, TNew>(this Task<Result<T>> result, Func<T, Result<TNew>> func)
|
||||
{
|
||||
return (await result).ThenTry(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> ThenAsync<T, TNew>(this Task<Result<T>> result, Func<T, Task<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> ThenAsync<T, TNew>(this Task<Result<T>> result, Func<T, ValueTask<Result<TNew>>> func)
|
||||
|
|
@ -62,6 +106,64 @@ public static partial class Extensions
|
|||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> ThenTryAsync<T, TNew>(this Task<Result<T>> result, Func<T, Task<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> ThenTryAsync<T, TNew>(this Task<Result<T>> result, Func<T, ValueTask<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async Task<Result> Then<T>(this Task<Result<T>> result, Func<T, Result> func)
|
||||
{
|
||||
return (await result).Then(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async Task<Result> ThenTry<T>(this Task<Result<T>> result, Func<T, Result> func)
|
||||
{
|
||||
return (await result).ThenTry(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result> ThenAsync<T>(this Task<Result<T>> result, Func<T, Task<Result>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result> ThenAsync<T>(this Task<Result<T>> result, Func<T, ValueTask<Result>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result> ThenTryAsync<T>(this Task<Result<T>> result, Func<T, Task<Result>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result> ThenTryAsync<T>(this Task<Result<T>> result, Func<T, ValueTask<Result>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ValueTask<Result<T>>
|
||||
|
|
@ -74,6 +176,21 @@ public static partial class Extensions
|
|||
return (await result).Map(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},Func{T,TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result<TNew>> TryMap<T, TNew>(this ValueTask<Result<T>> result, Func<T, TNew> func)
|
||||
{
|
||||
return (await result).TryMap(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},Func{T,TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> MapAsync<T, TNew>(this ValueTask<Result<T>> result, Func<T, Task<TNew>> func)
|
||||
{
|
||||
return await (await result).MapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},Func{T,TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> MapAsync<T, TNew>(this ValueTask<Result<T>> result, Func<T, ValueTask<TNew>> func)
|
||||
|
|
@ -81,6 +198,20 @@ public static partial class Extensions
|
|||
return await (await result).MapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},Func{T,TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> TryMapAsync<T, TNew>(this ValueTask<Result<T>> result, Func<T, Task<TNew>> func)
|
||||
{
|
||||
return await (await result).TryMapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{T,TNew}(Task{Result{T}},Func{T,TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> TryMapAsync<T, TNew>(this ValueTask<Result<T>> result, Func<T, ValueTask<TNew>> func)
|
||||
{
|
||||
return await (await result).TryMapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
|
|
@ -89,6 +220,21 @@ public static partial class Extensions
|
|||
return (await result).Then(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result<TNew>> ThenTry<T, TNew>(this ValueTask<Result<T>> result, Func<T, Result<TNew>> func)
|
||||
{
|
||||
return (await result).ThenTry(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> ThenAsync<T, TNew>(this ValueTask<Result<T>> result, Func<T, Task<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> ThenAsync<T, TNew>(this ValueTask<Result<T>> result, Func<T, ValueTask<Result<TNew>>> func)
|
||||
|
|
@ -96,5 +242,358 @@ public static partial class Extensions
|
|||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> ThenTryAsync<T, TNew>(this ValueTask<Result<T>> result, Func<T, Task<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> ThenTryAsync<T, TNew>(this ValueTask<Result<T>> result, Func<T, ValueTask<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result> Then<T>(this ValueTask<Result<T>> result, Func<T, Result> func)
|
||||
{
|
||||
return (await result).Then(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result> ThenTry<T>(this ValueTask<Result<T>> result, Func<T, Result> func)
|
||||
{
|
||||
return (await result).ThenTry(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result> ThenAsync<T>(this ValueTask<Result<T>> result, Func<T, Task<Result>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result> ThenAsync<T>(this ValueTask<Result<T>> result, Func<T, ValueTask<Result>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result> ThenTryAsync<T>(this ValueTask<Result<T>> result, Func<T, Task<Result>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{T,TNew}(Task{Result{T}},Func{T,Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result> ThenTryAsync<T>(this ValueTask<Result<T>> result, Func<T, ValueTask<Result>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Task<Result>
|
||||
|
||||
/// <summary>
|
||||
/// Maps the success of the result object of the completed task to a new success value using a mapping function, or
|
||||
/// does nothing if the result object of the completed task is a failure.
|
||||
/// </summary>
|
||||
/// <param name="result">A task object returning a result object when completing.</param>
|
||||
/// <param name="func">The function used to map the success to a new value.</param>
|
||||
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||
/// <returns>A new result containing either the mapped success value or the failure value of the original
|
||||
/// result.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async Task<Result<TNew>> Map<TNew>(this Task<Result> result, Func<TNew> func)
|
||||
{
|
||||
return (await result).Map(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async Task<Result<TNew>> TryMap<TNew>(this Task<Result> result, Func<TNew> func)
|
||||
{
|
||||
return (await result).TryMap(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> MapAsync<TNew>(this Task<Result> result, Func<Task<TNew>> func)
|
||||
{
|
||||
return await (await result).MapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> MapAsync<TNew>(this Task<Result> result, Func<ValueTask<TNew>> func)
|
||||
{
|
||||
return await (await result).MapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> TryMapAsync<TNew>(this Task<Result> result, Func<Task<TNew>> func)
|
||||
{
|
||||
return await (await result).TryMapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> TryMapAsync<TNew>(this Task<Result> result, Func<ValueTask<TNew>> func)
|
||||
{
|
||||
return await (await result).TryMapAsync(func);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Chains another result to the result object of the completed task if it is a success, or does nothing if the
|
||||
/// result object of the completed task is a failure.
|
||||
/// </summary>
|
||||
/// <param name="result">A task object returning a result object when completing.</param>
|
||||
/// <param name="func">The function used to create the next result.</param>
|
||||
/// <returns>A result which is either the mapped result or a new result containing the failure value of the original
|
||||
/// result.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async Task<Result> Then(this Task<Result> result, Func<Result> func)
|
||||
{
|
||||
return (await result).Then(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async Task<Result> ThenTry(this Task<Result> result, Func<Result> func)
|
||||
{
|
||||
return (await result).ThenTry(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result> ThenAsync(this Task<Result> result, Func<Task<Result>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result> ThenAsync(this Task<Result> result, Func<ValueTask<Result>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result> ThenTryAsync(this Task<Result> result, Func<Task<Result>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result> ThenTryAsync(this Task<Result> result, Func<ValueTask<Result>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Chains a generic result to the result object of the completed task if it is a success, or does nothing if the
|
||||
/// result object of the completed task is a failure.
|
||||
/// </summary>
|
||||
/// <param name="result">A task object returning a result object when completing.</param>
|
||||
/// <param name="func">The function used to create the next generic result.</param>
|
||||
/// <typeparam name="TNew">The type of the new value.</typeparam>
|
||||
/// <returns>A result which is either the mapped result or a new result containing the failure value of the original
|
||||
/// result.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async Task<Result<TNew>> Then<TNew>(this Task<Result> result, Func<Result<TNew>> func)
|
||||
{
|
||||
return (await result).Then(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async Task<Result<TNew>> ThenTry<TNew>(this Task<Result> result, Func<Result<TNew>> func)
|
||||
{
|
||||
return (await result).ThenTry(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> ThenAsync<TNew>(this Task<Result> result, Func<Task<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> ThenAsync<TNew>(this Task<Result> result, Func<ValueTask<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> ThenTryAsync<TNew>(this Task<Result> result, Func<Task<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async Task<Result<TNew>> ThenTryAsync<TNew>(this Task<Result> result, Func<ValueTask<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ValueTask<Result>
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result<TNew>> Map<TNew>(this ValueTask<Result> result, Func<TNew> func)
|
||||
{
|
||||
return (await result).Map(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result<TNew>> TryMap<TNew>(this ValueTask<Result> result, Func<TNew> func)
|
||||
{
|
||||
return (await result).TryMap(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> MapAsync<TNew>(this ValueTask<Result> result, Func<Task<TNew>> func)
|
||||
{
|
||||
return await (await result).MapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> MapAsync<TNew>(this ValueTask<Result> result, Func<ValueTask<TNew>> func)
|
||||
{
|
||||
return await (await result).MapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> TryMapAsync<TNew>(this ValueTask<Result> result, Func<Task<TNew>> func)
|
||||
{
|
||||
return await (await result).TryMapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Map{TNew}(Task{Result},Func{TNew})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> TryMapAsync<TNew>(this ValueTask<Result> result, Func<ValueTask<TNew>> func)
|
||||
{
|
||||
return await (await result).TryMapAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result> Then(this ValueTask<Result> result, Func<Result> func)
|
||||
{
|
||||
return (await result).Then(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result> ThenTry(this ValueTask<Result> result, Func<Result> func)
|
||||
{
|
||||
return (await result).ThenTry(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result> ThenAsync(this ValueTask<Result> result, Func<Task<Result>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result> ThenAsync(this ValueTask<Result> result, Func<ValueTask<Result>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result> ThenTryAsync(this ValueTask<Result> result, Func<Task<Result>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then(Task{Result},Func{Result})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result> ThenTryAsync(this ValueTask<Result> result, Func<ValueTask<Result>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result<TNew>> Then<TNew>(this ValueTask<Result> result, Func<Result<TNew>> func)
|
||||
{
|
||||
return (await result).Then(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result<TNew>> ThenTry<TNew>(this ValueTask<Result> result, Func<Result<TNew>> func)
|
||||
{
|
||||
return (await result).ThenTry(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> ThenAsync<TNew>(this ValueTask<Result> result, Func<Task<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> ThenAsync<TNew>(this ValueTask<Result> result, Func<ValueTask<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> ThenTryAsync<TNew>(this ValueTask<Result> result, Func<Task<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Then{TNew}(Task{Result},Func{Result{TNew}})"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static async ValueTask<Result<TNew>> ThenTryAsync<TNew>(this ValueTask<Result> result, Func<ValueTask<Result<TNew>>> func)
|
||||
{
|
||||
return await (await result).ThenTryAsync(func);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue