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
pub trait OptionExt<V> {
fn update_with<S, F, R>(&mut self, f: F) -> R
where
F: FnOnce(&mut Option<S>) -> R,
V: TryInto<S>,
S: TryInto<V>;
fn initialized(&mut self) -> &mut Self
where
V: Default;
}
impl<V> OptionExt<V> for Option<V> {
fn update_with<S, F, R>(&mut self, f: F) -> R
where
F: FnOnce(&mut Option<S>) -> R,
V: TryInto<S>,
S: TryInto<V>,
{
let mut entity = self.take().map(TryInto::try_into).and_then(Result::ok);
let res = f(&mut entity);
*self = entity.map(TryInto::try_into).and_then(Result::ok);
res
}
fn initialized(&mut self) -> &mut Self
where
V: Default,
{
if self.is_none() {
self.replace(Default::default());
}
self
}
}