1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
mod sealed {
    pub trait Sealed {}
}

pub trait ResultExt: Sized + sealed::Sealed {
    type T;
    type E;
    fn is_err_or(self, f: impl FnOnce(Self::T) -> bool) -> bool;
    fn is_ok_or(self, f: impl FnOnce(Self::E) -> bool) -> bool;
    fn contains<U>(&self, u: &U) -> bool
    where
        Self::T: PartialEq<U>;
    fn contains_err<U>(&self, u: &U) -> bool
    where
        Self::E: PartialEq<U>;
}

impl<T, E> sealed::Sealed for Result<T, E> {}

impl<T, E> ResultExt for Result<T, E> {
    type T = T;
    type E = E;

    fn is_err_or(self, f: impl FnOnce(Self::T) -> bool) -> bool {
        match self {
            Ok(ok) => f(ok),
            Err(_) => true,
        }
    }

    fn is_ok_or(self, f: impl FnOnce(Self::E) -> bool) -> bool {
        match self {
            Ok(_) => true,
            Err(err) => f(err),
        }
    }

    fn contains<U>(&self, u: &U) -> bool
    where
        Self::T: PartialEq<U>,
    {
        self.as_ref().is_ok_and(|this| *this == *u)
    }

    fn contains_err<U>(&self, u: &U) -> bool
    where
        Self::E: PartialEq<U>,
    {
        self.as_ref().is_err_and(|this| *this == *u)
    }
}