rate_common/
macros.rs

1//! Macros and other utility code
2
3/// This should be used for every write to stdout.
4#[macro_export]
5macro_rules! puts {
6    ($($arg:tt)*) => ({
7        use std::io::Write;
8        match write!(std::io::stdout(), $($arg)*) {
9            Ok(()) => (),
10            // Don't panic on SIGPIPE.
11            Err(ref err) if err.kind() == std::io::ErrorKind::BrokenPipe => std::process::exit(141),
12            Err(ref err) =>  panic!("{}", err),
13        };
14    })
15}
16
17/// Print to stdout, prefixed by "c ".
18#[macro_export]
19macro_rules! comment {
20    ($($arg:tt)*) => ({
21        $crate::puts!("c ");
22        $crate::puts!($($arg)*);
23        $crate::puts!("\n");
24    })
25}
26
27/// Print to stdout with yellow font color.
28#[macro_export]
29macro_rules! as_warning {
30    ($what:expr) => {{
31        if $crate::output::is_a_tty() {
32            $crate::puts!("\x1b[33;1m");
33        }
34        $what;
35        if $crate::output::is_a_tty() {
36            $crate::puts!("\x1b[0m");
37        }
38    }};
39}
40
41/// Print to stdout with red font color.
42#[macro_export]
43macro_rules! as_error {
44    ($what:expr) => {{
45        if $crate::output::is_a_tty() {
46            $crate::puts!("\x1b[31;1m");
47        }
48        $what;
49        if $crate::output::is_a_tty() {
50            $crate::puts!("\x1b[0m");
51        }
52    }};
53}
54
55/// Report a fatal error and exit.
56#[macro_export]
57macro_rules! die {
58    ($($arg:tt)*) => ({
59        $crate::as_error!({
60            $crate::puts!($($arg)*);
61            $crate::puts!("\n");
62        });
63        std::process::exit(2);
64    })
65}
66
67/// Native assertions cannot be disabled, that's why why prefer to use this
68/// macro.
69#[macro_export]
70macro_rules! invariant {
71    ($($arg:tt)*) => ({
72        if $crate::config::CHECK_INVARIANTS {
73            assert!($($arg)*);
74        }
75    })
76}
77
78/// Like invariant, but for preconditions.
79#[macro_export]
80macro_rules! requires {
81    ($($arg:tt)*) => ({
82        if $crate::config::CHECK_PRECONDITIONS {
83            assert!($($arg)*);
84        }
85    })
86}