Trait Diff

Source
pub trait Diff: Sized {
    type Repr;

    // Required methods
    fn diff(&self, other: &Self) -> Self::Repr;
    fn apply(&mut self, diff: &Self::Repr);
    fn identity() -> Self;

    // Provided methods
    fn diff_custom<D: Differ<Self>>(&self, other: &Self, visitor: &D) -> D::Repr { ... }
    fn apply_custom<D: Differ<Self>>(&mut self, diff: &D::Repr, visitor: &D) { ... }
    fn apply_new(&self, diff: &Self::Repr) -> Self { ... }
    fn apply_new_custom<D: Differ<Self>>(
        &self,
        diff: &D::Repr,
        visitor: &D,
    ) -> Self { ... }
}
Expand description

A trait to diff and apply diffs between two structs The derive macro can be used on structs when all fields of the struct implement Diff Implementations are provided for bools, numeric types, Option types, and HashMaps

Required Associated Types§

Source

type Repr

The type associated with the structs’ difference

Required Methods§

Source

fn diff(&self, other: &Self) -> Self::Repr

Produces a diff between two structs

Source

fn apply(&mut self, diff: &Self::Repr)

Applies the diff directly to the struct

Source

fn identity() -> Self

The identity element of the struct

use diff::Diff;
let s = 42;
let i = <i32 as Diff>::identity();
assert_eq!(i.apply_new(&i.diff(&s)), s);

or mathematically speaking, i + (s - i) = s

Provided Methods§

Source

fn diff_custom<D: Differ<Self>>(&self, other: &Self, visitor: &D) -> D::Repr

Produces a diff between two structs, using an external diffing implementation

Source

fn apply_custom<D: Differ<Self>>(&mut self, diff: &D::Repr, visitor: &D)

Applies the diff directly to the struct, using an external diffing implementation

Source

fn apply_new(&self, diff: &Self::Repr) -> Self

Applies the diff to the struct and produces a new struct

Source

fn apply_new_custom<D: Differ<Self>>(&self, diff: &D::Repr, visitor: &D) -> Self

Applies the diff to the struct and produces a new struct, using an external diffing implementation

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.

Implementations on Foreign Types§

Source§

impl Diff for bool

Source§

type Repr = Option<bool>

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl Diff for char

Source§

type Repr = Option<char>

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl Diff for f32

Source§

type Repr = f32

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> f32

Source§

impl Diff for f64

Source§

type Repr = f64

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> f64

Source§

impl Diff for i8

Source§

type Repr = i8

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> i8

Source§

impl Diff for i16

Source§

type Repr = i16

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> i16

Source§

impl Diff for i32

Source§

type Repr = i32

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> i32

Source§

impl Diff for i64

Source§

type Repr = i64

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> i64

Source§

impl Diff for isize

Source§

type Repr = isize

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> isize

Source§

impl Diff for u8

Source§

type Repr = u8

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> u8

Source§

impl Diff for u16

Source§

type Repr = u16

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> u16

Source§

impl Diff for u32

Source§

type Repr = u32

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> u32

Source§

impl Diff for u64

Source§

type Repr = u64

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> u64

Source§

impl Diff for usize

Source§

type Repr = usize

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> usize

Source§

impl Diff for String

Source§

type Repr = Option<String>

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl Diff for PathBuf

Source§

type Repr = Option<PathBuf>

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl Diff for NonZeroU8

Source§

type Repr = u8

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> NonZeroU8

Source§

impl Diff for NonZeroU16

Source§

type Repr = u16

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> NonZeroU16

Source§

impl Diff for NonZeroU32

Source§

type Repr = u32

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> NonZeroU32

Source§

impl Diff for NonZeroU64

Source§

type Repr = u64

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> NonZeroU64

Source§

impl Diff for NonZeroU128

Source§

type Repr = u128

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> NonZeroU128

Source§

impl Diff for NonZeroUsize

Source§

type Repr = usize

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> NonZeroUsize

Source§

impl<'a> Diff for &'a str

Source§

type Repr = Option<&'a str>

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<A> Diff for (A,)
where A: Diff,

Source§

