nativeview_store/wire/mod.rs
1//! `NV02`: closed, 4-byte-aligned, table-major wire for one committed frame.
2//!
3//! Used only where a frame crosses a process / `dlopen` / network / replay boundary; in-process
4//! backends read `&NodeStore` + `&ChangeSet` directly.
5//!
6//! ```text
7//! header : magic "NV02" u32 | version u16 | flags u16 | frame u64 | op_count u32 | words u32
8//! op : head u32 (opcode u8 | table u8 | count_u16; 0xFFFF => u32 count word follows) | payload_words u32 | payload...
9//! ```
10//! Ops are emitted (and must be applied) in this order: `DeclareTables`, `CreateNodes`,
11//! `DefineResources`, per table `Patch` / `SetStrings` / `Detach`, `SetChildren`, `RemoveNodes`,
12//! `ReleaseResources`. The receiving store is a replica: node and resource ids are mirrored
13//! exactly. Table slots are session-scoped: a sender declares each table key once per
14//! [`WireEncoder`] session and refers to it by its wire slot afterwards.
15
16pub(crate) mod budget;
17mod decode;
18mod encode;
19
20pub use decode::WireDecoder;
21pub use encode::{encode_declare_table_entry, DeclareTableWire, WireEncoder};
22
23/// `"NV02"` little-endian.
24pub const MAGIC: u32 = u32::from_le_bytes(*b"NV02");
25pub const VERSION: u16 = 3;
26/// Header size in words.
27pub const HEADER_WORDS: usize = 6;
28
29/// The closed opcode set.
30#[repr(u8)]
31#[derive(Debug, Clone, Copy, PartialEq, Eq, crate::ReprEnum)]
32pub enum Op {
33 DeclareTables = 1,
34 RemoveNodes = 2,
35 CreateNodes = 3,
36 DefineResources = 4,
37 ReleaseResources = 5,
38 Patch = 6,
39 Detach = 7,
40 SetStrings = 8,
41 SetChildren = 9,
42}
43
44/// Decoding / apply failure. The replica must be discarded (or resynced) after an error.
45#[derive(Debug, Clone, PartialEq, Eq)]
46pub enum WireError {
47 BadMagic(u32),
48 UnsupportedVersion(u16),
49 UnsupportedFlags(u16),
50 Truncated,
51 TrailingBytes,
52 Misaligned,
53 UnknownOp(u8),
54 /// Wire slot used before being declared.
55 UndeclaredTable(u8),
56 /// A declared key exists locally with another schema / words, or violates table invariants.
57 SchemaMismatch {
58 key: u64,
59 },
60 TableRegistry,
61 NodeExists(u32),
62 DeadNode(u32),
63 InvalidChildren,
64 BadMask {
65 mask: u32,
66 words: u32,
67 },
68 InvalidUtf8,
69 /// Resource definition failed validation (bounds, alignment, flags, missing child, or cycle).
70 Resource {
71 id: u32,
72 kind: u64,
73 },
74 /// Session or frame slot-memory budget exceeded while decoding a wire frame.
75 Budget,
76 /// Transient OS memory reservation failed while decoding or pre-sizing a frame.
77 Alloc,
78}
79
80impl core::fmt::Display for WireError {
81 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
82 write!(f, "NV02: {self:?}")
83 }
84}
85
86impl std::error::Error for WireError {}
87
88#[inline(always)]
89pub(crate) const fn pad4(n: usize) -> usize {
90 (n + 3) & !3
91}
92
93#[inline(always)]
94pub(crate) const fn pad4_checked(n: usize) -> Option<usize> {
95 match n.checked_add(3) {
96 Some(v) => Some(v & !3),
97 None => None,
98 }
99}
100
101#[cfg(test)]
102mod resource_tests;
103#[cfg(test)]
104mod tests;