Skip to main content

Module generic_diff

Module generic_diff 

Source
Expand description

Structural diff and patch via datatype-generic representation.

Works with any type implementing GenericRepr by computing diffs on the Sum/Product/Unit encoding and applying patches to transform values.

§Example

use ftui_core::generic_repr::*;
use ftui_core::generic_diff::*;

#[derive(Clone, Debug, PartialEq)]
struct Point { x: f64, y: f64 }

impl GenericRepr for Point {
    type Repr = Product<f64, Product<f64, Unit>>;
    fn into_repr(self) -> Self::Repr {
        Product(self.x, Product(self.y, Unit))
    }
    fn from_repr(repr: Self::Repr) -> Self {
        Point { x: repr.0, y: repr.1.0 }
    }
}

let old = Point { x: 1.0, y: 2.0 };
let new = Point { x: 1.0, y: 3.0 };
let diff = generic_diff(&old, &new);
let patched = generic_patch(&old, &diff);
assert_eq!(patched, new);
assert!(!diff.is_empty());

Structs§

ProductDiff
Diff of a product: diff each component independently.
UnitDiff
Diff of a Unit: always empty (nothing to compare).

Enums§

Delta
A change delta: either unchanged or replaced with a new value.
SumDiff
Diff of a sum type: either both are the same variant (structural diff) or the variant changed (full replacement).
VoidDiff
Void has no values, so Diff is trivially never called. We need the impl for sum chains to terminate.

Traits§

Diff
Compute a structural diff between two values of the same type.
DiffInfo
Metadata about a diff.
LeafDiff
Marker trait for types that use leaf-level (PartialEq) diffing.
Patch
Apply a diff to transform a value.

Functions§

generic_diff
Compute a structural diff between two values via their generic representation.
generic_patch
Apply a structural diff to transform a value via its generic representation.