All checks were successful
default / default (8.0) (push) Successful in 1m8s
115 lines
No EOL
4.5 KiB
C#
115 lines
No EOL
4.5 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using System.Diagnostics.Contracts;
|
|
using System.Numerics;
|
|
|
|
namespace Geekeey.Extensions.Result;
|
|
|
|
public readonly partial struct Result<T> : IEquatable<Result<T>>, IEquatable<T>,
|
|
IEqualityOperators<Result<T>, Result<T>, bool>, IEqualityOperators<Result<T>, T, bool>
|
|
{
|
|
/// <summary>
|
|
/// Checks whether the result is equal to another result. Results are equal if both results are success values and
|
|
/// the success values are equal, or if both results are failures.
|
|
/// </summary>
|
|
/// <param name="other">The result to check for equality with the current result.</param>
|
|
[Pure]
|
|
public bool Equals(Result<T> other)
|
|
=> Equals(this, other, EqualityComparer<T>.Default);
|
|
|
|
/// <summary>
|
|
/// Checks whether the result is equal to another result. Results are equal if both results are success values and
|
|
/// the success values are equal, or if both results are failures.
|
|
/// </summary>
|
|
/// <param name="other">The result to check for equality with the current result.</param>
|
|
/// <param name="comparer">The equality comparer to use for comparing values.</param>
|
|
[Pure]
|
|
public bool Equals(Result<T> other, IEqualityComparer<T> comparer)
|
|
=> Equals(this, other, comparer);
|
|
|
|
/// <summary>
|
|
/// Checks whether the result is a success value and the success value is equal to another value.
|
|
/// </summary>
|
|
/// <param name="other">The value to check for equality with the success value of the result.</param>
|
|
[Pure]
|
|
public bool Equals(T? other)
|
|
=> Equals(this, other, EqualityComparer<T>.Default);
|
|
|
|
/// <summary>
|
|
/// Checks whether the result is a success value and the success value is equal to another value using a specified
|
|
/// equality comparer.
|
|
/// </summary>
|
|
/// <param name="other">The value to check for equality with the success value of the result.</param>
|
|
/// <param name="comparer">The equality comparer to use for comparing values.</param>
|
|
[Pure]
|
|
public bool Equals(T? other, IEqualityComparer<T> comparer)
|
|
=> Equals(this, other, comparer);
|
|
|
|
/// <inheritdoc/>
|
|
[Pure]
|
|
public override bool Equals(object? other)
|
|
=> other is T x && Equals(x) || other is Result<T> r && Equals(r);
|
|
|
|
/// <inheritdoc/>
|
|
[Pure]
|
|
public override int GetHashCode()
|
|
=> GetHashCode(this, EqualityComparer<T>.Default);
|
|
|
|
internal static bool Equals(Result<T> a, Result<T> b, IEqualityComparer<T> comparer)
|
|
{
|
|
if (!a._success || !b._success) return !a._success && !b._success;
|
|
if (a.Value is null || b.Value is null) return a.Value is null && b.Value is null;
|
|
return comparer.Equals(a.Value, b.Value);
|
|
}
|
|
|
|
internal static bool Equals(Result<T> a, T? b, IEqualityComparer<T> comparer)
|
|
{
|
|
if (!a._success) return false;
|
|
if (a.Value is null || b is null) return a.Value is null && b is null;
|
|
return comparer.Equals(a.Value, b);
|
|
}
|
|
|
|
internal static int GetHashCode(Result<T> result, IEqualityComparer<T> comparer)
|
|
{
|
|
if (result is { _success: true, Value: not null })
|
|
return comparer.GetHashCode(result.Value);
|
|
return 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks whether two results are equal. Results are equal if both results are success values and the success
|
|
/// values are equal, or if both results are failures.
|
|
/// </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<T> a, Result<T> b) => a.Equals(b);
|
|
|
|
/// <summary>
|
|
/// Checks whether two results are not equal. Results are equal if both results are success values and the success
|
|
/// values are equal, or if both results are failures.
|
|
/// </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<T> a, Result<T> b) => !a.Equals(b);
|
|
|
|
/// <summary>
|
|
/// Checks whether a result is a success value and the success value is equal to another value.
|
|
/// </summary>
|
|
/// <param name="a">The result to compare.</param>
|
|
/// <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) => a.Equals(b);
|
|
|
|
/// <summary>
|
|
/// Checks whether a result either does not have a value, or the value is not equal to another value.
|
|
/// </summary>
|
|
/// <param name="a">The result to compare.</param>
|
|
/// <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) => !a.Equals(b);
|
|
} |