visit_diff 0.1.1

Efficiently finding differences between data structures.
Documentation
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use super::*;

////////////////////////////////////////////////////////////////////////////////
// Unit-shaped things

impl Diff for () {
    fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
    where
        D: Differ,
    {
        out.same(a, b)
    }
}

impl<T: ?Sized> Diff for core::marker::PhantomData<T> {
    fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
    where
        D: Differ,
    {
        out.same(a, b)
    }
}

////////////////////////////////////////////////////////////////////////////////
// Tuple boilerplate

macro_rules! tuple_impl {
    ($($p:ident / $n:tt),*) => {
        impl<$($p),*> Diff for ($($p,)*)
        where
            $($p: Diff),*
        {
            fn diff<DD>(a: &Self, b: &Self, out: DD) -> Result<DD::Ok, DD::Err>
            where
                DD: Differ,
            {
                let mut out = out.begin_tuple("");
                $(out.diff_field(&a.$n, &b.$n);)*
                out.end()
            }
        }
    };
}

tuple_impl!(A / 0);
tuple_impl!(A / 0, B / 1);
tuple_impl!(A / 0, B / 1, C / 2);
tuple_impl!(A / 0, B / 1, C / 2, D / 3);
tuple_impl!(A / 0, B / 1, C / 2, D / 3, E / 4);
tuple_impl!(A / 0, B / 1, C / 2, D / 3, E / 4, F / 5);
tuple_impl!(A / 0, B / 1, C / 2, D / 3, E / 4, F / 5, G / 6);
tuple_impl!(A / 0, B / 1, C / 2, D / 3, E / 4, F / 5, G / 6, H / 7);
tuple_impl!(
    A / 0,
    B / 1,
    C / 2,
    D / 3,
    E / 4,
    F / 5,
    G / 6,
    H / 7,
    I / 8
);

////////////////////////////////////////////////////////////////////////////////
// Slice and array boilerplate

impl<T> Diff for [T]
where
    T: Diff,
{
    fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
    where
        D: Differ,
    {
        let mut s = out.begin_seq();
        s.diff_elements(a, b);
        s.end()
    }
}

macro_rules! array_impl {
    ($n:tt) => {
        impl<T> Diff for [T; $n]
        where
            T: Diff,
        {
            fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
            where
                D: Differ,
            {
                Diff::diff(a as &[T], b as &[T], out)
            }
        }
    };
}

array_impl!(0);
array_impl!(1);
array_impl!(2);
array_impl!(3);
array_impl!(4);
array_impl!(5);
array_impl!(6);
array_impl!(7);
array_impl!(8);
array_impl!(9);
array_impl!(10);
array_impl!(11);
array_impl!(12);
array_impl!(13);
array_impl!(14);
array_impl!(15);
array_impl!(16);
array_impl!(17);
array_impl!(18);
array_impl!(19);
array_impl!(20);
array_impl!(21);
array_impl!(22);
array_impl!(23);
array_impl!(24);
array_impl!(25);
array_impl!(26);
array_impl!(27);
array_impl!(28);
array_impl!(29);
array_impl!(30);
array_impl!(31);
array_impl!(32);

////////////////////////////////////////////////////////////////////////////////
// References

/// Diff references by dereferencing.
impl<T> Diff for &T
where
    T: Diff + ?Sized,
{
    fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
    where
        D: Differ,
    {
        Diff::diff(*a, *b, out)
    }
}

/// Diff references by dereferencing.
impl<T> Diff for &mut T
where
    T: Diff + ?Sized,
{
    fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
    where
        D: Differ,
    {
        Diff::diff(*a, *b, out)
    }
}

impl<'a, T: ?Sized + Diff> Diff for core::cell::Ref<'a, T> {
    fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
    where
        D: Differ,
    {
        Diff::diff(&**a, &**b, out)
    }
}

impl<'a, T: ?Sized + Diff> Diff for core::cell::RefMut<'a, T> {
    fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
    where
        D: Differ,
    {
        Diff::diff(&**a, &**b, out)
    }
}

////////////////////////////////////////////////////////////////////////////////
// "Atomic" types that can be diffed using PartialEq.

macro_rules! impl_diff_partial_eq {
    // Specialization for unsized types: adds a reference on the way to the
    // differ.
    (unsized $ty:ty) => {
        impl Diff for $ty {
            fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
            where
                D: Differ,
            {
                if a != b {
                    out.difference(&a, &b)
                } else {
                    out.same(&a, &b)
                }
            }
        }
    };
    ($ty:ty | $p:ident) => {
        impl<$p> Diff for $ty
        where
            $ty: PartialEq + Debug,
        {
            fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
            where
                D: Differ,
            {
                if a != b {
                    out.difference(&a, &b)
                } else {
                    out.same(&a, &b)
                }
            }
        }
    };
    ($ty:ty) => {
        impl Diff for $ty {
            fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
            where
                D: Differ,
            {
                if a != b {
                    out.difference(a, b)
                } else {
                    out.same(a, b)
                }
            }
        }
    };
}

