All checks were successful
default / dotnet default workflow (8.0) (push) Successful in 39s
39 lines
No EOL
1.4 KiB
C#
39 lines
No EOL
1.4 KiB
C#
// Copyright (c) The Geekeey Authors
|
|
// SPDX-License-Identifier: EUPL-1.2
|
|
|
|
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();
|
|
} |