xentrace_parser/record/event/
code.rs

1use std::{
2    fmt::{Debug, Formatter, Result},
3    ops::BitAnd,
4};
5
6/// Contains the event code read as a 32-bit unsigned big-endian integer.
7#[repr(transparent)]
8#[derive(Clone, Copy, PartialEq, Eq)]
9pub struct EventCode(u32);
10
11impl EventCode {
12    /// Returns the complete event code.
13    pub fn value(&self) -> u32 {
14        self.0
15    }
16
17    /// Returns the event class, can be used to filter events.
18    pub fn main(&self) -> u32 {
19        (self.0 & 0x0FFF0000) >> 16
20    }
21
22    /// Returns the event subclass, can also be used to filter events.
23    pub fn sub(&self) -> u32 {
24        (self.0 & 0x0000F000) >> 12
25    }
26
27    /// Returns the event minor, identifies the event in its class and subclass.
28    pub fn minor(&self) -> u32 {
29        self.0 & 0x00000FFF
30    }
31}
32
33impl From<u32> for EventCode {
34    fn from(value: u32) -> Self {
35        Self(value)
36    }
37}
38
39impl From<EventCode> for u32 {
40    fn from(value: EventCode) -> Self {
41        value.0
42    }
43}
44
45impl From<&EventCode> for u32 {
46    fn from(value: &EventCode) -> Self {
47        value.0
48    }
49}
50
51impl PartialEq<u32> for EventCode {
52    fn eq(&self, other: &u32) -> bool {
53        u32::from(*self).eq(other)
54    }
55}
56
57impl PartialEq<EventCode> for u32 {
58    fn eq(&self, other: &EventCode) -> bool {
59        u32::from(*other).eq(self)
60    }
61}
62
63impl BitAnd<u32> for EventCode {
64    type Output = EventCode;
65
66    fn bitand(self, rhs: u32) -> Self::Output {
67        Self(self.0.bitand(rhs))
68    }
69}
70
71impl BitAnd<EventCode> for u32 {
72    type Output = EventCode;
73
74    fn bitand(self, rhs: EventCode) -> Self::Output {
75        EventCode(rhs.0.bitand(self))
76    }
77}
78
79impl Debug for EventCode {
80    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
81        f.debug_struct("EventCode")
82            .field("value", &format_args!("{:#010X}", self.0))
83            .field("main", &format_args!("{:#06X}", self.main()))
84            .field("sub", &format_args!("{:#03X}", self.sub()))
85            .field("minor", &format_args!("{:#05X}", self.minor()))
86            .finish()
87    }
88}
89
90#[cfg(test)]
91mod tests {
92    use super::EventCode;
93
94    #[test]
95    fn full_equality_test() {
96        let ecode1 = EventCode::from(0x00015003);
97        let ecode2 = EventCode::from(0x00015003);
98
99        assert_eq!(ecode1, ecode2);
100        assert_eq!(ecode1.main(), ecode2.main());
101        assert_eq!(ecode1.sub(), ecode2.sub());
102        assert_eq!(ecode1.minor(), ecode2.minor());
103    }
104
105    #[test]
106    fn equality_test() {
107        let ecode1 = EventCode::from(0x00015003);
108        let ecode2 = EventCode::from(0x01015003);
109
110        assert_ne!(ecode1, ecode2);
111        assert_ne!(ecode1.main(), ecode2.main());
112
113        assert_eq!(ecode1.sub(), ecode2.sub());
114        assert_eq!(ecode1.minor(), ecode2.minor());
115    }
116}