sleuth/
util.rs

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