Difficient
Efficient, type-safe, (almost) zero-allocation structural diffing.
- Annotate your
struct
orenum
with#[derive(Diffable)]
- Diff your value with another value of the same type
- Apply the diff!
- Serialize the diff!
Visit
the diff, generating a custom stream of change events
The possibilities are endless!
Example Usage
use ;
// here is the type we would like to diff - note `#[derive(Diffable)]`
// create an initial value
let mut source = First;
// diffing a value against itself results in no change
let diff1 = source.diff;
assert!;
// create a second value and diff
let mut target1 = Second ;
let diff2 = source.diff;
// the value is fully replaced by a different enum variant
let expect_diff = Replaced;
assert_eq!;
// 'applying' a diff to source value results in the target value
source.apply.unwrap;
assert_eq!;
// create a third value and diff
let target2 = Second ;
let diff3 = target1.diff;
// here the variant is patched but not totally replaced
let expect_diff = Patched;
assert_eq!;
target1.apply.unwrap;
assert_eq!;
// It works for structs too!
let mut source = SimpleStruct ;
let target = SimpleStruct ;
let diff = source.diff;
assert!; // unchanged because `c` is ignored
Visitor
With the visitor
feature activated, we can generate a series of discrete changes from the Diff
type.
See examples/visitor.rs
for a worked example.
Efficient?
The diff function does not allocate, in most cases. This makes it extremely fast.
The exceptions are
- when dealing with boxed types
- when dealing collections (
Vec
,HashMap
orBTreeMap
)
Then it may have to allocate, which makes it merely very fast.