structdiff
A lightweight, zero-dependency struct diffing library which allows changed fields to be collected and applied. Derive Difference
on a struct, then use the StructDiff
trait to make and apply diffs.
Example:
use ;
let first = Example ;
let second = Example ;
let diffs = first.diff;
// diffs is now a Vec of differences between the two instances,
// with length equal to number of changed/unskipped fields
assert_eq!;
let diffed = first.apply;
// diffed is now equal to second, except for skipped field
assert_eq!;
assert_eq!;
assert_ne!;
For more examples take a look at integration tests
Derive macro attributes
#[difference(skip)]
- Do not consider this field when creating a diff#[difference(recurse)]
- Generate a StructDiff for this field when creating a diff#[difference(collection_strategy = {})]
"unordered_array_like"
- Generates a changeset for array-like collections of items which implementHash + Eq
, rather than cloning the entire list. (currently works forVec
,BTreeSet
,LinkedList
, andHashSet
)"unordered_map_like"
- Generates a changeset for map-like collections for which the key implementsHash + Eq
, rather than cloning the entire map. (currently works forHashMap
andBTreeMap
)
#[difference(map_equality = {})]
- Used withunordered_map_like
, specifies whether the equality check should consider keys only, or both keys and values"key_only"
- only replace a key-value pair for which the key has changed"key_and_value"
- replace a key-value pair if either the key or value has changed
Optional features
- [
nanoserde
,serde
] - Serialization ofDifference
derived associated types. Allows diffs to easily be sent over network.
Development status
This is being used actively for my own projects, although it's mostly working now. PRs will be accepted for either more tests or functionality.