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)
}
}