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

Rust API

The user-facing crate is predsync, a facade that re-exports the three implementation crates and provides the predsync CLI. The implementation is split into:

  • predsync-schema — schema types, message IDs and storage layouts. It is no_std and supports alloc as an optional feature.
  • predsync-core — bit packing, CRC, quantisation, predictors, encoder and decoder. It is no_std; alloc is a default feature and can be disabled.
  • predsync-compiler — YAML parsing, validation, schema-set collision checks and deterministic Rust/Python/C code generation.

All four crates share one version and pin each other exactly. The facade’s compiler feature implies std; embedded users should use default-features = false. The full item-by-item reference is here.

Schema tooling

predsync-compiler exposes the same pipeline used by the command line:

  • parse_schema(&str) parses one YAML document into a ParsedSchema, with the parsed SchemaSet and any non-fatal warnings.
  • check::check_sets(&[PathBuf]) parses a set of files or directories and checks message-ID collisions; check::render_table formats its report.
  • Builder chains .set(path) for one or more sets. .check() runs the collision checks; .generate(name) writes into Cargo’s OUT_DIR and emits rerun-if-changed directives; .generate_to(path) writes to an explicit path. .custom_predictor(name, path) registers custom predictor modules.
  • generate_rust, generate_rust_with_options, generate_python and generate_c generate source from an already parsed SchemaSet.

For simple Rust-only generation use predsync schema_build (see Command line). Keep a build.rs when you need custom predictor registration or generated Python/C source.

Alloc API

#![allow(unused)]
fn main() {
use predsync::{parse_schema, Decoder, DecoderConfig, Encoder, EncoderConfig, Slot, Value};

let set = parse_schema(SCHEMA).unwrap().set;
let mut encoder = Encoder::new(&set, 0, EncoderConfig::default()).unwrap();
let mut decoder = Decoder::new(&set, DecoderConfig { resync_n: 1 }).unwrap();

for tick in 0..3u64 {
    let values = [Value::Scalar(Slot::from_f64(1.25))];
    let plan = encoder.plan(&values, tick * 1_000_000).unwrap();
    let mut frame = vec![0u8; plan.len];
    let info = encoder.commit(plan, &mut frame).unwrap();
    frame.truncate(info.len);

    let decoded = decoder.feed(&frame).unwrap();
    assert_eq!(decoded.fields[0].value, values[0]);
}
}

plan is pure; commit advances encoder state. encode combines the two when you do not need to inspect the plan. The output buffer must be at least plan.len bytes, but the frame may be shorter; slice it with info.len.

Encoder::new and Decoder::new validate the schema and configuration once. Encoder::with_registry and Decoder::with_registry add runtime custom predictors; the registry is borrowed for the lifetime of the engine.

Predictors

predsync_core::predictor exports HoldLastPredictor, EwmaPredictor and the LinearPredictor, QuadraticPredictor and CubicPredictor wrappers. All implement the Predictor trait (update, predict, reset, state_hash). EwmaPredictor::new(alpha) takes a finite alpha in (0, 1] and produces a flat smoothed forecast after the first sample; alpha = 1 matches hold-last behaviour. See Predictors and tolerance for the wire-level rules.

Decoded output

Decoded contains:

  • message_index — the selected message in the schema set;
  • fields — one FieldOut per field, with validity and value;
  • missed — frames lost since the previous accepted frame;
  • resynced — whether a refresh disagreed with receiver prediction.

Validity is Valid, Predicted, Unknown or ReSync. Unknown fields have a placeholder value that must not be used as a measurement.

Fixed storage

For no_std or allocation-free hosts, use Encoder::with_storage, Decoder::with_storage, plan_into and decode_into. The compiler emits SCHEMA_REF and STORAGE_LAYOUT constants so buffer sizes are derived from the schema instead of guessed. See Embedded and no-alloc use and the borrowed-storage test in examples/generated_example/tests/generated.rs.

Generated wrappers

predsync_compiler::Builder writes a Rust module with:

  • namespace and message modules;
  • ID and FIELD_* constants;
  • one enum per schema enum with index() and from_index();
  • a struct with alloc::String / alloc::Vec<T> fields;
  • to_values() and from_values(&[Value]) -> Option<Self>;
  • SCHEMA_REF, STORAGE_LAYOUT and a generated PredictorSet.

from_values returns None for a wrong field count, wrong value kind or an out-of-range enum index. The generated module depends only on predsync_core and alloc. See Generated wrappers.

Doctests

pixi run docs-doctests compiles and runs the public examples in the crate documentation. They cover schema parsing, encoding, decoding and generated wrapper use.