Struct comparer::HashMapComparer
source · pub struct HashMapComparer<K: Clone + Eq + Hash, V: Clone + PartialEq> { /* private fields */ }Expand description
HashMapC
Implementations§
source§impl<K: Clone + Eq + Hash, V: Clone + PartialEq> HashMapComparer<K, V>
impl<K: Clone + Eq + Hash, V: Clone + PartialEq> HashMapComparer<K, V>
pub fn new() -> Self
sourcepub fn clone_last(&self) -> HashMap<K, V>
pub fn clone_last(&self) -> HashMap<K, V>
Clones last hashmap
sourcepub fn is_same(&self, comparable: &HashMap<K, V>) -> bool
pub fn is_same(&self, comparable: &HashMap<K, V>) -> bool
Checks if last hashmap is the same as new one
sourcepub fn is_same_update(&self, new_map: &HashMap<K, V>) -> bool
pub fn is_same_update(&self, new_map: &HashMap<K, V>) -> bool
Checks if last hashmap is the same as new one and updates it to be that new value
§Examples
use std::collections::HashMap;
use comparer::HashMapComparer;
let mut my_hashmap = HashMap::<u8, &str>::new();
let comparer = HashMapComparer::<u8, &str>::new();
my_hashmap.insert(1, "foo");
// HashMap has new values
assert_eq!(false, comparer.is_same_update(&my_hashmap));
// Hashmap has not recived new values
assert_eq!(true, comparer.is_same_update(&my_hashmap));sourcepub fn update_and_compare(
&self,
new_map: &HashMap<K, V>,
) -> Result<HashMap<K, V>, PoisonError<K>>
pub fn update_and_compare( &self, new_map: &HashMap<K, V>, ) -> Result<HashMap<K, V>, PoisonError<K>>
Updates last hashmap, compares new one to the last one and returns changed values.
If you want to compare hashmap without updating last hashmap use compare().
If you want to update hashmap without comparing it use update() and if you want to return bool instead of changed values use is_same() or is_same_update().
§Examples
use std::collections::HashMap;
use comparer::HashMapComparer;
let comparer = HashMapComparer::<u8, &str>::new();
let mut my_hashmap = HashMap::<u8, &str>::new();
my_hashmap.insert(1, "foo");
my_hashmap.insert(2, "bar");
my_hashmap.insert(4, "foo");
let mut results: Vec<HashMap<u8, &str>> = vec![];
for i in 0..5 {
my_hashmap.insert(i, "foo");
results.push(comparer.update_and_compare(&my_hashmap).unwrap());
}
assert_eq!(
vec![
// In a first comparison comparer always returns whole hashmap because all values in it is new
HashMap::<u8, &str>::from_iter(vec![(0, "foo"), (4, "foo"), (2, "bar"), (1, "foo")]),
// Returns empty hashmap because value 1: "foo" didn't change
HashMap::<u8, &str>::new(),
// Returns hashmap with 2: "foo" because it was changed from 2: "bar"
HashMap::<u8, &str>::from_iter(vec![(2, "foo")]),
// Returns hashmap with 3: "foo" because it's a new value
HashMap::<u8, &str>::from_iter(vec![(3, "foo")]),
//Returns empty hashmap because value 4: "foo" didn't change
HashMap::<u8, &str>::new(),
],
results
);Trait Implementations§
source§impl<K: Clone + Clone + Eq + Hash, V: Clone + Clone + PartialEq> Clone for HashMapComparer<K, V>
impl<K: Clone + Clone + Eq + Hash, V: Clone + Clone + PartialEq> Clone for HashMapComparer<K, V>
source§fn clone(&self) -> HashMapComparer<K, V>
fn clone(&self) -> HashMapComparer<K, V>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl<K, V> Freeze for HashMapComparer<K, V>
impl<K, V> RefUnwindSafe for HashMapComparer<K, V>
impl<K, V> Send for HashMapComparer<K, V>
impl<K, V> Sync for HashMapComparer<K, V>
impl<K, V> Unpin for HashMapComparer<K, V>
impl<K, V> UnwindSafe for HashMapComparer<K, V>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit)