add Filter extension to Result

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-10-31 07:33:16 +00:00
parent e49aee61c1
commit 6b0eb7608d
2 changed files with 24 additions and 2 deletions

View file

@ -0,0 +1,21 @@
use super::Result;
pub trait Filter<T, E> {
/// Similar to Option::filter
#[must_use]
fn filter<P, U>(self, predicate: P) -> Self
where
P: FnOnce(&T) -> Result<(), U>,
E: From<U>;
}
impl<T, E> Filter<T, E> for Result<T, E> {
#[inline]
fn filter<P, U>(self, predicate: P) -> Self
where
P: FnOnce(&T) -> Result<(), U>,
E: From<U>,
{
self.and_then(move |t| predicate(&t).map(move |()| t).map_err(Into::into))
}
}