| 65 | #[expect(clippy::missing_errors_doc, reason = "todo")] |
| 66 | #[expect(clippy::missing_panics_doc, reason = "slices appropriately sized")] |
| 67 | pub fn read(vector: &Vec<u8>) -> Result<Self, ShmError> { |
| 68 | if vector.len() < size_of::<VMClockShmHeader>() { |
| 69 | return syserror!(format!( |
| 70 | "VMClockShmHeader is shorter than expected size [{} < {}].", |
| 71 | vector.len(), |
| 72 | size_of::<VMClockShmHeader>() |
| 73 | )); |
| 74 | } |
| 75 | |
| 76 | let slice = vector.as_slice(); |
| 77 | |
| 78 | let magic = u32::from_le_bytes(slice[0..4].try_into().unwrap()); |
| 79 | let size = u32::from_le_bytes(slice[4..8].try_into().unwrap()); |
| 80 | let version = u16::from_le_bytes(slice[8..10].try_into().unwrap()); |
| 81 | let counter_id = u8::from_le_bytes(slice[10..11].try_into().unwrap()); |
| 82 | let time_type = u8::from_le_bytes(slice[11..12].try_into().unwrap()); |
| 83 | let seq_count = u32::from_le_bytes(slice[12..16].try_into().unwrap()); |
| 84 | |
| 85 | let header = VMClockShmHeader { |
| 86 | magic: atomic::AtomicU32::new(magic), |
| 87 | size: atomic::AtomicU32::new(size), |
| 88 | version: atomic::AtomicU16::new(version), |
| 89 | counter_id: atomic::AtomicU8::new(counter_id), |
| 90 | time_type: atomic::AtomicU8::new(time_type), |
| 91 | seq_count: atomic::AtomicU32::new(seq_count), |
| 92 | }; |
| 93 | |
| 94 | header.is_valid()?; |
| 95 | Ok(header) |
| 96 | } |
| 97 | |
| 98 | /// Check whether the magic number matches the expected one. |
| 99 | fn matches_magic(&self) -> bool { |