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
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AllOrSome<T> {
All,
Some(T),
}
impl<T> Default for AllOrSome<T> {
fn default() -> Self {
AllOrSome::All
}
}
impl<T> AllOrSome<T> {
pub fn is_all(&self) -> bool {
matches!(self, AllOrSome::All)
}
#[allow(dead_code)]
pub fn is_some(&self) -> bool {
!self.is_all()
}
pub fn as_ref(&self) -> Option<&T> {
match *self {
AllOrSome::All => None,
AllOrSome::Some(ref t) => Some(t),
}
}
pub fn as_mut(&mut self) -> Option<&mut T> {
match *self {
AllOrSome::All => None,
AllOrSome::Some(ref mut t) => Some(t),
}
}
}
#[cfg(test)]
#[test]
fn tests() {
assert!(AllOrSome::<()>::All.is_all());
assert!(!AllOrSome::<()>::All.is_some());
assert!(!AllOrSome::Some(()).is_all());
assert!(AllOrSome::Some(()).is_some());
}