Skip to main content

predsync_core/
lib.rs

1//! Deterministic encoder and decoder for low-bandwidth predictor-state sync.
2//!
3//! The crate is `no_std`. With the default `alloc` feature, [`Encoder`] and
4//! [`Decoder`] accept owned [`Value`]s and allocate output buffers for you.
5//! Disable `alloc` and use `with_storage`/`plan_into`/`decode_into` to supply
6//! every scratch buffer yourself. The wire format is identical on both paths.
7//!
8//! ```
9//! use predsync_core::Slot;
10//!
11//! let slot = Slot::from_f64(1.25);
12//! assert_eq!(slot.as_f64(), 1.25);
13//! ```
14//!
15//! See the `predsync-compiler` crate for YAML parsing and typed wrapper
16//! generation, and the book in `docs/book` for the protocol contract.
17
18#![no_std]
19
20#[cfg(feature = "alloc")]
21extern crate alloc;
22
23#[cfg(feature = "std")]
24extern crate std;
25
26pub mod bits;
27pub mod crc;
28pub mod decoder;
29pub mod encoder;
30pub mod hash;
31pub mod managed;
32pub mod opaque;
33pub mod predictor;
34pub mod quant;
35#[cfg(feature = "alloc")]
36pub mod registry;
37pub mod slot;
38pub mod storage;
39pub mod tol;
40pub mod value;
41
42#[cfg(feature = "alloc")]
43pub use decoder::Decoded;
44pub use decoder::Decoder;
45pub use decoder::{DecodeError, DecoderConfig};
46pub use encoder::Encoder;
47#[cfg(feature = "alloc")]
48pub use encoder::Plan;
49pub use encoder::{
50    ConfigError, EncodeError, EncodeInfo, EncoderConfig, PredictorBuilder, Presence,
51};
52#[cfg(feature = "alloc")]
53pub use predsync_schema::FieldSchema;
54pub use predsync_schema::{self as schema, PredictorKind};
55#[cfg(feature = "alloc")]
56pub use registry::PredictorRegistry;
57pub use slot::{ScalarType, Slot};
58#[cfg(feature = "alloc")]
59pub use value::Value;
60pub use value::{BoundedValue, ValueRef};
61
62/// The trust state of one decoded field.
63#[derive(Debug, Clone, Copy, PartialEq, Eq)]
64pub enum Validity {
65    /// The value was received or checked this frame.
66    Valid,
67    /// The value came from the receiver's predictor.
68    Predicted,
69    /// No trustworthy value is available.
70    Unknown,
71    /// A refresh did not match the receiver's prediction.
72    ReSync,
73}
74
75/// One decoded field and its trust state.
76#[cfg(feature = "alloc")]
77#[derive(Debug, Clone, PartialEq, Eq)]
78pub struct FieldOut {
79    pub validity: Validity,
80    pub value: Value,
81}
82
83/// Returns a fixed value used by the M0 scaffolding smoke test.
84pub fn core_smoke_value() -> u8 {
85    42
86}
87
88#[cfg(test)]
89mod tests {
90    #[test]
91    fn smoke_test() {
92        assert_eq!(crate::core_smoke_value(), 42);
93    }
94}