1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#![forbid(unsafe_code)]

use core::fmt::{Debug, Display, Formatter};

pub enum OptionAbc<A, B, C> {
    A(A),
    B(B),
    C(C),
}

impl<T> OptionAbc<T, T, T> {
    pub fn take(self) -> T {
        match self {
            OptionAbc::A(value) | OptionAbc::B(value) | OptionAbc::C(value) => value,
        }
    }
}

impl<A, B, C> OptionAbc<A, B, C> {
    pub fn as_ref(&self) -> OptionAbc<&A, &B, &C> {
        match self {
            OptionAbc::A(value) => OptionAbc::A(&value),
            OptionAbc::B(value) => OptionAbc::B(&value),
            OptionAbc::C(value) => OptionAbc::C(&value),
        }
    }
    pub fn a(&self) -> Option<&A> {
        match self {
            OptionAbc::A(value) => Some(value),
            _ => None,
        }
    }
    pub fn b(&self) -> Option<&B> {
        match self {
            OptionAbc::B(value) => Some(value),
            _ => None,
        }
    }
    pub fn c(&self) -> Option<&C> {
        match self {
            OptionAbc::C(value) => Some(value),
            _ => None,
        }
    }
}
impl<A: Debug, B: Debug, C: Debug> OptionAbc<A, B, C> {
    /// # Panics
    /// Panics if `self` is not an `OptionABC::A`.
    pub fn unwrap_a(self) -> A {
        match self {
            OptionAbc::A(value) => value,
            _ => panic!("expected OptionABC::A(_) but found {:?}", self),
        }
    }
    /// # Panics
    /// Panics if `self` is not an `OptionABC::B`.
    pub fn unwrap_b(self) -> B {
        match self {
            OptionAbc::B(value) => value,
            _ => panic!("expected OptionABC::B(_) but found {:?}", self),
        }
    }
    /// # Panics
    /// Panics if `self` is not an `OptionABC::C`.
    pub fn unwrap_c(self) -> C {
        match self {
            OptionAbc::C(value) => value,
            _ => panic!("expected OptionABC::C(_) but found {:?}", self),
        }
    }
}

impl<A: Clone, B: Clone, C: Clone> Clone for OptionAbc<A, B, C> {
    fn clone(&self) -> Self {
        match self {
            OptionAbc::A(value) => OptionAbc::A(value.clone()),
            OptionAbc::B(value) => OptionAbc::B(value.clone()),
            OptionAbc::C(value) => OptionAbc::C(value.clone()),
        }
    }
}

impl<A: Debug, B: Debug, C: Debug> Debug for OptionAbc<A, B, C> {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
        match self {
            OptionAbc::A(value) => write!(f, "OptionABC::A({:?})", value),
            OptionAbc::B(value) => write!(f, "OptionABC::B({:?})", value),
            OptionAbc::C(value) => write!(f, "OptionABC::C({:?})", value),
        }
    }
}

impl<A: Display, B: Display, C: Display> Display for OptionAbc<A, B, C> {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
        match self {
            OptionAbc::A(value) => write!(f, "{}", value),
            OptionAbc::B(value) => write!(f, "{}", value),
            OptionAbc::C(value) => write!(f, "{}", value),
        }
    }
}

impl<A: PartialEq, B: PartialEq, C: PartialEq> PartialEq for OptionAbc<A, B, C> {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (OptionAbc::A(value), OptionAbc::A(other)) if value == other => true,
            (OptionAbc::B(value), OptionAbc::B(other)) if value == other => true,
            (OptionAbc::C(value), OptionAbc::C(other)) if value == other => true,
            _ => false,
        }
    }
}
impl<A: PartialEq, B: PartialEq, C: PartialEq> Eq for OptionAbc<A, B, C> {}

#[cfg(test)]
mod tests {
    use super::*;

    #[allow(clippy::type_complexity)]
    fn test_values() -> (
        OptionAbc<bool, u8, &'static str>,
        OptionAbc<bool, u8, &'static str>,
        OptionAbc<bool, u8, &'static str>,
    ) {
        (OptionAbc::A(true), OptionAbc::B(42), OptionAbc::C("s1"))
    }