impl_diff_partial_eq!(bool);
impl_diff_partial_eq!(char);
impl_diff_partial_eq!(u8);
impl_diff_partial_eq!(u16);
impl_diff_partial_eq!(u32);
impl_diff_partial_eq!(u64);
impl_diff_partial_eq!(u128);
impl_diff_partial_eq!(usize);
impl_diff_partial_eq!(i8);
impl_diff_partial_eq!(i16);
impl_diff_partial_eq!(i32);
impl_diff_partial_eq!(i64);
impl_diff_partial_eq!(i128);
impl_diff_partial_eq!(isize);
impl_diff_partial_eq!(f32);
impl_diff_partial_eq!(f64);
impl_diff_partial_eq!(unsized str);
impl_diff_partial_eq!(core::cmp::Ordering);
impl_diff_partial_eq!(core::time::Duration);

// Ranges are treated as atomic values in this version, because they have
// strange Debug impls that would otherwise require explicit support in the
// Differ traits.
impl_diff_partial_eq!(core::ops::Range<T> | T);
impl_diff_partial_eq!(core::ops::RangeFrom<T> | T);
impl_diff_partial_eq!(core::ops::RangeFull);
impl_diff_partial_eq!(core::ops::RangeTo<T> | T);
impl_diff_partial_eq!(core::ops::RangeToInclusive<T> | T);

/// Pointers diff by address, not by contents.
impl<T: ?Sized> Diff for *const T {
    fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
    where
        D: Differ,
    {
        if a != b {
            out.difference(a, b)
        } else {
            out.same(a, b)
        }
    }
}

/// Pointers diff by address, not by contents.
impl<T: ?Sized> Diff for *mut T {
    fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
    where
        D: Differ,
    {
        if a != b {
            out.difference(a, b)
        } else {
            out.same(a, b)
        }
    }
}

////////////////////////////////////////////////////////////////////////////////
// Trivial containers and cells. The trivial containers in core vary on whether
// they want to be represented as a simple newtype, or as a struct containing a
// single field. We follow their Debug implementations.

impl<T: Copy + Diff> Diff for core::cell::Cell<T> {
    fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
    where
        D: Differ,
    {
        let mut out = out.begin_struct("Cell");
        out.diff_field("value", &a.get(), &b.get());
        out.end()
    }
}

impl<T: ?Sized + Diff> Diff for core::mem::ManuallyDrop<T> {
    fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
    where
        D: Differ,
    {
        let mut out = out.begin_struct("ManuallyDrop");
        out.diff_field("value", &*a, &*b);
        out.end()
    }
}

impl<T: Diff> Diff for core::num::Wrapping<T> {
    fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
    where
        D: Differ,
    {
        Diff::diff(&a.0, &b.0, out)
    }
}

/// Note that this *will* panic if the RefCell is mutably borrowed.
impl<T: ?Sized + Diff> Diff for core::cell::RefCell<T> {
    fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
    where
        D: Differ,
    {
        let mut out = out.begin_struct("RefCell");
        out.diff_field("value", &*a.borrow(), &*b.borrow());
        out.end()
    }
}

impl<T: Diff> Diff for core::option::Option<T> {
    fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
    where
        D: Differ,
    {
        match (a, b) {
            (None, None) => out.same(a, b),
            (Some(a), Some(b)) => {
                let mut out = out.begin_tuple_variant("Option", "Some");
                out.diff_field(a, b);
                out.end()
            }
            _ => out.difference(a, b),
        }
    }
}

impl<T: Diff, E: Diff> Diff for core::result::Result<T, E> {
    fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
    where
        D: Differ,
    {
        match (a, b) {
            (Ok(a), Ok(b)) => {
                let mut out = out.begin_tuple_variant("Result", "Ok");
                out.diff_field(a, b);
                out.end()
            }
            (Err(a), Err(b)) => {
                let mut out = out.begin_tuple_variant("Result", "Err");
                out.diff_field(a, b);
                out.end()
            }
            _ => out.difference(a, b),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[allow(unused)]
    #[derive(Clone, Debug)]
    pub enum TestEnum {
        First,
        Second,
        Struct { a: usize, b: bool },
    }

    impl Diff for TestEnum {
        fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
        where
            D: Differ,
        {
            match (a, b) {
                (TestEnum::First, TestEnum::First) => out.same(a, b),
                (TestEnum::Second, TestEnum::Second) => out.same(a, b),
                (
                    TestEnum::Struct { a: aa, b: ab },
                    TestEnum::Struct { a: ba, b: bb },
                ) => {
                    let mut s = out.begin_struct_variant("TestEnum", "Struct");
                    s.diff_field("a", &aa, &ba);
                    s.diff_field("b", &ab, &bb);
                    s.end()
                }
                _ => out.difference(a, b),
            }
        }
    }

    #[derive(Clone, Debug)]
    pub struct TestStruct {
        pub distance: usize,
        pub silly: bool,
    }

    impl Diff for TestStruct {
        fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
        where
            D: Differ,
        {
            let mut s = out.begin_struct("TestStruct");
            s.diff_field("distance", &a.distance, &b.distance);
            s.diff_field("silly", &a.silly, &b.silly);
            s.end()
        }
    }
}