Skip to main content

Delta

Trait Delta 

Source
pub trait Delta {
    type Output;

    // Required methods
    fn delta(old: Self, new: Self) -> Option<Self::Output>;
    fn apply_delta(&mut self, delta: Self::Output) -> Result<(), Mismatch>;
}
Expand description

Computing the difference between two values, and applying it to a third.

You will normally derive this rather than implement it — see the crate documentation for the derive’s attributes and the shape of the type it generates. Implement it by hand when you want custom diffing for a type that other structs then reference with #[delta_struct(field_type = "delta")].

Required Associated Types§

Source

type Output

The type describing a difference between two Self values.

The derive sets this to the generated {Self}Delta struct — or, for an enum, to EnumDelta<Self, {Self}Delta>, since a value can change variant as well as change within one.

Required Methods§

Source

fn delta(old: Self, new: Self) -> Option<Self::Output>

Computes what it would take to turn old into new.

Returns None when the two are equivalent, which lets callers skip sending or storing an update that would do nothing. Both values are consumed: the delta takes ownership of whatever it needs from new.

Source

fn apply_delta(&mut self, delta: Self::Output) -> Result<(), Mismatch>

Applies a delta in place.

Applying the delta from delta(old, new) to a value equal to old yields a value equal to new — with the caveat that unordered fields preserve membership rather than order.

Fails only when the delta cannot fit the value, which only an enum can manage: a delta built for one variant, applied to a value now in another. See Mismatch. For a struct — and for an enum in the variant its delta expects — this always returns Ok.

A failure leaves the value partly updated, so treat it the way Versioned does: the value is no longer trustworthy and wants replacing wholesale, not patching again.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§