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
|
//! This crate adds the missing Option::inspect function via an extension trait
//!
/// Extension trait for adding Option::inspect
pub trait OptionInspect<F, T>
where
F: FnOnce(&T),
T: Sized,
{
/// Inspect the Option
///
/// Either call `f` on the value in `Some` or do nothing if this Option is a None.
fn inspect(self, f: F) -> Self;
}
pub trait OptionInspectRef<F, T>
where
F: FnOnce(&T),
T: Sized,
{
fn inspect(&self, f: F);
}
impl<F, T> OptionInspect<F, T> for Option<T>
where
F: FnOnce(&T),
T: Sized,
{
fn inspect(self, f: F) -> Self {
if let Some(o) = self.as_ref() {
(f)(o);
}
self
}
}
impl<F, T> OptionInspectRef<F, T> for Option<T>
where
F: FnOnce(&T),
T: Sized,
{
fn inspect(&self, f: F) {
if let Some(ref o) = self {
(f)(o);
}
}
}
/// Extension trait for adding Option::inspect_none
pub trait OptionInspectNone<F>
where
F: FnOnce(),
{
/// Call `f` if the Option this is called on is a None
fn inspect_none(self, f: F) -> Self;
}
pub trait OptionInspectNoneRef<F>
where
F: FnOnce(),
{
fn inspect_none(&self, f: F);
}
impl<F, T> OptionInspectNone<F> for Option<T>
where
F: FnOnce(),
{
fn inspect_none(self, f: F) -> Self {
if self.is_none() {
(f)();
}
self
}
}
impl<F, T> OptionInspectNoneRef<F> for Option<T>
where
F: FnOnce(),
{
fn inspect_none(&self, f: F) {
if self.is_none() {
(f)();
}
}
}
|