type Repr = (<A as Diff>::Repr,)

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<A, B> Diff for (A, B)
where A: Diff, B: Diff,

Source§

type Repr = (<A as Diff>::Repr, <B as Diff>::Repr)

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<A, B, C> Diff for (A, B, C)
where A: Diff, B: Diff, C: Diff,

Source§

type Repr = (<A as Diff>::Repr, <B as Diff>::Repr, <C as Diff>::Repr)

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<A, B, C, D> Diff for (A, B, C, D)
where A: Diff, B: Diff, C: Diff, D: Diff,

Source§

type Repr = (<A as Diff>::Repr, <B as Diff>::Repr, <C as Diff>::Repr, <D as Diff>::Repr)

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<A, B, C, D, F> Diff for (A, B, C, D, F)
where A: Diff, B: Diff, C: Diff, D: Diff, F: Diff,

Source§

type Repr = (<A as Diff>::Repr, <B as Diff>::Repr, <C as Diff>::Repr, <D as Diff>::Repr, <F as Diff>::Repr)

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<A, B, C, D, F, G> Diff for (A, B, C, D, F, G)
where A: Diff, B: Diff, C: Diff, D: Diff, F: Diff, G: Diff,

Source§

type Repr = (<A as Diff>::Repr, <B as Diff>::Repr, <C as Diff>::Repr, <D as Diff>::Repr, <F as Diff>::Repr, <G as Diff>::Repr)

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<A, B, C, D, F, G, H> Diff for (A, B, C, D, F, G, H)
where A: Diff, B: Diff, C: Diff, D: Diff, F: Diff, G: Diff, H: Diff,

Source§

type Repr = (<A as Diff>::Repr, <B as Diff>::Repr, <C as Diff>::Repr, <D as Diff>::Repr, <F as Diff>::Repr, <G as Diff>::Repr, <H as Diff>::Repr)

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<A, B, C, D, F, G, H, I> Diff for (A, B, C, D, F, G, H, I)
where A: Diff, B: Diff, C: Diff, D: Diff, F: Diff, G: Diff, H: Diff, I: Diff,

Source§

type Repr = (<A as Diff>::Repr, <B as Diff>::Repr, <C as Diff>::Repr, <D as Diff>::Repr, <F as Diff>::Repr, <G as Diff>::Repr, <H as Diff>::Repr, <I as Diff>::Repr)

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<A, B, C, D, F, G, H, I, J> Diff for (A, B, C, D, F, G, H, I, J)
where A: Diff, B: Diff, C: Diff, D: Diff, F: Diff, G: Diff, H: Diff, I: Diff, J: Diff,

Source§

type Repr = (<A as Diff>::Repr, <B as Diff>::Repr, <C as Diff>::Repr, <D as Diff>::Repr, <F as Diff>::Repr, <G as Diff>::Repr, <H as Diff>::Repr, <I as Diff>::Repr, <J as Diff>::Repr)

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<A, B, C, D, F, G, H, I, J, K> Diff for (A, B, C, D, F, G, H, I, J, K)
where A: Diff, B: Diff, C: Diff, D: Diff, F: Diff, G: Diff, H: Diff, I: Diff, J: Diff, K: Diff,

Source§

type Repr = (<A as Diff>::Repr, <B as Diff>::Repr, <C as Diff>::Repr, <D as Diff>::Repr, <F as Diff>::Repr, <G as Diff>::Repr, <H as Diff>::Repr, <I as Diff>::Repr, <J as Diff>::Repr, <K as Diff>::Repr)

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<A, B, C, D, F, G, H, I, J, K, L> Diff for (A, B, C, D, F, G, H, I, J, K, L)
where A: Diff, B: Diff, C: Diff, D: Diff, F: Diff, G: Diff, H: Diff, I: Diff, J: Diff, K: Diff, L: Diff,

Source§

type Repr = (<A as Diff>::Repr, <B as Diff>::Repr, <C as Diff>::Repr, <D as Diff>::Repr, <F as Diff>::Repr, <G as Diff>::Repr, <H as Diff>::Repr, <I as Diff>::Repr, <J as Diff>::Repr, <K as Diff>::Repr, <L as Diff>::Repr)

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<A, B, C, D, F, G, H, I, J, K, L, M> Diff for (A, B, C, D, F, G, H, I, J, K, L, M)
where A: Diff, B: Diff, C: Diff, D: Diff, F: Diff, G: Diff, H: Diff, I: Diff, J: Diff, K: Diff, L: Diff, M: Diff,

