Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Value encoding

Slots

All scalar values cross the Rust API as Slot(u64):

  • unsigned integers are zero-extended;
  • signed integers are sign-extended by as u64;
  • bool is 0 or 1;
  • f32 is f32::to_bits() as u64;
  • f64 is f64::to_bits();
  • an enum is its zero-based variant index.

Value wraps a scalar slot, a list of slots, or a UTF-8 string. A decoded field also carries a Validity.

Final values and quantisation

For a field with quantise: step, the sender computes q = round_half_away_from_zero(v / step). If range: [lo, hi] is declared, the quantised bounds are round(lo / step) and round(hi / step) and q is clamped to them. Otherwise q is clamped to the signed range of the wire width. Clamping sets bound_applies = false for that field and frame.

The receiver emits the final value:

  • quantised field: cast(q * step);
  • unquantised field: cast(v);
  • f32: round to f32, then widen to f64;
  • integer: round half away from zero, then saturate to the type’s range;
  • bool: 1.0 when x >= 0.5, otherwise 0.0.

Both ends update their predictors with final values, never with raw input. That is what makes the tolerance check meaningful: it compares the sender’s true value as the receiver would emit it with the value the receiver actually emits.

Wire widths

Field kindWidth and content
Quantised with rangemax(1, ceil_log2(q_hi - q_lo + 1)) bits, unsigned q - q_lo
Quantised without rangesigned q: 32 bits for f32, 64 bits for f64, type width for integers
Not quantisedraw bits: bool 1, integer width, f32 32, f64 64
Enummax(1, ceil_log2(n_variants)) bits, unsigned variant index

ceil_log2(1) is 0, so a single variant uses one bit by the max(1, …) rule. An index at or above the variant count is Malformed on decode.

Strings and lists

A variable-length field starts with an unsigned 16-bit length, LSB first. A string’s length counts UTF-8 bytes; a list’s length counts elements. Elements are bit-packed with no byte alignment and use the rules of their element type; quantisation and range apply to every list element.

Empty strings and lists are valid. The encoder rejects a value longer than max_len with TooLong. The decoder rejects a wire length above max_len, an element count whose bits would overrun the frame, and invalid UTF-8 as Malformed. These checks happen before allocation or state mutation.

Non-finite values

NaN and infinities are rejected by the encoder with EncodeError::NonFinite(field_index) and leave encoder state unchanged. Custom callback predictors must also return finite predictions; check_predictor checks this property.