Skip to main content

ensure

Macro ensure 

Source
macro_rules! ensure {
    ($result:expr) => { ... };
    ($result:expr, $($args:tt)*) => { ... };
}
Expand description

Check if an expression is true, as an assertion

This is meant to be assert-like: the condition is an invariant that should almost never fail if everything is correct, so a failure indicates a bug (i.e. it’s a debugging aid, like assert!, but returns an error instead of panicking). For errors that are expected to happen at runtime, such as invalid user input or system errors (e.g. file does not exist), use cu::check! or cu::bail! with a descriptive message instead.

Unlike anyhow::ensure, if the condition fail, this will generate an Error instead of returning an error directly, so you need to add a ?. It also always include the expression stringified in the debug info. However, it does not automatically parse the input and generate debug info message based on that (unlike anyhow)