2016-07-16 20:25:21 +00:00
|
|
|
/// Generic trait to enable chainable API
|
|
|
|
pub trait With: Sized {
|
|
|
|
/// Calls the given closure on `self`.
|
|
|
|
fn with<F: FnOnce(&mut Self)>(mut self, f: F) -> Self {
|
|
|
|
f(&mut self);
|
|
|
|
self
|
|
|
|
}
|
2018-01-10 22:58:29 +00:00
|
|
|
|
|
|
|
/// Calls the given closure on `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)
|
|
|
|
}
|
2018-04-10 18:46:02 +00:00
|
|
|
|
|
|
|
/// Calls the given closure if `condition == true`.
|
|
|
|
fn with_if<F>(mut self, condition: bool, f: F) -> Self
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self),
|
|
|
|
{
|
|
|
|
if condition {
|
|
|
|
f(&mut self);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
2016-07-16 20:25:21 +00:00
|
|
|
}
|
|
|
|
|
2016-07-16 20:52:28 +00:00
|
|
|
impl<T: Sized> With for T {}
|