add Filter extension to Result
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
e49aee61c1
commit
6b0eb7608d
2 changed files with 24 additions and 2 deletions
21
src/core/utils/result/filter.rs
Normal file
21
src/core/utils/result/filter.rs
Normal 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))
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue