63 lines
No EOL
1.7 KiB
C#
63 lines
No EOL
1.7 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using System.Diagnostics.Contracts;
|
|
|
|
namespace Geekeey.Common.Results;
|
|
|
|
public readonly partial struct Result<T>
|
|
{
|
|
/// <summary>
|
|
/// Tries to get the success value from the result.
|
|
/// </summary>
|
|
/// <param name="value">The success value of the result.</param>
|
|
/// <returns>Whether the result has success value.</returns>
|
|
[Pure]
|
|
public bool TryGetValue([MaybeNullWhen(false)] out T value)
|
|
{
|
|
value = Value!;
|
|
|
|
return _success;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tries to get the success value from the result.
|
|
/// </summary>
|
|
/// <param name="value">The success value of the result.</param>
|
|
/// <param name="error">The failure value of the result.</param>
|
|
/// <returns>Whether the result has a success value.</returns>
|
|
[Pure]
|
|
public bool TryGetValue([MaybeNullWhen(false)] out T value, [MaybeNullWhen(true)] out Error error)
|
|
{
|
|
value = Value!;
|
|
error = !_success ? Error : null!;
|
|
|
|
return _success;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tries to get the failure value from the result.
|
|
/// </summary>
|
|
/// <param name="error">The failure value of the result.</param>
|
|
/// <returns>Whether the result has a failure value.</returns>
|
|
[Pure]
|
|
public bool TryGetError([MaybeNullWhen(false)] out Error error)
|
|
{
|
|
error = !_success ? Error : null!;
|
|
|
|
return !_success;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tries to get the failure value from the result.
|
|
/// </summary>
|
|
/// <param name="error">The failure value of the result.</param>
|
|
/// <param name="value">The success value of the result.</param>
|
|
/// <returns>Whether the result a failure value.</returns>
|
|
[Pure]
|
|
public bool TryGetError([MaybeNullWhen(false)] out Error error, [MaybeNullWhen(true)] out T value)
|
|
{
|
|
error = !_success ? Error : null!;
|
|
value = Value!;
|
|
|
|
return !_success;
|
|
}
|
|
} |