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
use crate::applicative::Applicative;
pub trait Alternative: Applicative {
fn empty() -> Self::Wrapped<Self::Inner>;
fn mirror(
l: Self::Wrapped<Self::Inner>,
r: Self::Wrapped<Self::Inner>,
) -> Self::Wrapped<Self::Inner>;
}
impl<A> Alternative for Option<A> {
fn empty() -> Self::Wrapped<Self::Inner> {
None
}
fn mirror(
l: Self::Wrapped<Self::Inner>,
r: Self::Wrapped<Self::Inner>,
) -> Self::Wrapped<Self::Inner> {
match (l, r) {
(None, r) => r,
(l, _) => l,
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn alt_opt() {
let l = Some(4u8);
let r = None;
assert_eq!(<Option<u8> as Alternative>::empty(), None);
assert_eq!(<Option<u8> as Alternative>::mirror(l, r), l);
}
}