Skip to main content

Delta

Derive Macro Delta 

Source
#[derive(Delta)]
{
    // Attributes available to this derive:
    #[delta_struct]
}
Expand description

Derives Delta, generating a {Self}Delta struct that holds only the changed parts of a value plus the trait implementation that produces and applies one.

The generated type takes the visibility and generic parameters of the type it is derived on, and mirrors its shape: a tuple struct’s delta is a tuple struct with its fields in the same positions, and an enum’s is an enum with one variant per diffable source variant. A struct’s delta fields are all pub.

For an enum, Output is EnumDelta<Self, {Self}Delta> rather than the bare companion, because a value can change variant as well as change within one — and changing variant is a replacement rather than a difference.

See the delta-struct crate documentation for the full picture, including trait bounds, serde usage, and limitations; what follows is the attribute reference.

§Container attributes

AttributeEffect
default = "<field type>"Field type for fields that don’t specify one. Defaults to "scalar".
delta_leader = "<tokens>"Tokens emitted directly above the generated struct — derives, doc comments, anything.

§Field attributes

AttributeEffect
field_type = "<field type>"How this field is diffed. Overrides the container’s default.
delta_leader = "<tokens>"Tokens emitted directly above the generated field.

§Field types

Each maps one source field onto exactly one delta field.

ValueDelta representationRequires
"scalar"delta_struct::ScalarDelta<T>T: PartialEq
"unordered"<T as Unordered>::DeltaBagDelta<Item> for a set, EntryDelta<Key, Value> for a mapT: Unordered
"unordered-delta"MapDelta<Key, Value, <Value as Delta>::Output>, an add, a remove, and a changeT: IntoIterator + Extend<Item> + TryIndexMut<Key, Output = Value> Item: MapEntry (so (K, V)), Value: Delta
"ordered"SeqDelta<Item>, a Myers edit scriptT: IntoIterator + FromIterator<Item>, Item: Hash + Eq
"delta"Option<<T as Delta>::Output>T: Delta

§Example

use delta_struct::Delta;

#[derive(Delta)]
#[delta_struct(delta_leader = "#[derive(Debug)]")]
struct Device {
    #[delta_struct(field_type = "unordered")]
    services: std::collections::HashSet<String>,
    online: bool,
}