diff --git a/CHANGELOG.md b/CHANGELOG.md index c84abe7..130f78e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ ## Unreleased - ReleaseDate +### Fixed + +- `IndexMapDiff` and `IndexSetDiff` are now re-exported at the crate root, matching the other map and set diff types, and have rustdoc examples. + ## [0.1.6] - 2026-05-14 ### Added diff --git a/daft/src/lib.rs b/daft/src/lib.rs index 304f7f3..b17c4d4 100644 --- a/daft/src/lib.rs +++ b/daft/src/lib.rs @@ -524,3 +524,5 @@ pub use diffable::*; pub use leaf::*; #[cfg(feature = "std")] pub use std_impls::*; +#[cfg(feature = "indexmap")] +pub use third_party::indexmap::*; diff --git a/daft/src/third_party/indexmap.rs b/daft/src/third_party/indexmap.rs index 3955f0a..a099e06 100644 --- a/daft/src/third_party/indexmap.rs +++ b/daft/src/third_party/indexmap.rs @@ -2,8 +2,96 @@ use crate::Diffable; use core::hash::Hash; use indexmap::{IndexMap, IndexSet}; -map_diff!(IndexMap, Hash); -set_diff!(IndexSet, Hash); +map_diff!( + /// A diff of two [`IndexMap`] instances. + /// + /// The diff contains three elements: + /// + /// - `common`: Entries that are present in both maps, with their values + /// stored as a [`Leaf`][crate::Leaf]. + /// - `added`: Entries present in `after`, but not in `before`. + /// - `removed`: Entries present in `before`, but not in `after`. + /// + /// If `V` implements `Eq`, `common` can be split into + /// [`unchanged`][Self::unchanged] and [`modified`][Self::modified] entries. + /// Additionally, if `V` implements [`Diffable`], + /// [`modified_diff`][Self::modified_diff] can be used to recursively diff + /// modified entries. + /// + /// [`IndexMap`]: indexmap::IndexMap + /// + /// # Example + /// + /// ``` + /// # #[cfg(feature = "indexmap")] { + /// use daft::{Diffable, IndexMapDiff, Leaf}; + /// use indexmap::IndexMap; + /// + /// let a: IndexMap = + /// [(0, "lorem"), (1, "ipsum"), (2, "dolor")].into_iter().collect(); + /// let b: IndexMap = + /// [(1, "ipsum"), (2, "sit"), (3, "amet")].into_iter().collect(); + /// + /// let changes = a.diff(&b); + /// let expected = IndexMapDiff { + /// // Keys are stored by reference and matched by equality. + /// common: [ + /// (&1, Leaf { before: &"ipsum", after: &"ipsum" }), + /// (&2, Leaf { before: &"dolor", after: &"sit" }), + /// ].into_iter().collect(), + /// added: [(&3, &"amet")].into_iter().collect(), + /// removed: [(&0, &"lorem")].into_iter().collect(), + /// }; + /// + /// assert_eq!(changes, expected); + /// + /// // If the values are `Eq`, it's also possible to get lists of + /// // modified and unchanged entries. + /// assert!(changes.is_unchanged(&1)); + /// assert!(changes.is_modified(&2)); + /// let unchanged = changes.unchanged().collect::>(); + /// let modified = changes.modified().collect::>(); + /// + /// assert_eq!(unchanged, [(&1, &"ipsum")]); + /// assert_eq!(modified, [(&2, Leaf { before: &"dolor", after: &"sit" })]); + /// # } + /// ``` + IndexMap, Hash +); +set_diff!( + /// A diff of two [`IndexSet`] instances. + /// + /// The diff contains three elements: + /// + /// - `common`: Entries that are present in both sets. + /// - `added`: Entries present in `after`, but not in `before`. + /// - `removed`: Entries present in `before`, but not in `after`. + /// + /// [`IndexSet`]: indexmap::IndexSet + /// + /// # Example + /// + /// ``` + /// # #[cfg(feature = "indexmap")] { + /// use daft::{Diffable, IndexSetDiff}; + /// use indexmap::IndexSet; + /// + /// let a: IndexSet = [0, 1].into_iter().collect(); + /// let b: IndexSet = [1, 2].into_iter().collect(); + /// + /// let changes = a.diff(&b); + /// let expected = IndexSetDiff { + /// // Entries are stored by reference and matched by equality. + /// common: [&1].into_iter().collect(), + /// added: [&2].into_iter().collect(), + /// removed: [&0].into_iter().collect(), + /// }; + /// + /// assert_eq!(changes, expected); + /// # } + /// ``` + IndexSet, Hash +); #[cfg(test)] mod tests { diff --git a/daft/src/third_party/mod.rs b/daft/src/third_party/mod.rs index a109a1d..5603520 100644 --- a/daft/src/third_party/mod.rs +++ b/daft/src/third_party/mod.rs @@ -1,7 +1,7 @@ //! Implementations for third-party libraries. #[cfg(feature = "indexmap")] -mod indexmap; +pub(crate) mod indexmap; #[cfg(feature = "newtype-uuid1")] mod newtype_uuid_impls; #[cfg(feature = "oxnet01")]