Macro fail
macro_rules! fail {
($($x:tt)*) => { ... };
}
Available on crate feature
dep_rkyv
only.Expand description
Returns the given error from this function.
The current function must return a Result<_, E>
where E
implements
Source
.
ยงExample
use core::{error::Error, fmt};
use rancor::{fail, Source};
#[derive(Debug)]
struct DivideByZeroError;
impl fmt::Display for DivideByZeroError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "attempted to divide by zero")
}
}
impl Error for DivideByZeroError {}
fn divide<E: Source>(a: i32, b: i32) -> Result<i32, E> {
if b == 0 {
fail!(DivideByZeroError);
}
Ok(a / b)
}