| 1 | //! Implementations for types from the `alloc` crate. |
| 2 | |
| 3 | use crate::{Diffable, Leaf}; |
| 4 | use alloc::{ |
| 5 | borrow::{Cow, ToOwned}, |
| 6 | boxed::Box, |
| 7 | collections::{BTreeMap, BTreeSet}, |
| 8 | rc::Rc, |
| 9 | string::String, |
| 10 | sync::Arc, |
| 11 | vec::Vec, |
| 12 | }; |
| 13 | |
| 14 | leaf_deref! { String => str } |
| 15 | |
| 16 | impl<T: Diffable + ?Sized> Diffable for Box<T> { |
| 17 | type Diff<'daft> |
| 18 | = <T as Diffable>::Diff<'daft> |
| 19 | where |
| 20 | T: 'daft; |
| 21 | |
| 22 | fn diff<'daft>(&'daft self, other: &'daft Self) -> Self::Diff<'daft> { |
| 23 | (**self).diff(other) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | impl<T: Diffable + ToOwned + ?Sized> Diffable for Cow<'_, T> { |
| 28 | type Diff<'daft> |
| 29 | = <T as Diffable>::Diff<'daft> |
| 30 | where |
| 31 | Self: 'daft; |
| 32 | |
| 33 | fn diff<'daft>(&'daft self, other: &'daft Self) -> Self::Diff<'daft> { |
| 34 | self.as_ref().diff(other.as_ref()) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | impl<T: Diffable + ?Sized> Diffable for Arc<T> { |
| 39 | type Diff<'daft> |
| 40 | = <T as Diffable>::Diff<'daft> |
| 41 | where |
| 42 | T: 'daft; |
| 43 | |
| 44 | fn diff<'daft>(&'daft self, other: &'daft Self) -> Self::Diff<'daft> { |
| 45 | (**self).diff(other) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | impl<T: Diffable + ?Sized> Diffable for Rc<T> { |
| 50 | type Diff<'daft> |
| 51 | = <T as Diffable>::Diff<'daft> |
| 52 | where |
| 53 | T: 'daft; |
| 54 | |
| 55 | fn diff<'daft>(&'daft self, other: &'daft Self) -> Self::Diff<'daft> { |
| 56 | (**self).diff(other) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | map_diff!( |
| 61 | /// A diff of two [`BTreeMap`] instances. |
| 62 | /// |
| 63 | /// The diff contains three elements: |
| 64 | /// |
| 65 | /// - `common`: Entries that are present in both maps, with their values |
| 66 | /// stored as a [`Leaf`]. |
| 67 | /// - `added`: Entries present in `after`, but not in `before`. |
| 68 | /// - `removed`: Entries present in `before`, but not in `after`. |
| 69 | /// |
| 70 | /// If `V` implements `Eq`, `common` can be split into |
| 71 | /// [`unchanged`][Self::unchanged] and [`modified`][Self::modified] entries. |
| 72 | /// Additionally, if `V` implements [`Diffable`], |
| 73 | /// [`modified_diff`][Self::modified_diff] can be used to recursively diff |
| 74 | /// modified entries. |
| 75 | /// |
| 76 | /// # Example |
| 77 | /// |
| 78 | /// ``` |
| 79 | /// # #[cfg(feature = "std")] { |
| 80 | /// use daft::{BTreeMapDiff, Diffable, Leaf}; |
| 81 | /// use std::collections::BTreeMap; |
| 82 | /// |
| 83 | /// let a: BTreeMap<usize, &str> = |
| 84 | /// [(0, "lorem"), (1, "ipsum"), (2, "dolor")].into_iter().collect(); |
| 85 | /// let b: BTreeMap<usize, &str> = |
| 86 | /// [(1, "ipsum"), (2, "sit"), (3, "amet")].into_iter().collect(); |
| 87 | /// |
| 88 | /// let changes = a.diff(&b); |
| 89 | /// let expected = BTreeMapDiff { |
| 90 | /// // Keys are stored by reference and matched by equality. |
| 91 | /// common: [ |
| 92 | /// (&1, Leaf { before: &"ipsum", after: &"ipsum" }), |
| 93 | /// (&2, Leaf { before: &"dolor", after: &"sit" }), |
| 94 | /// ].into_iter().collect(), |
| 95 | /// added: [(&3, &"amet")].into_iter().collect(), |
| 96 | /// removed: [(&0, &"lorem")].into_iter().collect(), |
| 97 | /// }; |
| 98 | /// |
| 99 | /// assert_eq!(changes, expected); |
| 100 | /// |
| 101 | /// // If the values are `Eq`, it's also possible to get lists of |
| 102 | /// // modified and unchanged entries. |
| 103 | /// assert!(changes.is_unchanged(&1)); |
| 104 | /// assert!(changes.is_modified(&2)); |
| 105 | /// let unchanged = changes.unchanged().collect::<Vec<_>>(); |
| 106 | /// let modified = changes.modified().collect::<Vec<_>>(); |
| 107 | /// |
| 108 | /// assert_eq!(unchanged, [(&1, &"ipsum")]); |
| 109 | /// assert_eq!(modified, [(&2, Leaf { before: &"dolor", after: &"sit" })]); |
| 110 | /// # } |
| 111 | /// ``` |
| 112 | BTreeMap, Ord |
| 113 | ); |
| 114 | set_diff!( |
| 115 | /// A diff of two [`BTreeSet`] instances. |
| 116 | /// |
| 117 | /// The diff contains three elements: |
| 118 | /// |
| 119 | /// - `common`: Entries that are present in both sets. |
| 120 | /// - `added`: Entries present in `after`, but not in `before`. |
| 121 | /// - `removed`: Entries present in `before`, but not in `after`. |
| 122 | /// |
| 123 | /// # Example |
| 124 | /// |
| 125 | /// ``` |
| 126 | /// # #[cfg(feature = "std")] { |
| 127 | /// use daft::{BTreeSetDiff, Diffable}; |
| 128 | /// use std::collections::BTreeSet; |
| 129 | /// |
| 130 | /// let a: BTreeSet<usize> = [0, 1].into_iter().collect(); |
| 131 | /// let b: BTreeSet<usize> = [1, 2].into_iter().collect(); |
| 132 | /// |
| 133 | /// let changes = a.diff(&b); |
| 134 | /// let expected = BTreeSetDiff { |
| 135 | /// // Entries are stored by reference and matched by equality. |
| 136 | /// common: [&1].into_iter().collect(), |
| 137 | /// added: [&2].into_iter().collect(), |
| 138 | /// removed: [&0].into_iter().collect(), |
| 139 | /// }; |
| 140 | /// |
| 141 | /// assert_eq!(changes, expected); |
| 142 | /// # } |
| 143 | /// ``` |
| 144 | BTreeSet, Ord |
| 145 | ); |
| 146 | |
| 147 | /// Treat Vecs as Leafs |
| 148 | // |
| 149 | // We plan to add opt in diff functionality: set-like, reordered, etc... |
| 150 | impl<T: Diffable> Diffable for Vec<T> { |
| 151 | type Diff<'daft> |
| 152 | = Leaf<&'daft [T]> |
| 153 | where |
| 154 | T: 'daft; |
| 155 | |
| 156 | fn diff<'daft>(&'daft self, other: &'daft Self) -> Self::Diff<'daft> { |
| 157 | Leaf { before: self, after: other } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | #[cfg(test)] |
| 162 | mod tests { |
| 163 | use super::*; |
| 164 | |
| 165 | #[test] |
| 166 | fn btree_set_diff() { |
| 167 | let a: BTreeSet<_> = [0, 1, 2, 3, 4, 5].into_iter().collect(); |
| 168 | let b: BTreeSet<_> = [3, 4, 5, 6, 7, 8].into_iter().collect(); |
| 169 | let changes = a.diff(&b); |
| 170 | let expected = BTreeSetDiff { |
| 171 | added: [&6, &7, &8].into_iter().collect(), |
| 172 | removed: [&0, &1, &2].into_iter().collect(), |
| 173 | common: [&3, &4, &5].into_iter().collect(), |
| 174 | }; |
| 175 | assert_eq!(expected, changes); |
| 176 | } |
| 177 | |
| 178 | #[test] |
| 179 | fn btree_map_diff() { |
| 180 | let a: BTreeMap<_, _> = [(0, 1), (1, 1), (2, 1)].into_iter().collect(); |
| 181 | let b: BTreeMap<_, _> = [(0, 2), (2, 1), (3, 1)].into_iter().collect(); |
| 182 | |
| 183 | let changes = a.diff(&b); |
| 184 | let expected = BTreeMapDiff { |
| 185 | common: [ |
| 186 | (&0, Leaf { before: &1, after: &2 }), |
| 187 | (&2, Leaf { before: &1, after: &1 }), |
| 188 | ] |
| 189 | .into_iter() |
| 190 | .collect(), |
| 191 | added: [(&3, &1)].into_iter().collect(), |
| 192 | removed: [(&1, &1)].into_iter().collect(), |
| 193 | }; |
| 194 | |
| 195 | assert_eq!(changes, expected); |
| 196 | |
| 197 | // Ensure that keys don't need to be Diffable, and values don't need to |
| 198 | // be Eq or Diffable. |
| 199 | #[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] |
| 200 | struct K(i32); |
| 201 | |
| 202 | #[derive(Debug)] |
| 203 | #[expect(dead_code)] |
| 204 | struct V(f64); |
| 205 | |
| 206 | let floats_a: BTreeMap<K, V> = |
| 207 | [(K(0), V(1.0)), (K(1), V(1.0)), (K(2), V(1.0))] |
| 208 | .into_iter() |
| 209 | .collect(); |
| 210 | let floats_b: BTreeMap<K, V> = |
| 211 | [(K(0), V(2.0)), (K(2), V(1.0)), (K(3), V(1.0))] |
| 212 | .into_iter() |
| 213 | .collect(); |
| 214 | |
| 215 | let diff = floats_a.diff(&floats_b); |
| 216 | assert_eq!(diff.added.keys().copied().collect::<Vec<_>>(), [&K(3)]); |
| 217 | assert_eq!(diff.removed.keys().copied().collect::<Vec<_>>(), [&K(1)]); |
| 218 | assert_eq!( |
| 219 | diff.common.keys().copied().collect::<Vec<_>>(), |
| 220 | [&K(0), &K(2)] |
| 221 | ); |
| 222 | } |
| 223 | } |