StructDiff

Trait StructDiff 

Source
pub trait StructDiff {
    type Diff: StructDiffOwnedBound;
    type DiffRef<'target>: StructDiffRefBound + Into<Self::Diff>
       where Self: 'target;

    // Required methods
    fn diff(&self, updated: &Self) -> Vec<Self::Diff>;
    fn diff_ref<'target>(
        &'target self,
        updated: &'target Self,
    ) -> Vec<Self::DiffRef<'target>>;
    fn apply_single(&mut self, diff: Self::Diff);

    // Provided methods
    fn apply(self, diffs: Vec<Self::Diff>) -> Self
       where Self: Sized { ... }
    fn apply_ref(&self, diffs: Vec<Self::Diff>) -> Self
       where Self: Clone { ... }
    fn apply_mut(&mut self, diffs: Vec<Self::Diff>) { ... }
}

Required Associated Types§

Source

type Diff: StructDiffOwnedBound

A generated type used to represent the difference between two instances of a struct which implements the StructDiff trait.

Source

type DiffRef<'target>: StructDiffRefBound + Into<Self::Diff> where Self: 'target

A generated type used to represent the difference between two instances of a struct which implements the StructDiff trait (using references).

Required Methods§

Source

fn diff(&self, updated: &Self) -> Vec<Self::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: Vec<<Example as StructDiff>::Diff> = first.diff(&second);

let diffed = first.apply(diffs);
assert_eq!(diffed, second);
Source

fn diff_ref<'target>( &'target self, updated: &'target Self, ) -> Vec<Self::DiffRef<'target>>

Generate a diff between two instances of a struct, for use in passing to serializer. Much more efficient for structs with large fields where the diff will not be stored.

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: Vec<<Example as StructDiff>::DiffRef<'_>> = first.diff_ref(&second);

let diffed = first.clone().apply(diffs.into_iter().map(Into::into).collect());
assert_eq!(diffed, second);
Source

fn apply_single(&mut self, diff: Self::Diff)

Apply a single-field diff to a mutable self ref

Provided Methods§

Source

fn apply(self, diffs: Vec<Self::Diff>) -> Self
where Self: Sized,

Apply a full diff to an owned self

Source

fn apply_ref(&self, diffs: Vec<Self::Diff>) -> Self
where Self: Clone,

Apply a full diff to a self ref, returning a cloned version of self after diff is applied

Source

fn apply_mut(&mut self, diffs: Vec<Self::Diff>)

Apply a full diff to a mutable self ref

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§