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
#[cfg(feature = "either")]
pub use either::Either;
pub use maybe_owned::MaybeOwned;
mod callbacks;
pub(crate) use crate::types::callbacks::Callbacks;
mod storage;
pub(crate) use crate::types::storage::Storage;
pub trait SumType2 {
type Type1;
type Type2;
fn from_type1(val: Self::Type1) -> Self;
fn from_type2(val: Self::Type2) -> Self;
fn is_type1(&self) -> bool;
fn is_type2(&self) -> bool;
fn into_type1(self) -> Option<Self::Type1>;
fn into_type2(self) -> Option<Self::Type2>;
}
impl<T> SumType2 for Option<T> {
type Type1 = T;
type Type2 = ();
fn from_type1(val: Self::Type1) -> Self {
Some(val)
}
fn from_type2(_: Self::Type2) -> Self {
None
}
fn is_type1(&self) -> bool {
self.is_some()
}
fn is_type2(&self) -> bool {
self.is_none()
}
fn into_type1(self) -> Option<Self::Type1> {
self
}
fn into_type2(self) -> Option<Self::Type2> {
self.ok_or(()).err()
}
}
impl<T, E> SumType2 for Result<T, E> {
type Type1 = T;
type Type2 = E;
fn from_type1(val: Self::Type1) -> Self {
Ok(val)
}
fn from_type2(val: Self::Type2) -> Self {
Err(val)
}
fn is_type1(&self) -> bool {
self.is_ok()
}
fn is_type2(&self) -> bool {
self.is_err()
}
fn into_type1(self) -> Option<Self::Type1> {
self.ok()
}
fn into_type2(self) -> Option<Self::Type2> {
self.err()
}
}
#[cfg(feature = "either")]
impl<L, R> SumType2 for Either<L, R> {
type Type1 = L;
type Type2 = R;
fn from_type1(val: Self::Type1) -> Self {
Either::Left(val)
}
fn from_type2(val: Self::Type2) -> Self {
Either::Right(val)
}
fn is_type1(&self) -> bool {
self.is_left()
}
fn is_type2(&self) -> bool {
self.is_right()
}
fn into_type1(self) -> Option<Self::Type1> {
self.left()
}
fn into_type2(self) -> Option<Self::Type2> {
self.right()
}
}
pub trait ObserveResult {
fn is_callback_alive(self) -> bool;
}
impl ObserveResult for () {
fn is_callback_alive(self) -> bool {
true
}
}
impl ObserveResult for bool {
fn is_callback_alive(self) -> bool {
self
}
}
impl<T> ObserveResult for Option<T> {
fn is_callback_alive(self) -> bool {
self.is_some()
}
}
impl<T, E> ObserveResult for Result<T, E> {
fn is_callback_alive(self) -> bool {
self.is_ok()
}
}