add and/and_then to BoolExt

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-12-22 07:03:32 +00:00 committed by strawberry
parent a3d87be22f
commit 749f29aaab

View file

@ -2,6 +2,10 @@
/// Boolean extensions and chain.starters /// Boolean extensions and chain.starters
pub trait BoolExt { pub trait BoolExt {
fn and<T>(self, t: Option<T>) -> Option<T>;
fn and_then<T, F: FnOnce() -> Option<T>>(self, f: F) -> Option<T>;
#[must_use] #[must_use]
fn clone_or<T: Clone>(self, err: T, t: &T) -> T; fn clone_or<T: Clone>(self, err: T, t: &T) -> T;
@ -39,6 +43,12 @@ pub trait BoolExt {
} }
impl BoolExt for bool { impl BoolExt for bool {
#[inline]
fn and<T>(self, t: Option<T>) -> Option<T> { self.then_some(t).flatten() }
#[inline]
fn and_then<T, F: FnOnce() -> Option<T>>(self, f: F) -> Option<T> { self.then(f).flatten() }
#[inline] #[inline]
fn clone_or<T: Clone>(self, err: T, t: &T) -> T { self.map_or(err, || t.clone()) } fn clone_or<T: Clone>(self, err: T, t: &T) -> T { self.map_or(err, || t.clone()) }