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
use EitherOrBoth::*;
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum EitherOrBoth<A, B> {
Both(A, B),
Left(A),
Right(B),
}
impl<A, B> EitherOrBoth<A, B> {
pub fn has_left(&self) -> bool {
self.as_ref().left().is_some()
}
pub fn has_right(&self) -> bool {
self.as_ref().right().is_some()
}
pub fn left(self) -> Option<A> {
match self {
Left(left) | Both(left, _) => Some(left),
_ => None
}
}
pub fn right(self) -> Option<B> {
match self {
Right(right) | Both(_, right) => Some(right),
_ => None
}
}
pub fn as_ref(&self) -> EitherOrBoth<&A, &B> {
match *self {
Left(ref left) => Left(left),
Right(ref right) => Right(right),
Both(ref left, ref right) => Both(left, right),
}
}
pub fn as_mut(&mut self) -> EitherOrBoth<&mut A, &mut B> {
match *self {
Left(ref mut left) => Left(left),
Right(ref mut right) => Right(right),
Both(ref mut left, ref mut right) => Both(left, right),
}
}
}