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
|
/// 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()) }
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue