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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#[cfg(feature = "nanoserde")]
use nanoserde::{DeBin, SerBin};

#[cfg(feature = "serde")]
use serde::{de::DeserializeOwned, Serialize};

pub use structdiff_derive::Difference;

pub mod collections;

pub trait StructDiff {
    /// A generated type used to represent the difference
    /// between two instances of a struct which implements
    /// the StructDiff trait.
    #[cfg(all(feature = "nanoserde", feature = "serde", feature = "debug_diffs"))]
    type Diff: SerBin + DeBin + Serialize + DeserializeOwned + Clone + std::fmt::Debug;
    #[cfg(all(feature = "nanoserde", not(feature = "serde"), feature = "debug_diffs"))]
    type Diff: SerBin + DeBin + Clone + std::fmt::Debug;
    #[cfg(all(feature = "serde", not(feature = "nanoserde"), feature = "debug_diffs"))]
    type Diff: Serialize + DeserializeOwned + Clone + std::fmt::Debug;
    #[cfg(all(
        not(feature = "serde"),
        not(feature = "nanoserde"),
        feature = "debug_diffs"
    ))]
    type Diff: Clone + std::fmt::Debug;
    #[cfg(all(feature = "nanoserde", feature = "serde", not(feature = "debug_diffs")))]
    type Diff: SerBin + DeBin + Serialize + DeserializeOwned + Clone;
    #[cfg(all(
        feature = "nanoserde",
        not(feature = "serde"),
        not(feature = "debug_diffs")
    ))]
    type Diff: SerBin + DeBin + Clone;
    #[cfg(all(
        feature = "serde",
        not(feature = "nanoserde"),
        not(feature = "debug_diffs")
    ))]
    type Diff: Serialize + DeserializeOwned + Clone;
    #[cfg(all(
        not(feature = "serde"),
        not(feature = "nanoserde"),
        not(feature = "debug_diffs")
    ))]
    type Diff: Clone;

    /// A generated type used to represent the difference
    /// between two instances of a struct which implements
    /// the StructDiff trait (using references).
    #[cfg(all(feature = "nanoserde", feature = "serde", feature = "debug_diffs"))]
    type DiffRef<'target>: SerBin + Serialize + Clone + std::fmt::Debug + Into<Self::Diff>
    where
        Self: 'target;
    #[cfg(all(feature = "nanoserde", not(feature = "serde"), feature = "debug_diffs"))]
    type DiffRef<'target>: SerBin + Clone + std::fmt::Debug + Into<Self::Diff>
    where
        Self: 'target;
    #[cfg(all(feature = "serde", not(feature = "nanoserde"), feature = "debug_diffs"))]
    type DiffRef<'target>: Serialize + Clone + std::fmt::Debug + Into<Self::Diff>
    where
        Self: 'target;
    #[cfg(all(
        not(feature = "serde"),
        not(feature = "nanoserde"),
        feature = "debug_diffs"
    ))]
    type DiffRef<'target>: Clone + std::fmt::Debug + Into<Self::Diff>
    where
        Self: 'target;
    #[cfg(all(feature = "nanoserde", feature = "serde", not(feature = "debug_diffs")))]
    type DiffRef<'target>: SerBin + Serialize + Clone + Into<Self::Diff>
    where
        Self: 'target;
    #[cfg(all(
        feature = "nanoserde",
        not(feature = "serde"),
        not(feature = "debug_diffs")
    ))]
    type DiffRef<'target>: SerBin + Clone + Into<Self::Diff>
    where
        Self: 'target;
    #[cfg(all(
        feature = "serde",
        not(feature = "nanoserde"),
        not(feature = "debug_diffs")
    ))]
    type DiffRef<'target>: Serialize + Clone + Into<Self::Diff>
    where
        Self: 'target;
    #[cfg(all(
        not(feature = "serde"),
        not(feature = "nanoserde"),
        not(feature = "debug_diffs")
    ))]
    type DiffRef<'target>: Clone + Into<Self::Diff>
    where
        Self: 'target;

    /// 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);
    /// ```
    fn diff(&self, updated: &Self) -> Vec<Self::Diff>;

    /// 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);
    /// ```
    fn diff_ref<'target>(&'target self, updated: &'target Self) -> Vec<Self::DiffRef<'target>>;

    /// 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
    where
        Self: Sized,
    {
        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);
        }
    }
}