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
pub trait ResultEffect<T> {
fn when_ok<F: FnOnce()>(self, f: F) -> Self;
fn when_err<F: FnOnce()>(self, f: F) -> Self;
}
impl<T, E> ResultEffect<T> for Result<T, E> {
fn when_ok<F: FnOnce()>(self, f: F) -> Self {
if self.is_ok() {
f();
}
self
}
fn when_err<F: FnOnce()>(self, f: F) -> Self {
if self.is_err() {
f();
}
self
}
}
pub trait OptionEffect<T> {
fn when_none<F: FnOnce()>(self, f: F) -> Self;
fn when_some<F: FnOnce()>(self, f: F) -> Self;
}
impl<T> OptionEffect<T> for Option<T> {
fn when_none<F: FnOnce()>(self, f: F) -> Self {
if self.is_none() {
f();
}
self
}
fn when_some<F: FnOnce()>(self, f: F) -> Self {
if self.is_some() {
f();
}
self
}
}
#[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)]
pub enum Either<L, R> {
Left(L),
Right(R),
}
impl<L, R> Either<L, R> {
pub fn map_left<F, M>(self, f: F) -> Either<M, R>
where F: FnOnce(L) -> M
{
use self::Either::*;
match self {
Left(l) => Left(f(l)),
Right(r) => Right(r),
}
}
pub fn map_right<F, S>(self, f: F) -> Either<L, S>
where F: FnOnce(R) -> S
{
use self::Either::*;
match self {
Left(l) => Left(l),
Right(r) => Right(f(r)),
}
}
}