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
#![forbid(unsafe_code)]

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

pub enum OptionAB<A, B> {
    A(A),
    B(B),
}

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

impl<A, B> OptionAB<A, B> {
    pub fn as_ref(&self) -> OptionAB<&A, &B> {
        match self {
            OptionAB::A(value) => OptionAB::A(&value),
            OptionAB::B(value) => OptionAB::B(&value),
        }
    }
    pub fn a(&self) -> Option<&A> {
        match self {
            OptionAB::A(value) => Some(value),
            _ => None,
        }
    }
    pub fn b(&self) -> Option<&B> {
        match self {
            OptionAB::B(value) => Some(value),
            _ => None,
        }
    }
}

impl<A: Debug, B: Debug> OptionAB<A, B> {
    pub fn unwrap_a(self) -> A {
        match self {
            OptionAB::A(value) => value,
            _ => panic!("expected OptionAB::A(_) but found {:?}", self),
        }
    }
    pub fn unwrap_b(self) -> B {
        match self {
            OptionAB::B(value) => value,
            _ => panic!("expected OptionAB::B(_) but found {:?}", self),
        }
    }
}

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

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

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

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

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

    #[test]
    fn test() {
        let a: OptionAB<bool, u8> = OptionAB::A(true);
        let b: OptionAB<bool, u8> = OptionAB::B(42);
        // Test as_ref()
        let _: OptionAB<&bool, &u8> = a.as_ref();
        let _: &u8 = b.as_ref().unwrap_b();
        assert_eq!(true, *(a.as_ref().unwrap_a()));
        assert_eq!(42u8, *(b.as_ref().unwrap_b()));
        // Test accessors
        assert_eq!(true, *(a.a().unwrap()));
        assert_eq!(None, a.b());
        assert_eq!(None, b.a());
        assert_eq!(42u8, *(b.b().unwrap()));
        // Test Debug
        assert_eq!("OptionAB::A(true)", format!("{:?}", a));
        assert_eq!("OptionAB::B(42)", format!("{:?}", b));
        // Test Display
        assert_eq!("true", format!("{}", a));
        assert_eq!("42", format!("{}", b));
        // Test Eq
        assert_eq!(OptionAB::A(true), a);
        assert_ne!(OptionAB::A(false), a);
        assert_eq!(OptionAB::B(42u8), b);
        assert_ne!(OptionAB::B(2u8), b);
        assert_ne!(a, b);
        // Test unwrappers
        let a_clone = a.clone();
        assert_eq!(true, a_clone.unwrap_a());
        let a_clone = a.clone();
        assert_eq!(
            "expected OptionAB::B(_) but found OptionAB::A(true)",
            std::panic::catch_unwind(|| a_clone.unwrap_b())
                .unwrap_err()
                .downcast::<String>()
                .unwrap()
                .as_str()
        );

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

        // Test take()
        let same_a: OptionAB<u8, u8> = OptionAB::A(42u8);
        let same_b: OptionAB<u8, u8> = OptionAB::B(42u8);
        assert_eq!(42u8, same_a.take());
        assert_eq!(42u8, same_b.take());
    }
}