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. Supports optional serialization of the generated diff types with serde or nanoserde for ease of use.
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
- Field level
#[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 = {})]"ordered_array_like"- Generates a minimal changeset for ordered, array-like collections of items which implementPartialEq. (uses levenshtein difference)"unordered_array_like"- Generates a minimal changeset for unordered, array-like collections of items which implementHash + Eq."unordered_map_like"- Generates a minimal changeset for unordered, map-like collections for which the key implementsHash + Eq.
#[difference(map_equality = {})]- Used withunordered_map_like"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
#[difference(setter)]- Generate setters for this struct field#[difference(setter_name = {})]- Use this name instead of the default value when generating a setter for this field (used on field)
- Struct Level
#[difference(setters)]- Generate setters for all fields in the struct- Example: for the
field1of theExamplestruct used above, a function with the signatureset_field1_with_diff(&mut self, value: Option<usize>) -> Option<<Self as StructDiff>::Diff>will be generated. Useful when a single field will be changed in a struct with many fields, as it saves the comparison of all other fields.
- Example: for the
#[difference(expose)]/#[difference(expose = "MyDiffTypeName")]- expose the generated difference type (optionally, with the specified name)
Optional features
- [
nanoserde,serde] - Serialization ofDifferencederived associated types. Allows diffs to easily be sent over network. debug_diffs- DeriveDebugon the generated diff typegenerated_setters- Enable generation of setters for struct fields. These setters automatically return a diff if a field's value is changed by the assignment.rustc_hash- Use the (non-cryptographic) hash implementation from therustc-hashcrate instead of the default hasher. Much faster diff generation for collections at the cost of a dependency.
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.