Expand description
Detecting a delta that is being applied to the wrong state.
Delta::apply_delta assumes the value it is handed is equal to the old
the delta was computed from. Nothing checks that, so a dropped, reordered,
or replayed message silently diverges the two sides. Versioned wraps a
value with the bookkeeping to notice.
This module is entirely opt-in. The Delta trait, the derive, and every
generated struct are unchanged by it — if you are diffing locally or over a
reliable transport, you never have to name anything in here.
§What gets checked
Each VersionedDelta carries four numbers, and they catch different
failures:
fromandto— a sequence. A delta arriving out of order leaves a gap, and one arriving twice is recognised and ignored.base— theFingerprintof the state the sender diffed against. This catches a receiver whose value drifted for any reason at all, including one that never came through this stream.result— the fingerprint the sender expects applying to produce. This catches the delta itself being wrong.
use delta_struct::{Applied, Delta, Fingerprint, Versioned};
#[derive(Clone, Delta, Fingerprint)]
struct Config {
host: String,
port: u16,
}
let config = |port| Config { host: "localhost".to_string(), port };
let mut sender = Versioned::new(config(80));
let mut receiver = Versioned::new(config(80));
let message = sender.commit(config(8080)).expect("the port changed");
assert!(matches!(receiver.apply(message), Ok(Applied::Updated)));
assert_eq!(receiver.get().port, 8080);Structs§
- Versioned
- A value and the version it is currently at.
- Versioned
Delta - A delta, plus everything needed to tell whether it belongs here.
Enums§
- Applied
- What
Versioned::applydid. - Rejected
- Why a delta could not be applied.