pub trait OptionExt<A> {
fn then<F, B>(self, f: F) -> Option<B>
where
F: FnOnce() -> Option<B>;
fn inspect<F>(self, f: F) -> Option<A>
where
F: FnOnce(&A);
fn recover_with<F>(self, f: F) -> Option<A>
where
F: FnOnce() -> Option<A>;
fn remap<F, B>(self, f: F) -> Option<B>
where
Self: Sized,
F: FnOnce() -> B,
{ ... }
fn void(self) -> Option<()>
where
Self: Sized,
{ ... }
fn recover<F>(self, f: F) -> Option<A>
where
F: FnOnce() -> A,
Self: Sized,
{ ... }
}Expand description
Extension with a set of extra combinators for Option<A>.
Required Methods
sourcefn then<F, B>(self, f: F) -> Option<B>where
F: FnOnce() -> Option<B>,
fn then<F, B>(self, f: F) -> Option<B>where
F: FnOnce() -> Option<B>,
Applies f yielding yet another option if Some(x) otherwise propagates None.
use lifterr::option::OptionExt;
fn some() -> Option<i32> { Some(1) }
fn none() -> Option<i32> { None }
assert_eq!(some().then(|| Some("42")), Some("42"));
assert_eq!(none().then(|| Some("42")), None);sourcefn inspect<F>(self, f: F) -> Option<A>where
F: FnOnce(&A),
fn inspect<F>(self, f: F) -> Option<A>where
F: FnOnce(&A),
Runs f with a reference to A when Some(a).
use lifterr::option::OptionExt;
assert_eq!(Some(10).inspect(|a| println!("a = {a}")), Some(10));sourcefn recover_with<F>(self, f: F) -> Option<A>where
F: FnOnce() -> Option<A>,
fn recover_with<F>(self, f: F) -> Option<A>where
F: FnOnce() -> Option<A>,
Recovers from an absent value with a partial function.
use lifterr::option::OptionExt;
fn not_found() -> Option<i32> { None }
fn fallback() -> Option<i32> { Some(42) }
assert_eq!(Some(10).recover_with(fallback), Some(10));
assert_eq!(not_found().recover_with(fallback), Some(42));Provided Methods
sourcefn remap<F, B>(self, f: F) -> Option<B>where
Self: Sized,
F: FnOnce() -> B,
fn remap<F, B>(self, f: F) -> Option<B>where
Self: Sized,
F: FnOnce() -> B,
Applies f yielding a value which is then wrapped into another option if Some(x) otherwise propagates None.
use lifterr::option::OptionExt;
fn some() -> Option<i32> { Some(1) }
fn none() -> Option<i32> { None }
assert_eq!(some().remap(|| "42"), Some("42"));
assert_eq!(none().remap(|| "42"), None);