pub struct HashMapDiff<'daft, K: Hash + Eq, V> {
pub common: HashMap<&'daft K, Leaf<&'daft V>>,
pub added: HashMap<&'daft K, &'daft V>,
pub removed: HashMap<&'daft K, &'daft V>,
}std only.Expand description
A diff of two HashMap instances.
The diff contains three elements:
common: Entries that are present in both maps, with their values stored as aLeaf.added: Entries present inafter, but not inbefore.removed: Entries present inbefore, but not inafter.
If V implements Eq, common can be split into
unchanged and modified entries.
Additionally, if V implements Diffable,
modified_diff can be used to recursively diff
modified entries.
§Example
use daft::{Diffable, HashMapDiff, Leaf};
use std::collections::HashMap;
let a: HashMap<usize, &str> =
[(0, "lorem"), (1, "ipsum"), (2, "dolor")].into_iter().collect();
let b: HashMap<usize, &str> =
[(1, "ipsum"), (2, "sit"), (3, "amet")].into_iter().collect();
let changes = a.diff(&b);
let expected = HashMapDiff {
// 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 mut unchanged = changes.unchanged().collect::<Vec<_>>();
unchanged.sort_by_key(|(k, _)| *k);
let mut modified = changes.modified().collect::<Vec<_>>();
modified.sort_by_key(|(k, _)| *k);
assert_eq!(unchanged, [(&1, &"ipsum")]);
assert_eq!(modified, [(&2, Leaf { before: &"dolor", after: &"sit" })]);Fields§
§common: HashMap<&'daft K, Leaf<&'daft V>>Entries common to both maps.
Values are stored as Leafs to references.
added: HashMap<&'daft K, &'daft V>Entries present in the after map, but not in before.
removed: HashMap<&'daft K, &'daft V>Entries present in the before map, but not in after.
Implementations§
Source§impl<'daft, K: Hash + Eq, V> HashMapDiff<'daft, K, V>
impl<'daft, K: Hash + Eq, V> HashMapDiff<'daft, K, V>
Source§impl<'daft, K: Hash + Eq, V: Eq> HashMapDiff<'daft, K, V>
impl<'daft, K: Hash + Eq, V: Eq> HashMapDiff<'daft, K, V>
Sourcepub fn unchanged(&self) -> impl Iterator<Item = (&'daft K, &'daft V)> + '_
pub fn unchanged(&self) -> impl Iterator<Item = (&'daft K, &'daft V)> + '_
Return an iterator over unchanged keys and values.
Sourcepub fn is_unchanged(&self, key: &K) -> bool
pub fn is_unchanged(&self, key: &K) -> bool
Return true if the value corresponding to the key is unchanged.
Sourcepub fn get_unchanged(&self, key: &K) -> Option<&'daft V>
pub fn get_unchanged(&self, key: &K) -> Option<&'daft V>
Return the value associated with the key if it is unchanged,
otherwise None.
Sourcepub fn unchanged_keys(&self) -> impl Iterator<Item = &'daft K> + '_
pub fn unchanged_keys(&self) -> impl Iterator<Item = &'daft K> + '_
Return an iterator over unchanged keys.
Sourcepub fn unchanged_values(&self) -> impl Iterator<Item = &'daft V> + '_
pub fn unchanged_values(&self) -> impl Iterator<Item = &'daft V> + '_
Return an iterator over unchanged values.
Sourcepub fn modified(&self) -> impl Iterator<Item = (&'daft K, Leaf<&'daft V>)> + '_
pub fn modified(&self) -> impl Iterator<Item = (&'daft K, Leaf<&'daft V>)> + '_
Return an iterator over modified keys and values.
Sourcepub fn is_modified(&self, key: &K) -> bool
pub fn is_modified(&self, key: &K) -> bool
Return true if the value corresponding to the key is modified.
Sourcepub fn get_modified(&self, key: &K) -> Option<Leaf<&'daft V>>
pub fn get_modified(&self, key: &K) -> Option<Leaf<&'daft V>>
Return the Leaf associated with the key if it is modified,
otherwise None.
Sourcepub fn modified_keys(&self) -> impl Iterator<Item = &'daft K> + '_
pub fn modified_keys(&self) -> impl Iterator<Item = &'daft K> + '_
Return an iterator over modified keys.
Sourcepub fn modified_values(&self) -> impl Iterator<Item = Leaf<&'daft V>> + '_
pub fn modified_values(&self) -> impl Iterator<Item = Leaf<&'daft V>> + '_
Return an iterator over modified values.
Trait Implementations§
Source§impl<'daft, K: PartialEq + Hash + Eq, V: PartialEq> PartialEq for HashMapDiff<'daft, K, V>
impl<'daft, K: PartialEq + Hash + Eq, V: PartialEq> PartialEq for HashMapDiff<'daft, K, V>
Source§fn eq(&self, other: &HashMapDiff<'daft, K, V>) -> bool
fn eq(&self, other: &HashMapDiff<'daft, K, V>) -> bool
self and other values to be equal, and is used by ==.