Barotrauma Client Doc
Either.cs
1 #nullable enable
2 using System;
3 
4 namespace Barotrauma
5 {
6  public abstract class Either<T, U> where T : notnull where U : notnull
7  {
8  public static implicit operator Either<T, U>(T t) => new EitherT<T, U>(t);
9  public static implicit operator Either<T, U>(U u) => new EitherU<T, U>(u);
10 
11  public static explicit operator T(Either<T, U> e) => e.TryGet(out T t) ? t : throw new InvalidCastException($"Contained object is not of type {typeof(T).Name}");
12  public static explicit operator U(Either<T, U> e) => e.TryGet(out U u) ? u : throw new InvalidCastException($"Contained object is not of type {typeof(U).Name}");
13 
14  public abstract bool TryGet(out T t);
15  public abstract bool TryGet(out U u);
16 
17  public abstract bool TryCast<V>(out V v);
18 
19  public abstract override string? ToString();
20 
21  public abstract override bool Equals(object? obj);
22 
23  public abstract override int GetHashCode();
24 
25  public static bool operator ==(Either<T, U>? a, Either<T, U>? b)
26  => a is null ? b is null : a.Equals(b);
27 
28  public static bool operator !=(Either<T, U>? a, Either<T, U>? b)
29  => !(a == b);
30  }
31 
32  public sealed class EitherT<T, U> : Either<T, U> where T : notnull where U : notnull
33  {
34  public readonly T Value;
35 
36  public EitherT(T value) { Value = value; }
37 
38  public override string? ToString()
39  => $"Either<{typeof(T).NameWithGenerics()}, {typeof(U).NameWithGenerics()}>({Value}: {typeof(T).NameWithGenerics()})";
40 
41  public override bool TryGet(out T t) { t = Value; return true; }
42  public override bool TryGet(out U u) { u = default!; return false; }
43 
44  public override bool TryCast<V>(out V v)
45  {
46  if (Value is V result)
47  {
48  v = result;
49  return true;
50  }
51  else
52  {
53  v = default!;
54  return false;
55  }
56  }
57 
58  public override bool Equals(object? obj)
59  => obj switch
60  {
61  EitherT<T, U> other => Value.Equals(other.Value),
62  T value => Value.Equals(value),
63  _ => false
64  };
65 
66  public override int GetHashCode() => Value.GetHashCode();
67  }
68 
69  public sealed class EitherU<T, U> : Either<T, U> where T : notnull where U : notnull
70  {
71  public readonly U Value;
72 
73  public EitherU(U value) { Value = value; }
74 
75  public override string? ToString()
76  => $"Either<{typeof(T).NameWithGenerics()}, {typeof(U).NameWithGenerics()}>({Value}: {typeof(U).NameWithGenerics()})";
77 
78  public override bool TryGet(out T t) { t = default!; return false; }
79  public override bool TryGet(out U u) { u = Value; return true; }
80 
81  public override bool TryCast<V>(out V v)
82  {
83  if (Value is V result)
84  {
85  v = result;
86  return true;
87  }
88  else
89  {
90  v = default!;
91  return false;
92  }
93  }
94 
95  public override bool Equals(object? obj)
96  => obj switch
97  {
98  EitherU<T, U> other => Value.Equals(other.Value),
99  U value => Value.Equals(value),
100  _ => false
101  };
102 
103  public override int GetHashCode() => Value.GetHashCode();
104  }
105 }
abstract override int GetHashCode()
abstract bool TryCast< V >(out V v)
abstract override bool Equals(object? obj)
abstract override? string ToString()
static bool operator==(Either< T, U >? a, Either< T, U >? b)
abstract bool TryGet(out U u)
abstract bool TryGet(out T t)
static bool operator!=(Either< T, U >? a, Either< T, U >? b)
override? string ToString()
override bool TryGet(out T t)
Definition: Either.cs:41
readonly T Value
Definition: Either.cs:34
override int GetHashCode()
EitherT(T value)
Definition: Either.cs:36
override bool Equals(object? obj)
override bool TryCast< V >(out V v)
Definition: Either.cs:44
override bool TryGet(out U u)
Definition: Either.cs:42
override int GetHashCode()
readonly U Value
Definition: Either.cs:71
override bool TryCast< V >(out V v)
Definition: Either.cs:81
override bool TryGet(out T t)
Definition: Either.cs:78
EitherU(U value)
Definition: Either.cs:73
override? string ToString()
override bool TryGet(out U u)
Definition: Either.cs:79
override bool Equals(object? obj)