Source§

type Repr = (<A as Diff>::Repr, <B as Diff>::Repr, <C as Diff>::Repr, <D as Diff>::Repr, <F as Diff>::Repr, <G as Diff>::Repr, <H as Diff>::Repr, <I as Diff>::Repr, <J as Diff>::Repr, <K as Diff>::Repr, <L as Diff>::Repr, <M as Diff>::Repr)

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<A, B, C, D, F, G, H, I, J, K, L, M, N> Diff for (A, B, C, D, F, G, H, I, J, K, L, M, N)
where A: Diff, B: Diff, C: Diff, D: Diff, F: Diff, G: Diff, H: Diff, I: Diff, J: Diff, K: Diff, L: Diff, M: Diff, N: Diff,

Source§

type Repr = (<A as Diff>::Repr, <B as Diff>::Repr, <C as Diff>::Repr, <D as Diff>::Repr, <F as Diff>::Repr, <G as Diff>::Repr, <H as Diff>::Repr, <I as Diff>::Repr, <J as Diff>::Repr, <K as Diff>::Repr, <L as Diff>::Repr, <M as Diff>::Repr, <N as Diff>::Repr)

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<A, B, C, D, F, G, H, I, J, K, L, M, N, O> Diff for (A, B, C, D, F, G, H, I, J, K, L, M, N, O)
where A: Diff, B: Diff, C: Diff, D: Diff, F: Diff, G: Diff, H: Diff, I: Diff, J: Diff, K: Diff, L: Diff, M: Diff, N: Diff, O: Diff,

Source§

type Repr = (<A as Diff>::Repr, <B as Diff>::Repr, <C as Diff>::Repr, <D as Diff>::Repr, <F as Diff>::Repr, <G as Diff>::Repr, <H as Diff>::Repr, <I as Diff>::Repr, <J as Diff>::Repr, <K as Diff>::Repr, <L as Diff>::Repr, <M as Diff>::Repr, <N as Diff>::Repr, <O as Diff>::Repr)

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<A, B, C, D, F, G, H, I, J, K, L, M, N, O, P> Diff for (A, B, C, D, F, G, H, I, J, K, L, M, N, O, P)
where A: Diff, B: Diff, C: Diff, D: Diff, F: Diff, G: Diff, H: Diff, I: Diff, J: Diff, K: Diff, L: Diff, M: Diff, N: Diff, O: Diff, P: Diff,

Source§

type Repr = (<A as Diff>::Repr, <B as Diff>::Repr, <C as Diff>::Repr, <D as Diff>::Repr, <F as Diff>::Repr, <G as Diff>::Repr, <H as Diff>::Repr, <I as Diff>::Repr, <J as Diff>::Repr, <K as Diff>::Repr, <L as Diff>::Repr, <M as Diff>::Repr, <N as Diff>::Repr, <O as Diff>::Repr, <P as Diff>::Repr)

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<A, B, C, D, F, G, H, I, J, K, L, M, N, O, P, Q> Diff for (A, B, C, D, F, G, H, I, J, K, L, M, N, O, P, Q)
where A: Diff, B: Diff, C: Diff, D: Diff, F: Diff, G: Diff, H: Diff, I: Diff, J: Diff, K: Diff, L: Diff, M: Diff, N: Diff, O: Diff, P: Diff, Q: Diff,

Source§

type Repr = (<A as Diff>::Repr, <B as Diff>::Repr, <C as Diff>::Repr, <D as Diff>::Repr, <F as Diff>::Repr, <G as Diff>::Repr, <H as Diff>::Repr, <I as Diff>::Repr, <J as Diff>::Repr, <K as Diff>::Repr, <L as Diff>::Repr, <M as Diff>::Repr, <N as Diff>::Repr, <O as Diff>::Repr, <P as Diff>::Repr, <Q as Diff>::Repr)

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<A, B, C, D, F, G, H, I, J, K, L, M, N, O, P, Q, R> Diff for (A, B, C, D, F, G, H, I, J, K, L, M, N, O, P, Q, R)
where A: Diff, B: Diff, C: Diff, D: Diff, F: Diff, G: Diff, H: Diff, I: Diff, J: Diff, K: Diff, L: Diff, M: Diff, N: Diff, O: Diff, P: Diff, Q: Diff, R: Diff,

