pub trait Apply<T> {
type Cont<_T>;
fn apply<U, F>(&self, rhs: F) -> Self::Cont<U>
where
F: FnOnce(&T) -> U;
}
pub trait ApplyOnce<T> {
type Cont<_T>;
fn apply_once<U, F>(self, rhs: F) -> Self::Cont<U>
where
F: FnOnce(T) -> U;
}
pub trait ApplyMut<T> {
type Cont<_T>;
fn apply_mut<F>(&mut self, rhs: F) -> &mut Self::Cont<T>
where
F: FnMut(&mut T);
}
impl<T> Apply<T> for Option<T> {
type Cont<U> = Option<U>;
fn apply<U, F>(&self, rhs: F) -> Self::Cont<U>
where
F: FnOnce(&T) -> U,
{
self.as_ref().map(rhs)
}
}
impl<T> ApplyOnce<T> for Option<T> {
type Cont<U> = Option<U>;
fn apply_once<U, F>(self, rhs: F) -> Self::Cont<U>
where
F: FnOnce(T) -> U,
{
self.map(rhs)
}
}
impl<T> ApplyMut<T> for Option<T> {
type Cont<U> = Option<U>;
fn apply_mut<F>(&mut self, mut rhs: F) -> &mut Self::Cont<T>
where
F: FnMut(&mut T),
{
if let Some(value) = self {
rhs(value);
}
self
}
}