sequoia_openpgp_mt/
macros.rs

1use std::cmp;
2
3macro_rules! trace {
4    ( $TRACE:expr, $fmt:expr, $($pargs:expr),* ) => {
5        if $TRACE {
6            eprintln!($fmt, $($pargs),*);
7        }
8    };
9    ( $TRACE:expr, $fmt:expr ) => {
10        trace!($TRACE, $fmt, );
11    };
12}
13
14// Converts an indentation level to whitespace.
15pub(crate) fn indent(i: isize) -> &'static str {
16    let s = "                                                  ";
17    &s[0..cmp::min(usize::try_from(i).unwrap_or(0), s.len())]
18}
19
20macro_rules! tracer {
21    ( $TRACE:expr, $func:expr ) => {
22        tracer!($TRACE, $func, 0)
23    };
24    ( $TRACE:expr, $func:expr, $indent:expr ) => {
25        // Currently, Rust doesn't support $( ... ) in a nested
26        // macro's definition.  See:
27        // https://users.rust-lang.org/t/nested-macros-issue/8348/2
28        macro_rules! t {
29            ( $fmt:expr ) =>
30            { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, $fmt) };
31            ( $fmt:expr, $a:expr ) =>
32            { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a)) };
33            ( $fmt:expr, $a:expr, $b:expr ) =>
34            { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b)) };
35            ( $fmt:expr, $a:expr, $b:expr, $c:expr ) =>
36            { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c)) };
37            ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr ) =>
38            { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d)) };
39            ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr ) =>
40            { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e)) };
41            ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr ) =>
42            { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f)) };
43            ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr ) =>
44            { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g)) };
45            ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr ) =>
46            { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h)) };
47            ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr ) =>
48            { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h, $i)) };
49            ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr, $j:expr ) =>
50            { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j)) };
51            ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr, $j:expr, $k:expr ) =>
52            { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k)) };
53        }
54    }
55}
56
57
58/// A very simple profiling tool.
59///
60/// Note: don't ever profile code that has not been compiled in
61/// release mode.  There can be orders of magnitude difference in
62/// execution time between it and debug mode!
63///
64/// This macro measures the wall time it takes to execute the block.
65/// If the time is at least $ms_threshold (in milli-seconds), then it
66/// displays the output on stderr.  The output is prefixed with label,
67/// if it is provided.
68///
69/// ```rust,ignore
70/// let result = time_it!("Some code", 10, {
71///     // Some code.
72///     5
73/// });
74/// assert_eq!(result, 5);
75/// ```
76#[allow(unused_macros)]
77macro_rules! time_it {
78    ( $label:expr, $ms_threshold:expr, $body:expr ) => {{
79        use std::time::{SystemTime, Duration};
80
81        // We use drop so that code that uses non-local exits (e.g.,
82        // using break 'label) still works.
83        struct Timer {
84            start: SystemTime,
85        };
86        impl Drop for Timer {
87            fn drop(&mut self) {
88                let elapsed = self.start.elapsed();
89                if elapsed.clone().unwrap_or(Duration::from_millis($ms_threshold))
90                    >= Duration::from_millis($ms_threshold)
91                {
92                    if $label.len() > 0 {
93                        eprint!("{}:", $label);
94                    }
95                    eprintln!("{}:{}: {:?}", file!(), line!(), elapsed);
96                }
97            }
98        }
99
100        let _start = Timer { start: SystemTime::now() };
101        $body
102    }};
103    ( $label:expr, $body:expr ) => {
104        time_it!($label, 0, $body)
105    };
106    ( $body:expr ) => {
107        time_it!("", $body)
108    };
109}