diffus/diffable_impls/
borrow.rs

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
use crate::{edit, Diffable};
use std::borrow::Borrow;

fn diff_borrowable<'a, T, C, D>(left: &'a C, right: &'a C) -> edit::Edit<'a, C>
where
    T: Diffable<'a> + ?Sized + 'a,
    C: Borrow<T> + Diffable<'a, Diff = D> + ?Sized,
    D: From<T::Diff>,
{
    match left.borrow().diff(right.borrow()) {
        edit::Edit::Copy(_) => edit::Edit::Copy(left),
        edit::Edit::Change(diff) => edit::Edit::Change(diff.into()),
    }
}

macro_rules! borrow_impl {
    ($($typ:ident),*) => {
        $(
            impl<'a, T: Diffable<'a> + ?Sized + 'a> Diffable<'a> for $typ<T> {
                type Diff = $typ<T::Diff>;

                fn diff(&'a self, other: &'a Self) -> edit::Edit<'a, Self> {
                    diff_borrowable::<T, _, _>(self, other)
                }
            }
        )*
    }
}

use std::{rc::Rc, sync::Arc};
borrow_impl! {
    Box, Rc, Arc
}

impl<'a, T: Diffable<'a> + ?Sized + 'a> Diffable<'a> for &'a T {
    type Diff = T::Diff;

    fn diff(&'a self, other: &'a Self) -> edit::Edit<'a, Self> {
        diff_borrowable::<T, _, _>(self, other)
    }
}

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

    #[test]
    fn box_example() {
        let left = 13;
        let right = 37;

        if let edit::Edit::Change(diff) = Box::new(left).diff(&Box::new(right)) {
            assert_eq!(*diff, (&13, &37));
        }
    }

    #[test]
    fn rc_example() {
        let left = 13;
        let right = 37;

        if let edit::Edit::Change(diff) = Rc::new(left).diff(&Rc::new(right)) {
            assert_eq!(*diff, (&13, &37));
        }
    }

    #[test]
    fn arc_example() {
        let left = 13;
        let right = 37;

        if let edit::Edit::Change(diff) = Arc::new(left).diff(&Arc::new(right)) {
            assert_eq!(*diff, (&13, &37));
        }
    }

    #[test]
    fn reference_example() {
        let left = 13;
        let right = 37;

        if let edit::Edit::Change(diff) = (&left).diff(&(&right)) {
            assert_eq!(diff, (&13, &37));
        }
    }
}