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>

source

pub fn new() -> Self

source

pub fn clone_last(&self) -> HashMap<K, V>

Clones last hashmap

source

pub fn is_same(&self, comparable: &HashMap<K, V>) -> bool

Checks if last hashmap is the same as new one

source

pub fn update(&self, new_map: &HashMap<K, V>)

Updates last hashmap to a new value

source

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));
source

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
  );
source

pub fn compare( &self, new_map: &HashMap<K, V>, ) -> Result<HashMap<K, V>, PoisonError<K>>

Compares new hashmap to the last one and returns changed values

Trait Implementations§

source§

impl<K: Clone + Clone + Eq + Hash, V: Clone + Clone + PartialEq> Clone for HashMapComparer<K, V>

source§

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)

Performs copy-assignment from source. Read more
source§

impl<K: Debug + Clone + Eq + Hash, V: Debug + Clone + PartialEq> Debug for HashMapComparer<K, V>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<K: Clone + Eq + Hash, V: Clone + PartialEq> Default for HashMapComparer<K, V>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto 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>
where K: Send, V: Send,

§

impl<K, V> Sync for HashMapComparer<K, V>
where K: Send, V: Send,

§

impl<K, V> Unpin for HashMapComparer<K, V>

§

impl<K, V> UnwindSafe for HashMapComparer<K, V>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.