Macro ensure
macro_rules! ensure {
( $condition:expr ) => { ... };
( $condition:expr , $( $args:tt )* ) => { ... };
}Expand description
Ensure that a condition holds true, or else early exit from the current function with an error.
ensure!(condition, ...) is equivalent to the following:
ⓘ
if !condition {
return Err(format_err!(...));
}Like anyhow::ensure! but for wasmtime::Error.
§Example
use wasmtime::{ensure, Result};
fn checked_div(a: u32, b: u32) -> Result<u32> {
ensure!(b != 0, "cannot divide by zero: {a} / {b}");
Ok(a / b)
}
let x = checked_div(6, 2).unwrap();
assert_eq!(x, 3);
let error = checked_div(9, 0).unwrap_err();
assert_eq!(
error.to_string(),
"cannot divide by zero: 9 / 0",
);