Source§

type Repr = (<A as Diff>::Repr, <B as Diff>::Repr, <C as Diff>::Repr, <D as Diff>::Repr, <F as Diff>::Repr, <G as Diff>::Repr, <H as Diff>::Repr, <I as Diff>::Repr, <J as Diff>::Repr, <K as Diff>::Repr, <L as Diff>::Repr, <M as Diff>::Repr, <N as Diff>::Repr, <O as Diff>::Repr, <P as Diff>::Repr, <Q as Diff>::Repr, <R as Diff>::Repr)

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<A, B, C, D, F, G, H, I, J, K, L, M, N, O, P, Q, R, S> Diff for (A, B, C, D, F, G, H, I, J, K, L, M, N, O, P, Q, R, S)
where A: Diff, B: Diff, C: Diff, D: Diff, F: Diff, G: Diff, H: Diff, I: Diff, J: Diff, K: Diff, L: Diff, M: Diff, N: Diff, O: Diff, P: Diff, Q: Diff, R: Diff, S: Diff,

Source§

type Repr = (<A as Diff>::Repr, <B as Diff>::Repr, <C as Diff>::Repr, <D as Diff>::Repr, <F as Diff>::Repr, <G as Diff>::Repr, <H as Diff>::Repr, <I as Diff>::Repr, <J as Diff>::Repr, <K as Diff>::Repr, <L as Diff>::Repr, <M as Diff>::Repr, <N as Diff>::Repr, <O as Diff>::Repr, <P as Diff>::Repr, <Q as Diff>::Repr, <R as Diff>::Repr, <S as Diff>::Repr)

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<K, V> Diff for BTreeMap<K, V>
where K: Clone + Ord, V: PartialEq + Diff,

Source§

type Repr = BTreeMapDiff<K, V>

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<K, V> Diff for HashMap<K, V>
where K: Clone + Hash + Eq, V: PartialEq + Diff,

Source§

type Repr = HashMapDiff<K, V>

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<T> Diff for Cow<'_, T>
where T: ToOwned + PartialEq + ?Sized, <T as ToOwned>::Owned: Clone + Default,

Source§

type Repr = Option<<T as ToOwned>::Owned>

Note: This was done to make sure a diff is able to outlive its sources, which is the most desirable outcome if the diff is to be moved around and consumed much later (or even serialized into foreign programs)

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<T> Diff for Box<T>
where T: Diff,

Source§

type Repr = Box<<T as Diff>::Repr>

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<T> Diff for BTreeSet<T>
where T: Clone + Ord,

Source§

type Repr = BTreeSetDiff<T>

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<T> Diff for Rc<T>
where T: Diff + Clone,

Source§

type Repr = <T as Diff>::Repr

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<T> Diff for Arc<T>
where T: Diff + Clone,

Source§

type Repr = <T as Diff>::Repr

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<T> Diff for PhantomData<T>

Source§

type Repr = PhantomData<T>

Source§

fn diff(&self, _other: &Self) -> Self::Repr

Source§

fn apply(&mut self, _diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<T> Diff for HashSet<T>
where T: Clone + Hash + Eq,

Source§

type Repr = HashSetDiff<T>

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<T: Diff + PartialEq> Diff for Option<T>

Source§

type Repr = OptionDiff<T>

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<T: Diff + PartialEq> Diff for Vec<T>

Source§

type Repr = VecDiff<T>

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Source§

impl<T: Diff + PartialEq, const N: usize> Diff for [T; N]

Source§

type Repr = ArrayDiff<T>

Source§

fn diff(&self, other: &Self) -> Self::Repr

Source§

fn apply(&mut self, diff: &Self::Repr)

Source§

fn identity() -> Self

Implementors§