1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
//! Defines the `timid_assert` macro to fail with a message without `panic`king.
/// Like an ordinary assertion, except success yields `None` and error yields the error message.
#[macro_export]
macro_rules! timid_assert {
($cond:expr) => {
match std::panic::catch_unwind(|| $cond) {
Ok(b) => {
if !b {
Some(stringify!($cond))
} else {
None
}
}
_ => Some(concat!(
stringify!($cond),
" PANICKED (see two lines above)"
)),
}
};
}
/// Like an ordinary assertion, except success yields `None` and error yields the error message.
#[macro_export]
macro_rules! timid_assert_false {
($cond:expr) => {
match std::panic::catch_unwind(|| $cond) {
Ok(b) => {
if b {
Some(concat!("!", stringify!($cond)))
} else {
None
}
}
_ => Some(concat!(
"!",
stringify!($cond),
" PANICKED (see two lines above)"
)),
}
};
}
pub use {timid_assert, timid_assert_false};