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
pub trait OptionOps {
type Item;
fn map_none <F> (self , f:F) -> Self where F : FnOnce();
fn map_ref <U,F> (&self , f:F) -> Option<U> where F : FnOnce(&Self::Item) -> U;
fn for_each <U,F> (self , f:F) where F : FnOnce(Self::Item) -> U;
fn for_each_ref <U,F> (&self , f:F) where F : FnOnce(&Self::Item) -> U;
fn contains_if <F> (&self, f:F) -> bool where F : FnOnce(&Self::Item) -> bool;
}
impl<T> OptionOps for Option<T> {
type Item = T;
fn map_none<F>(self, f:F) -> Self
where F:FnOnce(), T:Sized {
if self.is_none() { f() }
self
}
fn map_ref<U,F>(&self, f:F) -> Option<U> where F : FnOnce(&Self::Item) -> U {
self.as_ref().map(f)
}
fn for_each<U,F>(self, f:F) where F : FnOnce(Self::Item) -> U {
if let Some(x) = self { f(x); }
}
fn for_each_ref<U,F>(&self, f:F) where F : FnOnce(&Self::Item) -> U {
if let Some(x) = self { f(x); }
}
fn contains_if<F>(&self, f:F) -> bool where F : FnOnce(&Self::Item) -> bool {
self.as_ref().map_or(false,f)
}
}