Rust quickstart
Add the facade crate to your application:
[dependencies]
predsync = "=0.1.0"
predsync re-exports predsync_core as predsync::core,
predsync_schema as predsync::schema and predsync_compiler as
predsync::compiler. The default features enable alloc and compiler.
Embedded applications should set default-features = false, optionally enable
alloc, and use generated wrappers instead of
runtime YAML parsing.
use predsync::{parse_schema, Decoder, DecoderConfig, Encoder, EncoderConfig, Slot, Value};
const SCHEMA: &str = r#"
name: telemetry
sync:
refresh: 8
reset: 32
fields:
- name: x
type: f64
quantise: 0.01
range: [-100.0, 100.0]
predictor: { type: linear, window: 4 }
tolerance: { abs: 0.01 }
"#;
fn main() {
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];
encoder.commit(plan, &mut frame).unwrap();
let decoded = decoder.feed(&frame).unwrap();
assert_eq!(decoded.fields[0].value, values[0]);
}
}
plan decides which fields are sent; commit writes the bytes. Splitting the
two calls lets you size a fixed output buffer from plan.len. Use
Encoder::encode when a Vec is acceptable.
The first frame is a startup Reset. Later frames omit x while the prediction
stays within abs: 0.01 of the quantised truth. Decoder::feed takes no wall
clock argument: it derives predictor time only from the wire ticks.
Multiple senders
Frames carry no sender identity. Give each sender its own Encoder, and give
each incoming stream its own Decoder. The optional message-ID byte selects one
message type inside a single stream; it does not select a sender.
No allocation
For fixed-storage targets, use Encoder::with_storage, Decoder::with_storage
and the generated SCHEMA_REF/STORAGE_LAYOUT. See
Embedded and no-alloc use and the
generated example.