From 49343281d477cf414cfec737a00c150d5db34ba3 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 24 Oct 2024 06:01:53 +0000 Subject: [PATCH] additional bool extensions Signed-off-by: Jason Volk --- src/core/utils/bool.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/core/utils/bool.rs b/src/core/utils/bool.rs index d7ce78fe..d5fa85aa 100644 --- a/src/core/utils/bool.rs +++ b/src/core/utils/bool.rs @@ -2,12 +2,49 @@ /// Boolean extensions and chain.starters pub trait BoolExt { + fn map T>(self, f: F) -> T + where + Self: Sized; + + fn map_ok_or T>(self, err: E, f: F) -> Result; + + fn map_or T>(self, err: T, f: F) -> T; + + fn map_or_else T>(self, err: F, f: F) -> T; + + fn ok_or(self, err: E) -> Result<(), E>; + + fn ok_or_else E>(self, err: F) -> Result<(), E>; + fn or T>(self, f: F) -> Option; fn or_some(self, t: T) -> Option; } impl BoolExt for bool { + #[inline] + fn map T>(self, f: F) -> T + where + Self: Sized, + { + f(self) + } + + #[inline] + fn map_ok_or T>(self, err: E, f: F) -> Result { self.ok_or(err).map(|()| f()) } + + #[inline] + fn map_or T>(self, err: T, f: F) -> T { self.then(f).unwrap_or(err) } + + #[inline] + fn map_or_else T>(self, err: F, f: F) -> T { self.then(f).unwrap_or_else(err) } + + #[inline] + fn ok_or(self, err: E) -> Result<(), E> { self.then_some(()).ok_or(err) } + + #[inline] + fn ok_or_else E>(self, err: F) -> Result<(), E> { self.then_some(()).ok_or_else(err) } + #[inline] fn or T>(self, f: F) -> Option { (!self).then(f) }