| 1 | //! Implementations for core types. |
| 2 | |
| 3 | use crate::{Diffable, Leaf}; |
| 4 | use core::{ |
| 5 | cell::RefCell, |
| 6 | marker::PhantomData, |
| 7 | net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}, |
| 8 | num::{ |
| 9 | NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, |
| 10 | NonZeroIsize, NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, |
| 11 | NonZeroU8, NonZeroUsize, |
| 12 | }, |
| 13 | }; |
| 14 | |
| 15 | leaf! { i64, i32, i16, i8, u64, u32, u16, u8, char, bool, isize, usize, NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize, NonZeroUsize, () } |
| 16 | leaf! { IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6 } |
| 17 | leaf! { str } |
| 18 | |
| 19 | impl<T> Diffable for Option<T> { |
| 20 | type Diff<'daft> |
| 21 | = Leaf<Option<&'daft T>> |
| 22 | where |
| 23 | T: 'daft; |
| 24 | |
| 25 | fn diff<'daft>(&'daft self, other: &'daft Self) -> Self::Diff<'daft> { |
| 26 | Leaf { before: self.as_ref(), after: other.as_ref() } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | impl<T, U> Diffable for Result<T, U> { |
| 31 | type Diff<'daft> |
| 32 | = Leaf<Result<&'daft T, &'daft U>> |
| 33 | where |
| 34 | T: 'daft, |
| 35 | U: 'daft; |
| 36 | fn diff<'daft>(&'daft self, other: &'daft Self) -> Self::Diff<'daft> { |
| 37 | Leaf { before: self.as_ref(), after: other.as_ref() } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | impl<'a, T: Diffable + ?Sized> Diffable for &'a T { |
| 42 | // It would be nice to extend the lifetime of the diff to `'a` (e.g. diffing |
| 43 | // &'static str`s results in a Leaf<&'static str>), and it does actually |
| 44 | // work for that simpler case, but rustc (1.84.1) complains about cases with |
| 45 | // multiple references like &'a &'b T (see complex-lifetimes.rs for an |
| 46 | // example). This is probably a bug in rustc: see |
| 47 | // https://gist.github.com/sunshowers/25fcc2f590f1c6b19daa99e11e927dc7 for |
| 48 | // the error with Rust 1.84.1. |
| 49 | type Diff<'daft> |
| 50 | = <T as Diffable>::Diff<'daft> |
| 51 | where |
| 52 | 'a: 'daft; |
| 53 | |
| 54 | fn diff<'daft>(&'daft self, other: &'daft Self) -> Self::Diff<'daft> { |
| 55 | (**self).diff(other) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Can't express lifetimes due to `RefCell`'s limited borrows, so we must return |
| 60 | // a leaf node that can be recursively diffed. |
| 61 | impl<T: ?Sized> Diffable for RefCell<T> { |
| 62 | type Diff<'daft> |
| 63 | = Leaf<&'daft Self> |
| 64 | where |
| 65 | T: 'daft; |
| 66 | |
| 67 | fn diff<'daft>(&'daft self, other: &'daft Self) -> Self::Diff<'daft> { |
| 68 | Leaf { before: self, after: other } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | impl<T: ?Sized> Diffable for PhantomData<T> { |
| 73 | type Diff<'daft> |
| 74 | = Leaf<&'daft PhantomData<T>> |
| 75 | where |
| 76 | Self: 'daft; |
| 77 | |
| 78 | fn diff<'daft>(&'daft self, other: &'daft Self) -> Self::Diff<'daft> { |
| 79 | Leaf { before: self, after: other } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /// Treat slices as leaf nodes. |
| 84 | impl<T: Diffable> Diffable for [T] { |
| 85 | type Diff<'daft> |
| 86 | = Leaf<&'daft [T]> |
| 87 | where |
| 88 | T: 'daft; |
| 89 | |
| 90 | fn diff<'daft>(&'daft self, other: &'daft Self) -> Self::Diff<'daft> { |
| 91 | Leaf { before: self, after: other } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | macro_rules! tuple_diffable { |
| 96 | ($(($($name:ident $ix:tt),+)),+) => { |
| 97 | $( |
| 98 | impl<$($name: Diffable),+> Diffable for ($($name,)+) { |
| 99 | type Diff<'daft> = ($($name::Diff<'daft>,)+) |
| 100 | where |
| 101 | $($name: 'daft,)+; |
| 102 | |
| 103 | fn diff<'daft>(&'daft self, other: &'daft Self) -> Self::Diff<'daft> { |
| 104 | ($(self.$ix.diff(&other.$ix),)+) |
| 105 | } |
| 106 | } |
| 107 | )+ |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | tuple_diffable! { |
| 112 | (A 0), |
| 113 | (A 0, B 1), |
| 114 | (A 0, B 1, C 2), |
| 115 | (A 0, B 1, C 2, D 3), |
| 116 | (A 0, B 1, C 2, D 3, E 4), |
| 117 | (A 0, B 1, C 2, D 3, E 4, F 5), |
| 118 | (A 0, B 1, C 2, D 3, E 4, F 5, G 6), |
| 119 | (A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7), |
| 120 | (A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8), |
| 121 | (A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9) |
| 122 | } |
| 123 | |
| 124 | #[cfg(test)] |
| 125 | mod tests { |
| 126 | use super::*; |
| 127 | |
| 128 | #[test] |
| 129 | fn reference_diffable() { |
| 130 | // Test that a reference to a diffable type can be diffed. |
| 131 | let before = &&&&&&"hello"; |
| 132 | let after = &&&&&&"world"; |
| 133 | |
| 134 | // This should automatically dereference the references. |
| 135 | let diff: Leaf<&str> = before.diff(after); |
| 136 | assert_eq!(diff.before, ******before); |
| 137 | assert_eq!(diff.after, ******after); |
| 138 | } |
| 139 | |
| 140 | #[test] |
| 141 | fn tuple_diffable() { |
| 142 | let before = (1usize, 2usize, 3usize); |
| 143 | let after = (4, 5, 6); |
| 144 | |
| 145 | let diff = before.diff(&after); |
| 146 | assert_eq!( |
| 147 | diff, |
| 148 | ( |
| 149 | Leaf { before: &1, after: &4 }, |
| 150 | Leaf { before: &2, after: &5 }, |
| 151 | Leaf { before: &3, after: &6 }, |
| 152 | ) |
| 153 | ); |
| 154 | } |
| 155 | } |