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 With: Sized {
fn wrap_with<U, F: FnOnce(Self) -> U>(self, f: F) -> U {
f(self)
}
#[must_use]
fn with<F: FnOnce(&mut Self)>(mut self, f: F) -> Self {
f(&mut self);
self
}
fn try_with<E, F>(mut self, f: F) -> Result<Self, E>
where
F: FnOnce(&mut Self) -> Result<(), E>,
{
f(&mut self)?;
Ok(self)
}
#[must_use]
fn with_if<F>(mut self, condition: bool, f: F) -> Self
where
F: FnOnce(&mut Self),
{
if condition {
f(&mut self);
}
self
}
}
impl<T: Sized> With for T {}