Skip to main content

assert_or_error

Macro assert_or_error 

Source
macro_rules! assert_or_error {
    ($evaluate:expr, $($message:tt)+) => { ... };
}
Expand description

Conditional assertion that logs an error on failure.

When ENABLE_ASSERT environment variable is set to “1” and the condition is false, this macro exits the process with a colorized error message. When assertions are disabled and the condition is false, it logs an error message using the log crate instead.

§Environment Variables

  • ENABLE_ASSERT - Set to “1” to enable assertions, any other value disables them

§Examples

use moosicbox_assert::assert_or_error;

fn process_data(data: &[u8]) {
    assert_or_error!(!data.is_empty(), "Cannot process empty data");
    assert_or_error!(data.len() < 1024, "Data too large: {} bytes", data.len());
}

§Panics

Exits the process when the condition is false and ENABLE_ASSERT=1.