boolext
This commit is contained in:
parent
a67e3d1d8c
commit
29574637da
1 changed files with 40 additions and 0 deletions
|
|
@ -44,6 +44,46 @@ impl<O, E> ResultExt for Result<O, E> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait BoolExt: Sized {
|
||||||
|
fn true_or<E>(self, err: E) -> Result<(), E>;
|
||||||
|
fn false_or<E>(self, err: E) -> Result<(), E>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BoolExt for bool {
|
||||||
|
fn true_or<E>(self, err: E) -> Result<(), E> {
|
||||||
|
if self {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn false_or<E>(self, err: E) -> Result<(), E> {
|
||||||
|
if self {
|
||||||
|
Err(err)
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait OptExt: Sized {
|
||||||
|
type Inside;
|
||||||
|
|
||||||
|
fn none_or<E>(self, f: impl FnOnce(Self::Inside) -> E) -> Result<(), E>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> OptExt for Option<T> {
|
||||||
|
type Inside = T;
|
||||||
|
|
||||||
|
fn none_or<E>(self, f: impl FnOnce(Self::Inside) -> E) -> Result<(), E> {
|
||||||
|
match self {
|
||||||
|
Some(v) => Err(f(v)),
|
||||||
|
None => Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub trait ErrExt: Sized {
|
pub trait ErrExt: Sized {
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
fn aux(self) -> Aux
|
fn aux(self) -> Aux
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue