Skip to main content

vortex_ensure_eq

Macro vortex_ensure_eq 

Source
macro_rules! vortex_ensure_eq {
    ($left:expr, $right:expr $(,)?) => { ... };
    ($left:expr, $right:expr, $variant:ident: $fmt:literal $(, $arg:expr)* $(,)?) => { ... };
    ($left:expr, $right:expr, $fmt:literal $(, $arg:expr)* $(,)?) => { ... };
}
Expand description

A macro that mirrors assert_eq! but instead of panicking when left != right, it will immediately return an erroneous VortexResult to the calling context.

Both values must implement PartialEq and Display, and each is evaluated exactly once. Use vortex_ensure! for values that do not implement Display.

By default this returns an AssertionFailed error that shows both expressions and their values, so most callers do not need a message:

fn check(len: usize) -> VortexResult<()> {
    vortex_ensure_eq!(len, 2);
    Ok(())
}

let err = check(3).unwrap_err().to_string();
assert!(err.starts_with("Assertion failed error: `len == 2`\n  left: 3\n right: 2"));

A custom message, optionally prefixed with an error variant, replaces the expressions. The values are still appended, so the message does not need to repeat them:

fn check(len: usize) -> VortexResult<()> {
    vortex_ensure_eq!(len, 2, InvalidArgument: "map entries must have {} fields", 2);
    Ok(())
}

let err = check(3).unwrap_err().to_string();
assert!(err.starts_with(
    "Invalid argument error: map entries must have 2 fields\n  left: 3\n right: 2"
));