From c944f1124a07ad4c6d03c7b1e721e9a76cd56c68 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 10 Jul 2025 03:03:57 +0000 Subject: [PATCH] Additional BoolExt utils. Signed-off-by: Jason Volk --- src/core/utils/bool.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/core/utils/bool.rs b/src/core/utils/bool.rs index e9db1e4f..fed8774b 100644 --- a/src/core/utils/bool.rs +++ b/src/core/utils/bool.rs @@ -23,6 +23,10 @@ pub trait BoolExt { #[allow(clippy::result_unit_err)] fn into_result(self) -> Result<(), ()>; + fn then_ok_or(self, t: T, e: E) -> Result; + + fn then_ok_or_else E>(self, t: T, e: F) -> Result; + fn map T>(self, f: F) -> T where Self: Sized; @@ -67,6 +71,14 @@ impl BoolExt for bool { #[inline] fn into_result(self) -> Result<(), ()> { self.ok_or(()) } + #[inline] + fn then_ok_or(self, t: T, e: E) -> Result { self.map_ok_or(e, move || t) } + + #[inline] + fn then_ok_or_else E>(self, t: T, e: F) -> Result { + self.ok_or_else(e).map(move |()| t) + } + #[inline] fn map T>(self, f: F) -> T where