Barotrauma Client Doc
Result.cs
1 #nullable enable
2 using System;
3 using System.Diagnostics.CodeAnalysis;
4 
5 namespace Barotrauma
6 {
7  public abstract class Result<T, TError>
8  where T: notnull
9  where TError: notnull
10  {
11  public abstract bool IsSuccess { get; }
12  public bool IsFailure => !IsSuccess;
13 
14  public static Result<T, TError> Success(T value)
15  => new Success<T, TError>(value);
16 
17  public static Result<T, TError> Failure(TError error)
18  => new Failure<T, TError>(error);
19 
20  public abstract bool TryUnwrapSuccess([MaybeNullWhen(returnValue: false)] out T value);
21  public abstract bool TryUnwrapFailure([MaybeNullWhen(returnValue: false)] out TError value);
22 
23  public abstract override string? ToString();
24 
25  public static (Func<T, Result<T, TError>> Success, Func<TError, Result<T, TError>> Failure) GetFactoryMethods()
26  => (Success, Failure);
27  }
28 
29  public sealed class Success<T, TError> : Result<T, TError>
30  where T: notnull
31  where TError: notnull
32  {
33  public readonly T Value;
34  public override bool IsSuccess => true;
35 
36  public override bool TryUnwrapSuccess([MaybeNullWhen(returnValue: false)] out T value)
37  {
38  value = Value;
39  return true;
40  }
41 
42  public override bool TryUnwrapFailure([MaybeNullWhen(returnValue: false)] out TError value)
43  {
44  value = default;
45  return false;
46  }
47 
48  public override string ToString()
49  => $"Success<{typeof(T).NameWithGenerics()}, {typeof(TError).NameWithGenerics()}>({Value})";
50 
51  public Success(T value)
52  {
53  Value = value;
54  }
55  }
56 
57  public sealed class Failure<T, TError> : Result<T, TError>
58  where T: notnull
59  where TError: notnull
60  {
61  public readonly TError Error;
62 
63  public override bool IsSuccess => false;
64 
65  public override bool TryUnwrapSuccess([MaybeNullWhen(returnValue: false)] out T value)
66  {
67  value = default;
68  return false;
69  }
70 
71  public override bool TryUnwrapFailure([MaybeNullWhen(returnValue: false)] out TError value)
72  {
73  value = Error;
74  return true;
75  }
76 
77  public override string ToString()
78  => $"Failure<{typeof(T).NameWithGenerics()}, {typeof(TError).NameWithGenerics()}>({Error})";
79 
80  public Failure(TError error)
81  {
82  Error = error;
83  }
84  }
85 }
override bool TryUnwrapFailure([MaybeNullWhen(returnValue:false)] out TError value)
Definition: Result.cs:71
Failure(TError error)
Definition: Result.cs:80
readonly TError Error
Definition: Result.cs:61
override bool TryUnwrapSuccess([MaybeNullWhen(returnValue:false)] out T value)
Definition: Result.cs:65
override string ToString()
override bool IsSuccess
Definition: Result.cs:63
static Result< T, TError > Failure(TError error)
abstract bool TryUnwrapFailure([MaybeNullWhen(returnValue:false)] out TError value)
static Func< T, Result< T, TError > > Success
Definition: Result.cs:25
abstract bool TryUnwrapSuccess([MaybeNullWhen(returnValue:false)] out T value)
bool IsFailure
Definition: Result.cs:12
static Result< T, TError > Success(T value)
abstract bool IsSuccess
Definition: Result.cs:11
abstract override? string ToString()
override string ToString()
override bool TryUnwrapSuccess([MaybeNullWhen(returnValue:false)] out T value)
Definition: Result.cs:36
override bool IsSuccess
Definition: Result.cs:34
Success(T value)
Definition: Result.cs:51
readonly T Value
Definition: Result.cs:33
override bool TryUnwrapFailure([MaybeNullWhen(returnValue:false)] out TError value)
Definition: Result.cs:42