Add then_none() to BoolExt; move unsorted then_ utils.
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
@@ -23,10 +23,6 @@ pub trait BoolExt {
|
|||||||
#[allow(clippy::result_unit_err)]
|
#[allow(clippy::result_unit_err)]
|
||||||
fn into_result(self) -> Result<(), ()>;
|
fn into_result(self) -> Result<(), ()>;
|
||||||
|
|
||||||
fn then_ok_or<T, E>(self, t: T, e: E) -> Result<T, E>;
|
|
||||||
|
|
||||||
fn then_ok_or_else<T, E, F: FnOnce() -> E>(self, t: T, e: F) -> Result<T, E>;
|
|
||||||
|
|
||||||
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;
|
||||||
@@ -44,6 +40,12 @@ pub trait BoolExt {
|
|||||||
fn or<T, F: FnOnce() -> T>(self, f: F) -> Option<T>;
|
fn or<T, F: FnOnce() -> T>(self, f: F) -> Option<T>;
|
||||||
|
|
||||||
fn or_some<T>(self, t: T) -> Option<T>;
|
fn or_some<T>(self, t: T) -> Option<T>;
|
||||||
|
|
||||||
|
fn then_none<T>(self) -> Option<T>;
|
||||||
|
|
||||||
|
fn then_ok_or<T, E>(self, t: T, e: E) -> Result<T, E>;
|
||||||
|
|
||||||
|
fn then_ok_or_else<T, E, F: FnOnce() -> E>(self, t: T, e: F) -> Result<T, E>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BoolExt for bool {
|
impl BoolExt for bool {
|
||||||
@@ -71,14 +73,6 @@ impl BoolExt for bool {
|
|||||||
#[inline]
|
#[inline]
|
||||||
fn into_result(self) -> Result<(), ()> { self.ok_or(()) }
|
fn into_result(self) -> Result<(), ()> { self.ok_or(()) }
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn then_ok_or<T, E>(self, t: T, e: E) -> Result<T, E> { self.map_ok_or(e, move || t) }
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn then_ok_or_else<T, E, F: FnOnce() -> E>(self, t: T, e: F) -> Result<T, E> {
|
|
||||||
self.ok_or_else(e).map(move |()| t)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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
|
||||||
@@ -113,4 +107,15 @@ impl BoolExt for bool {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn or_some<T>(self, t: T) -> Option<T> { (!self).then_some(t) }
|
fn or_some<T>(self, t: T) -> Option<T> { (!self).then_some(t) }
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn then_none<T>(self) -> Option<T> { Option::<T>::None }
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn then_ok_or<T, E>(self, t: T, e: E) -> Result<T, E> { self.map_ok_or(e, move || t) }
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn then_ok_or_else<T, E, F: FnOnce() -> E>(self, t: T, e: F) -> Result<T, E> {
|
||||||
|
self.ok_or_else(e).map(move |()| t)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user