#![doc = include_str!("../README.md")]
#[macro_export]
macro_rules! fast_assert {
($cond:expr $(,)?) => {
if !$cond {
$crate::cold::assert_failed(|| {
panic!("assertion failed: {}", stringify!($cond));
});
}
};
($cond:expr, $($arg:tt)+) => {
if !$cond {
$crate::cold::assert_failed(|| {
panic!($($arg)+);
});
}
};
}
#[doc(hidden)]
pub mod cold {
#[cold]
#[track_caller]
pub fn assert_failed<F>(msg_fn: F)
where
F: FnOnce(),
{
msg_fn();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn holds() {
fast_assert!(0 < 100);
}
#[test]
#[should_panic]
fn fails() {
fast_assert!(100 < 0);
}
#[test]
fn holds_custom_message() {
let x = 0;
let y = 100;
fast_assert!(x < y, "x ({}) should be less than y ({})", x, y);
}
#[test]
#[should_panic]
fn fails_custom_message() {
let x = 100;
let y = 0;
fast_assert!(x < y, "x ({}) should be less than y ({})", x, y);
}
}