rapace_core/
header.rs

1//! Message header (in payload).
2
3use crate::Encoding;
4
5/// Message header at the start of each payload.
6///
7/// Layout: `payload = [MsgHeader][metadata...][body...]`
8#[derive(Debug, Clone, Copy)]
9#[repr(C)]
10pub struct MsgHeader {
11    /// Message format version.
12    pub version: u16,
13    /// Total header size (including this struct + metadata).
14    pub header_len: u16,
15    /// Body encoding (Postcard, Json, Raw).
16    pub encoding: u16,
17    /// Header flags (compression, etc.).
18    pub flags: u16,
19    /// Reply-to: msg_id of request.
20    pub correlation_id: u64,
21    /// Absolute deadline (nanos since epoch, 0 = none).
22    pub deadline_ns: u64,
23}
24
25/// Current message header version.
26pub const MSG_HEADER_VERSION: u16 = 1;
27
28/// Size of MsgHeader in bytes.
29pub const MSG_HEADER_SIZE: usize = core::mem::size_of::<MsgHeader>();
30
31const _: () = assert!(MSG_HEADER_SIZE == 24);
32
33impl MsgHeader {
34    /// Create a new header with default values.
35    pub const fn new() -> Self {
36        Self {
37            version: MSG_HEADER_VERSION,
38            header_len: MSG_HEADER_SIZE as u16,
39            encoding: Encoding::Postcard as u16,
40            flags: 0,
41            correlation_id: 0,
42            deadline_ns: 0,
43        }
44    }
45
46    /// Get the encoding as an enum.
47    pub fn encoding(&self) -> Option<Encoding> {
48        Encoding::from_u16(self.encoding)
49    }
50
51    /// Set the encoding.
52    pub fn set_encoding(&mut self, encoding: Encoding) {
53        self.encoding = encoding as u16;
54    }
55}
56
57impl Default for MsgHeader {
58    fn default() -> Self {
59        Self::new()
60    }
61}