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 struct takes the visibility and generic parameters of the type it is derived on, and all of its fields are pub. A tuple struct’s delta is a tuple struct in turn, with its fields in the same positions.

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"Option<T>T: PartialEq
"unordered"BagDelta<Item>, an add and a removeT: IntoIterator + Extend<Item> + TryIndex<Item, Output = Item>
"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,
}