diff --git a/src/core/utils/result.rs b/src/core/utils/result.rs index 9a60d19e..fb1b7b95 100644 --- a/src/core/utils/result.rs +++ b/src/core/utils/result.rs @@ -1,4 +1,5 @@ mod debug_inspect; +mod filter; mod flat_ok; mod into_is_ok; mod log_debug_err; @@ -8,8 +9,8 @@ mod not_found; mod unwrap_infallible; pub use self::{ - debug_inspect::DebugInspect, flat_ok::FlatOk, into_is_ok::IntoIsOk, log_debug_err::LogDebugErr, log_err::LogErr, - map_expect::MapExpect, not_found::NotFound, unwrap_infallible::UnwrapInfallible, + debug_inspect::DebugInspect, filter::Filter, flat_ok::FlatOk, into_is_ok::IntoIsOk, log_debug_err::LogDebugErr, + log_err::LogErr, map_expect::MapExpect, not_found::NotFound, unwrap_infallible::UnwrapInfallible, }; pub type Result = std::result::Result; diff --git a/src/core/utils/result/filter.rs b/src/core/utils/result/filter.rs new file mode 100644 index 00000000..f11d3632 --- /dev/null +++ b/src/core/utils/result/filter.rs @@ -0,0 +1,21 @@ +use super::Result; + +pub trait Filter { + /// Similar to Option::filter + #[must_use] + fn filter(self, predicate: P) -> Self + where + P: FnOnce(&T) -> Result<(), U>, + E: From; +} + +impl Filter for Result { + #[inline] + fn filter(self, predicate: P) -> Self + where + P: FnOnce(&T) -> Result<(), U>, + E: From, + { + self.and_then(move |t| predicate(&t).map(move |()| t).map_err(Into::into)) + } +}