statime/datastructures/common/
clock_identity.rs1use crate::datastructures::{WireFormat, WireFormatError};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12pub struct ClockIdentity(pub [u8; 8]);
13
14impl ClockIdentity {
15 pub fn from_mac_address(addr: [u8; 6]) -> Self {
26 let mut this = Self([0; 8]);
27
28 this.0[0..6].copy_from_slice(&addr);
29
30 this
31 }
32}
33
34impl WireFormat for ClockIdentity {
35 fn serialize(&self, buffer: &mut [u8]) -> Result<(), WireFormatError> {
36 buffer[0..8].copy_from_slice(&self.0);
37 Ok(())
38 }
39
40 fn deserialize(buffer: &[u8]) -> Result<Self, WireFormatError> {
41 Ok(Self(buffer[0..8].try_into().unwrap()))
42 }
43}
44
45#[cfg(feature = "std")]
46impl core::fmt::Display for ClockIdentity {
47 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
48 for (i, val) in self.0.iter().enumerate() {
49 if i != 0 {
50 write!(f, ":")?;
51 }
52
53 write!(f, "{:02x}", val)?;
54 }
55
56 Ok(())
57 }
58}
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63
64 #[test]
65 fn timestamp_wireformat() {
66 let representations = [(
67 [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08u8],
68 ClockIdentity([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]),
69 )];
70
71 for (byte_representation, object_representation) in representations {
72 let mut serialization_buffer = [0; 8];
74 object_representation
75 .serialize(&mut serialization_buffer)
76 .unwrap();
77 assert_eq!(serialization_buffer, byte_representation);
78
79 let deserialized_data = ClockIdentity::deserialize(&byte_representation).unwrap();
81 assert_eq!(deserialized_data, object_representation);
82 }
83 }
84
85 #[test]
86 fn from_mac() {
87 let mac = [1, 2, 3, 4, 5, 6];
88 let id = ClockIdentity::from_mac_address(mac);
89 assert_eq!(id, ClockIdentity([1, 2, 3, 4, 5, 6, 0, 0]));
90 }
91}