zachs18_stdx/
result.rs

1mod sealed {
2    pub trait Sealed {}
3}
4
5pub trait ResultExt: Sized + sealed::Sealed {
6    type T;
7    type E;
8    fn is_err_or(self, f: impl FnOnce(Self::T) -> bool) -> bool;
9    fn is_ok_or(self, f: impl FnOnce(Self::E) -> bool) -> bool;
10    fn contains<U>(&self, u: &U) -> bool
11    where
12        Self::T: PartialEq<U>;
13    fn contains_err<U>(&self, u: &U) -> bool
14    where
15        Self::E: PartialEq<U>;
16}
17
18impl<T, E> sealed::Sealed for Result<T, E> {}
19
20impl<T, E> ResultExt for Result<T, E> {
21    type T = T;
22    type E = E;
23
24    fn is_err_or(self, f: impl FnOnce(Self::T) -> bool) -> bool {
25        match self {
26            Ok(ok) => f(ok),
27            Err(_) => true,
28        }
29    }
30
31    fn is_ok_or(self, f: impl FnOnce(Self::E) -> bool) -> bool {
32        match self {
33            Ok(_) => true,
34            Err(err) => f(err),
35        }
36    }
37
38    fn contains<U>(&self, u: &U) -> bool
39    where
40        Self::T: PartialEq<U>,
41    {
42        self.as_ref().is_ok_and(|this| *this == *u)
43    }
44
45    fn contains_err<U>(&self, u: &U) -> bool
46    where
47        Self::E: PartialEq<U>,
48    {
49        self.as_ref().is_err_and(|this| *this == *u)
50    }
51}