pub struct SchemaTemplate { /* private fields */ }Expand description
A learned constant-position template for a fixed-width slot stream.
template holds the constant baseline (varying positions are zero);
var_pos lists the byte positions that vary (shipped per slot);
const_pos is the complement (checked for the escape). Both position
lists are sorted ascending.
Implementations§
Source§impl SchemaTemplate
impl SchemaTemplate
Sourcepub fn learn(sample: &[&[u8]], width: usize) -> Self
pub fn learn(sample: &[&[u8]], width: usize) -> Self
Learn a template from a sample of real slots, all width bytes. A
byte position is constant if and only if it is identical in every
sample slot; its value is recorded in the template. An empty
sample yields the identity template (every position varies, encode
is a pass-through plus one flag byte).
Sourcepub fn compact_len(&self) -> usize
pub fn compact_len(&self) -> usize
Compact size of a non-escaped slot: one flag byte plus the varying bytes.
Sourcepub fn is_escape(compact: &[u8]) -> bool
pub fn is_escape(compact: &[u8]) -> bool
Did this compact record take the escape path (the slot violated the template and shipped in full)? A rising escape rate is the signal to re-learn the template.
Sourcepub fn encode(&self, slot: &[u8], out: &mut Vec<u8>) -> usize
pub fn encode(&self, slot: &[u8], out: &mut Vec<u8>) -> usize
Encode one width-byte slot, appending the compact record to
out. Returns the number of bytes appended. Exact: a slot that
differs at a constant position is escaped in full.
Sourcepub fn decode(&self, inp: &[u8], out: &mut [u8]) -> usize
pub fn decode(&self, inp: &[u8], out: &mut [u8]) -> usize
Decode one compact record from the front of inp into out (which
must be at least width bytes). Returns the number of bytes
consumed from inp. Inverse of encode.
Sourcepub fn serialize(&self) -> Vec<u8> ⓘ
pub fn serialize(&self) -> Vec<u8> ⓘ
Serialize the template for the handshake (the “mask in one
packet”): width: u16, n_var: u16, the varying positions
(u16 each), then the width-byte constant template. Both ends
reconstruct an identical codec from these bytes.
Sourcepub fn deserialize(bytes: &[u8]) -> Option<Self>
pub fn deserialize(bytes: &[u8]) -> Option<Self>
Reconstruct a template from serialize bytes.
Returns None if the buffer is malformed.
Trait Implementations§
Source§impl Clone for SchemaTemplate
impl Clone for SchemaTemplate
Source§fn clone(&self) -> SchemaTemplate
fn clone(&self) -> SchemaTemplate
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more