aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/either.rs
blob: 71c53ebc066226086530aa26c7140c80121bdf77 (plain)
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
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Either<A, B> {
    A(A),
    B(B),
}

impl<A, B> Either<A, B> {
    pub fn either<R, F1, F2>(self, f1: F1, f2: F2) -> R
    where
        F1: FnOnce(A) -> R,
        F2: FnOnce(B) -> R,
    {
        match self {
            Either::A(a) => f1(a),
            Either::B(b) => f2(b),
        }
    }
    pub fn map<U, V, F1, F2>(self, f1: F1, f2: F2) -> Either<U, V>
    where
        F1: FnOnce(A) -> U,
        F2: FnOnce(B) -> V,
    {
        match self {
            Either::A(a) => Either::A(f1(a)),
            Either::B(b) => Either::B(f2(b)),
        }
    }
    pub fn a(self) -> Option<A> {
        match self {
            Either::A(it) => Some(it),
            Either::B(_) => None,
        }
    }
    pub fn b(self) -> Option<B> {
        match self {
            Either::A(_) => None,
            Either::B(it) => Some(it),
        }
    }
    pub fn as_ref(&self) -> Either<&A, &B> {
        match self {
            Either::A(it) => Either::A(it),
            Either::B(it) => Either::B(it),
        }
    }
}