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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//! Provides implementations of format traits

use crate::rolling::RollingRateCounter;
use crate::DiscreteRateCounter;
use crate::RateCounter;
use std::fmt;

/// Creates an implementation of fmt::Display and fmt::Debug for the given type implementing RateCounter
macro_rules! format_impl_for_RateCounter {
    ($($type:tt)*) => {
        impl fmt::Display for $($type)* {
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                write!(f, "{} Hz", self.rate())
            }
        }

        impl fmt::Debug for $($type)* {
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                write!(f, "{{ samples: {}, rate: {} }}", self.samples(), self.rate())
            }
        }
    };
}

format_impl_for_RateCounter!(RollingRateCounter);

format_impl_for_RateCounter!(DiscreteRateCounter);

#[cfg(test)]
mod tests {
    use super::{DiscreteRateCounter, RateCounter, RollingRateCounter};
    use std::thread::sleep;
    use std::time::Duration;
    #[test]
    fn test_rolling_rate_counter_formatting() {
        let mut c = RollingRateCounter::new(10);

        let sample_period = Duration::from_millis(10);
        for _ in 1..5 {
            // Accuracy doesn't matter here so we can just sleep
            sleep(sample_period);
            c.update();
        }

        // Rate should be 100 Hz with 10 ms/update
        let rate = c.rate();
        let samples = c.samples();
        let expected = format!("{} Hz", rate);
        assert_eq!(
            expected,
            format!("{}", c),
            "Display output should be of the form \"<rate> Hz\""
        );
        let expected = format!("{{ samples: {}, rate: {} }}", samples, rate);
        assert_eq!(
            expected,
            format!("{:?}", c),
            "Debug output should be of the form \"{{ samples: <samp>, rate: <rate> }}\""
        );
    }

    #[test]
    fn test_discrete_rate_counter_formatting() {
        let mut c = DiscreteRateCounter::new(10);

        let sample_period = ::std::time::Duration::from_millis(10);
        for _ in 1..5 {
            // Accuracy doesn't matter here so we can just sleep
            sleep(sample_period);
            c.update();
        }

        // Rate should be 100 Hz with 10 ms/update
        let rate = c.rate();
        let samples = c.samples();
        let expected = format!("{} Hz", rate);
        assert_eq!(
            expected,
            format!("{}", c),
            "Display output should be of the form \"<rate> Hz\""
        );
        let expected = format!("{{ samples: {}, rate: {} }}", samples, rate);
        assert_eq!(
            expected,
            format!("{:?}", c),
            "Debug output should be of the form \"{{ samples: <samp>, rate: <rate> }}\""
        );
    }
}