1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
pub use structdiff_derive::Difference;
pub mod collections;
pub trait StructDiff: PartialEq + Sized {
/// A generated type used to represent the difference
/// between two instances of a struct which implements
/// the StructDiff trait.
type Diff;
/// Generate a diff between two instances of a struct.
/// This diff may be serialized if one of the serialization
/// features is enabled.
///
/// ```
/// use structdiff::{Difference, StructDiff};
///
/// #[derive(Debug, PartialEq, Clone, Difference)]
/// struct Example {
/// field1: f64,
/// }
///
/// let first = Example {
/// field1: 0.0,
/// };
///
/// let second = Example {
/// field1: 3.14,
/// };
///
/// let diffs = first.diff(&second);
///
/// let diffed = first.apply(diffs);
/// assert_eq!(diffed, second);
/// ```
fn diff(&self, updated: &Self) -> Vec<Self::Diff>;
/// Apply a single-field diff to a mutable self ref
fn apply_single(&mut self, diff: Self::Diff);
/// Apply a full diff to an owned self
fn apply(mut self, diffs: Vec<Self::Diff>) -> Self {
for diff in diffs {
self.apply_single(diff);
}
self
}
/// Apply a full diff to a self ref, returning a cloned version of self
/// after diff is applied
fn apply_ref(&self, diffs: Vec<Self::Diff>) -> Self
where
Self: Clone,
{
self.clone().apply(diffs)
}
/// Apply a full diff to a mutable self ref
fn apply_mut(&mut self, diffs: Vec<Self::Diff>) {
for diff in diffs {
self.apply_single(diff);
}
}
}