    #[test]
    fn test_as_ref() {
        let (a, b, c) = test_values();
        let _: OptionAbc<&bool, &u8, &&'static str> = a.as_ref();
        let _: &u8 = b.as_ref().unwrap_b();
        let _: &&'static str = c.as_ref().unwrap_c();
        assert_eq!(true, *(a.as_ref().unwrap_a()));
        assert_eq!(42_u8, *(b.as_ref().unwrap_b()));
        assert_eq!("s1", *(c.as_ref().unwrap_c()));
    }

    #[test]
    fn test_accessors() {
        let (a, b, c) = test_values();
        assert_eq!(true, *(a.a().unwrap()));
        assert_eq!(None, a.b());
        assert_eq!(None, a.c());

        assert_eq!(None, b.a());
        assert_eq!(42_u8, *(b.b().unwrap()));
        assert_eq!(None, b.c());

        assert_eq!(None, c.a());
        assert_eq!(None, c.b());
        assert_eq!("s1", *(c.c().unwrap()));
    }

    #[test]
    fn test_debug() {
        let (a, b, c) = test_values();
        assert_eq!("OptionABC::A(true)", format!("{:?}", a));
        assert_eq!("OptionABC::B(42)", format!("{:?}", b));
        assert_eq!("OptionABC::C(\"s1\")", format!("{:?}", c));
    }

    #[test]
    fn test_display() {
        let (a, b, c) = test_values();
        assert_eq!("true", format!("{}", a));
        assert_eq!("42", format!("{}", b));
        assert_eq!("s1", format!("{}", c));
    }

    #[test]
    fn test_eq() {
        let (a, b, c) = test_values();
        assert_eq!(OptionAbc::A(true), a);
        assert_ne!(OptionAbc::A(false), a);
        assert_eq!(OptionAbc::B(42_u8), b);
        assert_ne!(OptionAbc::B(2_u8), b);
        assert_eq!(OptionAbc::C("s1"), c);
        assert_ne!(OptionAbc::C("other"), c);
        assert_ne!(a, b);
        assert_ne!(a, c);
        assert_ne!(b, c);
    }

    #[test]
    fn test_unwrap() {
        let (a, b, c) = test_values();
        let a_clone = a.clone();
        assert_eq!(true, a_clone.unwrap_a());
        let a_clone = a.clone();
        assert_eq!(
            "expected OptionABC::B(_) but found OptionABC::A(true)",
            std::panic::catch_unwind(|| a_clone.unwrap_b())
                .unwrap_err()
                .downcast::<String>()
                .unwrap()
                .as_str()
        );
        let a_clone = a.clone();
        assert_eq!(
            "expected OptionABC::C(_) but found OptionABC::A(true)",
            std::panic::catch_unwind(|| a_clone.unwrap_c())
                .unwrap_err()
                .downcast::<String>()
                .unwrap()
                .as_str()
        );

        let b_clone = b.clone();
        assert_eq!(
            "expected OptionABC::A(_) but found OptionABC::B(42)",
            std::panic::catch_unwind(|| b_clone.unwrap_a())
                .unwrap_err()
                .downcast::<String>()
                .unwrap()
                .as_str()
        );
        let b_clone = b.clone();
        assert_eq!(42_u8, b_clone.unwrap_b());
        let b_clone = b.clone();
        std::panic::catch_unwind(|| b_clone.unwrap_c()).unwrap_err();

        let c_clone = c.clone();
        std::panic::catch_unwind(|| c_clone.unwrap_a()).unwrap_err();
        let c_clone = c.clone();
        std::panic::catch_unwind(|| c_clone.unwrap_b()).unwrap_err();
        let c_clone = c.clone();
        assert_eq!("s1", c_clone.unwrap_c());
    }

    #[test]
    fn test_take() {
        let same_a: OptionAbc<u8, u8, u8> = OptionAbc::A(42_u8);
        let same_b: OptionAbc<u8, u8, u8> = OptionAbc::B(42_u8);
        let same_c: OptionAbc<u8, u8, u8> = OptionAbc::C(42_u8);
        assert_eq!(42_u8, same_a.take());
        assert_eq!(42_u8, same_b.take());
        assert_eq!(42_u8, same_c.take());
    }
}