additional bool extensions for Result/Option conversion
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
9775694423
commit
0eb67cfea0
1 changed files with 37 additions and 2 deletions
|
@ -2,6 +2,23 @@
|
||||||
|
|
||||||
/// Boolean extensions and chain.starters
|
/// Boolean extensions and chain.starters
|
||||||
pub trait BoolExt {
|
pub trait BoolExt {
|
||||||
|
#[must_use]
|
||||||
|
fn clone_or<T: Clone>(self, err: T, t: &T) -> T;
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
fn copy_or<T: Copy>(self, err: T, t: T) -> T;
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
fn expect(self, msg: &str) -> Self;
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
fn expect_false(self, msg: &str) -> Self;
|
||||||
|
|
||||||
|
fn into_option(self) -> Option<()>;
|
||||||
|
|
||||||
|
#[allow(clippy::result_unit_err)]
|
||||||
|
fn into_result(self) -> Result<(), ()>;
|
||||||
|
|
||||||
fn map<T, F: FnOnce(Self) -> T>(self, f: F) -> T
|
fn map<T, F: FnOnce(Self) -> T>(self, f: F) -> T
|
||||||
where
|
where
|
||||||
Self: Sized;
|
Self: Sized;
|
||||||
|
@ -22,6 +39,24 @@ pub trait BoolExt {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BoolExt for bool {
|
impl BoolExt for bool {
|
||||||
|
#[inline]
|
||||||
|
fn clone_or<T: Clone>(self, err: T, t: &T) -> T { self.map_or(err, || t.clone()) }
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn copy_or<T: Copy>(self, err: T, t: T) -> T { self.map_or(err, || t) }
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn expect(self, msg: &str) -> Self { self.then_some(true).expect(msg) }
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn expect_false(self, msg: &str) -> Self { (!self).then_some(false).expect(msg) }
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn into_option(self) -> Option<()> { self.then_some(()) }
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn into_result(self) -> Result<(), ()> { self.ok_or(()) }
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn map<T, F: FnOnce(Self) -> T>(self, f: F) -> T
|
fn map<T, F: FnOnce(Self) -> T>(self, f: F) -> T
|
||||||
where
|
where
|
||||||
|
@ -40,10 +75,10 @@ impl BoolExt for bool {
|
||||||
fn map_or_else<T, F: FnOnce() -> T>(self, err: F, f: F) -> T { self.then(f).unwrap_or_else(err) }
|
fn map_or_else<T, F: FnOnce() -> T>(self, err: F, f: F) -> T { self.then(f).unwrap_or_else(err) }
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn ok_or<E>(self, err: E) -> Result<(), E> { self.then_some(()).ok_or(err) }
|
fn ok_or<E>(self, err: E) -> Result<(), E> { self.into_option().ok_or(err) }
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn ok_or_else<E, F: FnOnce() -> E>(self, err: F) -> Result<(), E> { self.then_some(()).ok_or_else(err) }
|
fn ok_or_else<E, F: FnOnce() -> E>(self, err: F) -> Result<(), E> { self.into_option().ok_or_else(err) }
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn or<T, F: FnOnce() -> T>(self, f: F) -> Option<T> { (!self).then(f) }
|
fn or<T, F: FnOnce() -> T>(self, f: F) -> Option<T> { (!self).then(f) }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue