pub trait OptionExt<A> {
fn then<F, B>(self, f: F) -> Option<B>
where
F: FnOnce() -> Option<B>;
fn remap<F, B>(self, f: F) -> Option<B>
where
Self: Sized,
F: FnOnce() -> B,
{
self.then(|| f().into())
}
fn void(self) -> Option<()>
where
Self: Sized,
{
self.remap(|| ())
}
fn inspect<F>(self, f: F) -> Option<A>
where
F: FnOnce(&A);
fn recover<F>(self, f: F) -> Option<A>
where
F: FnOnce() -> A,
Self: Sized,
{
self.recover_with(|| f().into())
}
fn recover_with<F>(self, f: F) -> Option<A>
where
F: FnOnce() -> Option<A>;
}
impl<A> OptionExt<A> for Option<A> {
fn then<F, B>(self, f: F) -> Option<B>
where
F: FnOnce() -> Option<B>,
{
self.and_then(|_| f())
}
fn inspect<F>(self, f: F) -> Option<A>
where
F: FnOnce(&A),
{
self.map(|a| {
f(&a);
a
})
}
fn recover_with<F>(self, f: F) -> Option<A>
where
F: FnOnce() -> Option<A>,
{
self.map_or_else(f, A::into)
}
}