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
/// A trait for computing the difference between two objects.
pub trait Diff<K: Ord + Clone, V: Clone> {
    /// The type that describes the difference between two objects.
    type DiffType;

    /// Returns a [Self::DiffType] object that represents the difference between this object and
    /// other.
    fn diff(&self, other: &Self) -> Self::DiffType;
}

/// A trait for applying the difference between two objects.
pub trait ApplyDiff<K: Ord + Clone, V: Clone> {
    /// The type that describes the difference between two objects.
    type DiffType;

    /// Applies the provided changes described by [Self::DiffType] to the object implementing this trait.
    fn apply(&mut self, diff: Self::DiffType);
}

/// A trait for applying the difference between two objects with the possibility of failure.
pub trait TryApplyDiff<K: Ord + Clone, V: Clone> {
    /// The type that describes the difference between two objects.
    type DiffType;

    /// An error type that can be returned if the changes cannot be applied.
    type Error;

    /// Applies the provided changes described by [Self::DiffType] to the object implementing this trait.
    /// Returns an error if the changes cannot be applied.
    fn try_apply(&mut self, diff: Self::DiffType) -> Result<(), Self::Error>;
}