add and/and_then to BoolExt
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
a3d87be22f
commit
749f29aaab
1 changed files with 10 additions and 0 deletions
|
@ -2,6 +2,10 @@
|
|||
|
||||
/// Boolean extensions and chain.starters
|
||||
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]
|
||||
fn clone_or<T: Clone>(self, err: T, t: &T) -> T;
|
||||
|
||||
|
@ -39,6 +43,12 @@ pub trait BoolExt {
|
|||
}
|
||||
|
||||
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]
|
||||
fn clone_or<T: Clone>(self, err: T, t: &T) -> T { self.map_or(err, || t.clone()) }
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue