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
//! Conversion between Cycles struct and Frequency

use libc::c_longlong as c_ll;

use super::Cycles;

impl Cycles {
    /// ```
    /// use cpu_cycles_reader::Cycles;
    ///
    /// assert_eq!(Cycles::new(1000), Cycles::from_hz(1000));
    /// ```
    #[must_use]
    pub const fn from_hz(h: c_ll) -> Self {
        Self { raw: h }
    }

    /// ```
    /// use cpu_cycles_reader::Cycles;
    ///
    /// assert_eq!(Cycles::new(1000), Cycles::from_khz(1));
    /// ```
    #[must_use]
    pub const fn from_khz(k: c_ll) -> Self {
        Self { raw: k * 1000 }
    }

    /// ```
    /// use cpu_cycles_reader::Cycles;
    ///
    /// assert_eq!(Cycles::new(2_000_000), Cycles::from_mhz(2));
    /// ```
    #[must_use]
    pub const fn from_mhz(m: c_ll) -> Self {
        Self { raw: m * 1_000_000 }
    }

    /// ```
    /// use cpu_cycles_reader::Cycles;
    ///
    /// assert_eq!(Cycles::new(3_000_000_000), Cycles::from_ghz(3));
    /// ```
    #[must_use]
    pub const fn from_ghz(g: c_ll) -> Self {
        Self {
            raw: g * 1_000_000_000,
        }
    }

    /// ```
    /// use cpu_cycles_reader::Cycles;
    ///
    /// assert_eq!(Cycles::new(1000).as_hz(), 1000);
    /// ```
    #[must_use]
    pub const fn as_hz(&self) -> c_ll {
        self.raw
    }

    /// ```
    /// use cpu_cycles_reader::Cycles;
    ///
    /// assert_eq!(Cycles::new(1_000_000).as_khz(), 1000);
    /// ```
    #[must_use]
    pub const fn as_khz(&self) -> c_ll {
        self.raw / 1000
    }

    /// ```
    /// use cpu_cycles_reader::Cycles;
    ///
    /// assert_eq!(Cycles::new(1_000_000_000).as_mhz(), 1000);
    /// ```
    #[must_use]
    pub const fn as_mhz(&self) -> c_ll {
        self.raw / 1_000_000
    }

    /// ```
    /// use cpu_cycles_reader::Cycles;
    ///
    /// assert_eq!(Cycles::new(1_000_000_000_000).as_ghz(), 1000);
    /// ```
    #[must_use]
    pub const fn as_ghz(&self) -> c_ll {
        self.raw / 1_000_000_000
    }
}