Skip to main content

detcore_model/
liteinst_native_exec_runtime.rs

1//! Exact fixed runtime envelope shared by native consumer and coordinator.
2//! Decoding shape is not authority to restore counters or publish an image.
3
4pub const STATS_BYTES: usize = 8338;
5pub const PACKET_BYTES: usize = 24 + STATS_BYTES;
6const HEADER: [u8; 16] = *b"HLXRT001\x01\x00\x00\x00\x00\x00\x00\x00";
7
8#[derive(Clone, Copy, Debug, Eq, PartialEq)]
9pub struct RuntimePacket<'a> {
10    pub sequence: u64,
11    pub stats: &'a [u8; STATS_BYTES],
12}
13#[derive(Clone, Copy, Debug, Eq, PartialEq)]
14pub struct PacketError;
15
16impl RuntimePacket<'_> {
17    pub fn encode(&self, output: &mut [u8; PACKET_BYTES]) -> Result<(), PacketError> {
18        if self.sequence == 0 || self.sequence == u64::MAX {
19            return Err(PacketError);
20        }
21        output[..16].copy_from_slice(&HEADER);
22        output[16..24].copy_from_slice(&self.sequence.to_le_bytes());
23        output[24..].copy_from_slice(self.stats);
24        Ok(())
25    }
26}
27pub fn decode(bytes: &[u8]) -> Result<RuntimePacket<'_>, PacketError> {
28    if bytes.len() != PACKET_BYTES || bytes[..16] != HEADER {
29        return Err(PacketError);
30    }
31    let sequence = u64::from_le_bytes(bytes[16..24].try_into().map_err(|_| PacketError)?);
32    if sequence == 0 || sequence == u64::MAX {
33        return Err(PacketError);
34    }
35    Ok(RuntimePacket {
36        sequence,
37        stats: bytes[24..].try_into().map_err(|_| PacketError)?,
38    })
39}