| 1 | //! Implementations for types in std. |
| 2 | |
| 3 | use craterust-analyzer cargo daft 0.1.5 crate/::Diffablerust-analyzer cargo daft 0.1.5 diffable/Diffable#; |
| 4 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{ |
| 5 | collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::{HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#, HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#}, |
| 6 | ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::{OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#, OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#}, |
| 7 | hashrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/::Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash#, |
| 8 | pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::{Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#}, |
| 9 | }; |
| 10 | |
| 11 | leafrust-analyzer cargo daft 0.1.5 macros/leaf!! { Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr# } |
| 12 | leaf_derefrust-analyzer cargo daft 0.1.5 macros/leaf_deref!! { PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf# => Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString# => OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr# } |
| 13 | |
| 14 | map_diffrust-analyzer cargo daft 0.1.5 macros/map_diff!!( |
| 15 | /// A diff of two [`HashMap`] instances. |
| 16 | /// |
| 17 | /// The diff contains three elements: |
| 18 | /// |
| 19 | /// - `common`: Entries that are present in both maps, with their values |
| 20 | /// stored as a [`Leaf`][crate::Leaf]. |
| 21 | /// - `added`: Entries present in `after`, but not in `before`. |
| 22 | /// - `removed`: Entries present in `before`, but not in `after`. |
| 23 | /// |
| 24 | /// If `V` implements `Eq`, `common` can be split into |
| 25 | /// [`unchanged`][Self::unchanged] and [`modified`][Self::modified] entries. |
| 26 | /// Additionally, if `V` implements [`Diffable`], |
| 27 | /// [`modified_diff`][Self::modified_diff] can be used to recursively diff |
| 28 | /// modified entries. |
| 29 | /// |
| 30 | /// # Example |
| 31 | /// |
| 32 | /// ``` |
| 33 | /// # #[cfg(feature = "std")] { |
| 34 | /// use daft::{Diffable, HashMapDiff, Leaf}; |
| 35 | /// use std::collections::HashMap; |
| 36 | /// |
| 37 | /// let a: HashMap<usize, &str> = |
| 38 | /// [(0, "lorem"), (1, "ipsum"), (2, "dolor")].into_iter().collect(); |
| 39 | /// let b: HashMap<usize, &str> = |
| 40 | /// [(1, "ipsum"), (2, "sit"), (3, "amet")].into_iter().collect(); |
| 41 | /// |
| 42 | /// let changes = a.diff(&b); |
| 43 | /// let expected = HashMapDiff { |
| 44 | /// // Keys are stored by reference and matched by equality. |
| 45 | /// common: [ |
| 46 | /// (&1, Leaf { before: &"ipsum", after: &"ipsum" }), |
| 47 | /// (&2, Leaf { before: &"dolor", after: &"sit" }), |
| 48 | /// ].into_iter().collect(), |
| 49 | /// added: [(&3, &"amet")].into_iter().collect(), |
| 50 | /// removed: [(&0, &"lorem")].into_iter().collect(), |
| 51 | /// }; |
| 52 | /// |
| 53 | /// assert_eq!(changes, expected); |
| 54 | /// |
| 55 | /// // If the values are `Eq`, it's also possible to get lists of |
| 56 | /// // modified and unchanged entries. |
| 57 | /// assert!(changes.is_unchanged(&1)); |
| 58 | /// assert!(changes.is_modified(&2)); |
| 59 | /// let mut unchanged = changes.unchanged().collect::<Vec<_>>(); |
| 60 | /// unchanged.sort_by_key(|(k, _)| *k); |
| 61 | /// let mut modified = changes.modified().collect::<Vec<_>>(); |
| 62 | /// modified.sort_by_key(|(k, _)| *k); |
| 63 | /// |
| 64 | /// assert_eq!(unchanged, [(&1, &"ipsum")]); |
| 65 | /// assert_eq!(modified, [(&2, Leaf { before: &"dolor", after: &"sit" })]); |
| 66 | /// # } |
| 67 | /// ``` |
| 68 | HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash# |
| 69 | ); |
| 70 | set_diffrust-analyzer cargo daft 0.1.5 macros/set_diff!!( |
| 71 | /// A diff of two [`HashSet`] instances. |
| 72 | /// |
| 73 | /// The diff contains three elements: |
| 74 | /// |
| 75 | /// - `common`: Entries that are present in both sets. |
| 76 | /// - `added`: Entries present in `after`, but not in `before`. |
| 77 | /// - `removed`: Entries present in `before`, but not in `after`. |
| 78 | /// |
| 79 | /// # Example |
| 80 | /// |
| 81 | /// ``` |
| 82 | /// # #[cfg(feature = "std")] { |
| 83 | /// use daft::{Diffable, HashSetDiff}; |
| 84 | /// use std::collections::HashSet; |
| 85 | /// |
| 86 | /// let a: HashSet<usize> = [0, 1].into_iter().collect(); |
| 87 | /// let b: HashSet<usize> = [1, 2].into_iter().collect(); |
| 88 | /// |
| 89 | /// let changes = a.diff(&b); |
| 90 | /// let expected = HashSetDiff { |
| 91 | /// // Entries are stored by reference and matched by equality. |
| 92 | /// common: [&1].into_iter().collect(), |
| 93 | /// added: [&2].into_iter().collect(), |
| 94 | /// removed: [&0].into_iter().collect(), |
| 95 | /// }; |
| 96 | /// |
| 97 | /// assert_eq!(changes, expected); |
| 98 | /// # } |
| 99 | /// ``` |
| 100 | HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash# |
| 101 | ); |
| 102 | |
| 103 | #[cfg(test)] |
| 104 | mod testsrust-analyzer cargo daft 0.1.5 std_impls/tests/ { |
| 105 | use superrust-analyzer cargo daft 0.1.5 std_impls/::*; |
| 106 | use craterust-analyzer cargo daft 0.1.5 crate/::Leafrust-analyzer cargo daft 0.1.5 leaf/Leaf#; |
| 107 | |
| 108 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 109 | fn hash_set_diffrust-analyzer cargo daft 0.1.5 std_impls/tests/hash_set_diff().() { |
| 110 | let a: HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#<_> = [0, 1, 2, 3, 4, 5].into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 111 | let b: HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#<_> = [3, 4, 5, 6, 7, 8].into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 112 | let changes = a.diffrust-analyzer cargo daft 0.1.5 std_impls/impl#[`HashSet<K>`][Diffable]diff().(&b); |
| 113 | |
| 114 | let expected = HashSetDiffrust-analyzer cargo daft 0.1.5 std_impls/HashSetDiff# { |
| 115 | addedrust-analyzer cargo daft 0.1.5 std_impls/HashSetDiff#added.: [&6, &7, &8].into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(), |
| 116 | removedrust-analyzer cargo daft 0.1.5 std_impls/HashSetDiff#removed.: [&0, &1, &2].into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(), |
| 117 | commonrust-analyzer cargo daft 0.1.5 std_impls/HashSetDiff#common.: [&3, &4, &5].into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(), |
| 118 | }; |
| 119 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected, changes); |
| 120 | } |
| 121 | |
| 122 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 123 | fn hash_map_diffrust-analyzer cargo daft 0.1.5 std_impls/tests/hash_map_diff().() { |
| 124 | let a: HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<_, _> = [(0, 1), (1, 1), (2, 1)].into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 125 | let b: HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<_, _> = [(0, 2), (2, 1), (3, 1)].into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 126 | |
| 127 | let changes = a.diffrust-analyzer cargo daft 0.1.5 std_impls/impl#[`HashMap<K, V>`][Diffable]diff().(&b); |
| 128 | let expected = HashMapDiffrust-analyzer cargo daft 0.1.5 std_impls/HashMapDiff# { |
| 129 | commonrust-analyzer cargo daft 0.1.5 std_impls/HashMapDiff#common.: [ |
| 130 | (&0, Leafrust-analyzer cargo daft 0.1.5 leaf/Leaf# { beforerust-analyzer cargo daft 0.1.5 leaf/Leaf#before.: &1, afterrust-analyzer cargo daft 0.1.5 leaf/Leaf#after.: &2 }), |
| 131 | (&2, Leafrust-analyzer cargo daft 0.1.5 leaf/Leaf# { beforerust-analyzer cargo daft 0.1.5 leaf/Leaf#before.: &1, afterrust-analyzer cargo daft 0.1.5 leaf/Leaf#after.: &1 }), |
| 132 | ] |
| 133 | .into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().() |
| 134 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(), |
| 135 | addedrust-analyzer cargo daft 0.1.5 std_impls/HashMapDiff#added.: [(&3, &1)].into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(), |
| 136 | removedrust-analyzer cargo daft 0.1.5 std_impls/HashMapDiff#removed.: [(&1, &1)].into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(), |
| 137 | }; |
| 138 | |
| 139 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(changes, expected); |
| 140 | |
| 141 | // Ensure that keys don't need to be Diffable, and values don't need to |
| 142 | // be Eq or Diffable. |
| 143 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)] |
| 144 | struct Krust-analyzer cargo daft 0.1.5 std_impls/tests/K#(i32); |
| 145 | |
| 146 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 147 | #[expect(dead_code)] |
| 148 | struct Vrust-analyzer cargo daft 0.1.5 std_impls/tests/V#(f64); |
| 149 | |
| 150 | let floats_a: HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<Krust-analyzer cargo daft 0.1.5 std_impls/tests/K#, Vrust-analyzer cargo daft 0.1.5 std_impls/tests/V#> = |
| 151 | [(Krust-analyzer cargo daft 0.1.5 std_impls/tests/K#(0), Vrust-analyzer cargo daft 0.1.5 std_impls/tests/V#(1.0)), (Krust-analyzer cargo daft 0.1.5 std_impls/tests/K#(1), Vrust-analyzer cargo daft 0.1.5 std_impls/tests/V#(1.0)), (Krust-analyzer cargo daft 0.1.5 std_impls/tests/K#(2), Vrust-analyzer cargo daft 0.1.5 std_impls/tests/V#(1.0))] |
| 152 | .into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().() |
| 153 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 154 | let floats_b: HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<Krust-analyzer cargo daft 0.1.5 std_impls/tests/K#, Vrust-analyzer cargo daft 0.1.5 std_impls/tests/V#> = |
| 155 | [(Krust-analyzer cargo daft 0.1.5 std_impls/tests/K#(0), Vrust-analyzer cargo daft 0.1.5 std_impls/tests/V#(2.0)), (Krust-analyzer cargo daft 0.1.5 std_impls/tests/K#(2), Vrust-analyzer cargo daft 0.1.5 std_impls/tests/V#(1.0)), (Krust-analyzer cargo daft 0.1.5 std_impls/tests/K#(3), Vrust-analyzer cargo daft 0.1.5 std_impls/tests/V#(1.0))] |
| 156 | .into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().() |
| 157 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 158 | |
| 159 | let diff = floats_a.diffrust-analyzer cargo daft 0.1.5 std_impls/impl#[`HashMap<K, V>`][Diffable]diff().(&floats_b); |
| 160 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(diff.addedrust-analyzer cargo daft 0.1.5 std_impls/HashMapDiff#added..keysrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]keys().().copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#copied().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>(), [&Krust-analyzer cargo daft 0.1.5 std_impls/tests/K#(3)]); |
| 161 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(diff.removedrust-analyzer cargo daft 0.1.5 std_impls/HashMapDiff#removed..keysrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]keys().().copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#copied().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>(), [&Krust-analyzer cargo daft 0.1.5 std_impls/tests/K#(1)]); |
| 162 | // HashMaps have non-deterministic order, so they should be sorted |
| 163 | // before comparison. |
| 164 | let mut common = diff.commonrust-analyzer cargo daft 0.1.5 std_impls/HashMapDiff#common..keysrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]keys().().copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#copied().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>(); |
| 165 | common.sortrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort().(); |
| 166 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(common, [&Krust-analyzer cargo daft 0.1.5 std_impls/tests/K#(0), &Krust-analyzer cargo daft 0.1.5 std_impls/tests/K#(2)]); |
| 167 | } |
| 168 | } |