result/src/Result/Result.Conversion.cs
Louis Seubert 3457f4e692
All checks were successful
default / default (8.0) (push) Successful in 1m8s
feat: initial project commit
2024-04-26 20:40:08 +02:00

36 lines
No EOL
1.3 KiB
C#

using System.Diagnostics.Contracts;
namespace Geekeey.Extensions.Result;
public readonly partial struct Result<T>
{
/// <summary>
/// Implicitly constructs a result from a success value.
/// </summary>
/// <param name="value">The value to construct the result from.</param>
[Pure]
public static implicit operator Result<T>(T value) => new(value);
/// <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<T>(Error error) => new(error);
/// <summary>
/// Unwraps the success value of the result. Throws an <see cref="UnwrapException"/> if the result is a failure.
/// </summary>
/// <remarks>
/// This call is <b>unsafe</b> in the sense that it might intentionally throw an exception. Please only use this
/// call if the caller knows that this operation is safe, or that an exception is acceptable to be thrown.
/// </remarks>
/// <returns>The success value of the result.</returns>
/// <exception cref="UnwrapException">The result is not a success.</exception>
[Pure]
public T Unwrap() => IsSuccess ? Value! : throw new UnwrapException();
/// <inheritdoc cref="Unwrap"/>
[Pure]
public static explicit operator T(Result<T> result) => result.Unwrap();
}