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
pub trait AllowIf: Sized {
type Accepted;
fn allow_if<F: FnOnce(&Self::Accepted) -> bool> (self, f: F) -> Option<Self::Accepted>;
fn allow_if_mut<F: FnOnce(&mut Self::Accepted) -> bool> (self, f: F) -> Option<Self::Accepted>;
#[inline]
fn allow_if_not<F: FnOnce(&Self::Accepted) -> bool> (self, f: F) -> Option<Self::Accepted> {
self.allow_if(|x| !f(x))
}
#[inline]
fn allow_if_not_mut<F: FnOnce(&mut Self::Accepted) -> bool> (self, f: F) -> Option<Self::Accepted> {
self.allow_if_mut(|x| !f(x))
}
}
impl<T> AllowIf for Option<T> {
type Accepted = T;
#[inline]
fn allow_if<F: FnOnce(&Self::Accepted) -> bool> (self, f: F) -> Option<T> {
match self {
Some(x) => if f(&x) { Some(x) } else { None },
None => None
}
}
#[inline]
fn allow_if_mut<F: FnOnce(&mut Self::Accepted) -> bool> (self, f: F) -> Option<T> {
match self {
Some(mut x) => if f(&mut x) { Some(x) } else { None },
None => None
}
}
}