macro_rules! invariant {
    ($invariant: expr $(,)?) => { ... };
    ($invariant: expr, $err_code: ident $(,)?) => { ... };
    ($invariant: expr, $err_code: ident, $msg: expr $(,)?) => { ... };
    ($invariant: expr, $msg: literal $(,)?) => { ... };
    ($invariant:expr, $err:expr $(,)?) => { ... };
    ($invariant:expr, $err:expr, $msg: expr $(,)?) => { ... };
}
Expand description

Asserts that an invariant holds, otherwise logs the given message. This is a drop-in replacement for require!.

Example

invariant!(1 == 2, "incorrect");

Invariants do not throw if they pass.

invariant!(1 == 1, "won't throw");

Error codes are optional:

invariant!(1 == 1);

You may also use a crate ErrorCode:

#[error_code]
pub enum ErrorCode { MyError }

assert_does_not_throw!({
  invariant!(1 == 1, MyError);
});
assert_throws!({
  invariant!(1 == 2);
}, vipers::VipersError::InvariantFailed);
assert_throws!({
  invariant!(1 == 2, "this is stupid");
}, vipers::VipersError::InvariantFailed);
assert_throws!({
  invariant!(1 == 2, MyError);
}, ErrorCode::MyError);
assert_throws!({
  invariant!(1 == 2, MyError);
}, ErrorCode::MyError);
assert_throws!({
  invariant!(1 == 2, MyError, "this is wack");
}, ErrorCode::MyError);