Skip to content

Commit

Permalink
feat: implement PartialEq for sync FrozenVec/FrozenMap/FrozenBTreeMap
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Sep 23, 2023
1 parent 0f1fd15 commit c5840c1
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use stable_deref_trait::StableDeref;
use std::alloc::Layout;
use std::borrow::Borrow;
use std::cmp::Eq;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::hash::Hash;
Expand Down Expand Up @@ -381,6 +382,14 @@ impl<K, V> std::convert::AsMut<HashMap<K, V>> for FrozenMap<K, V> {
}
}

impl<K: PartialEq + Eq + Hash, V: PartialEq> PartialEq for FrozenMap<K, V> {
fn eq(&self, other: &Self) -> bool {
let self_ref: &HashMap<K, V> = &self.map.read().unwrap();
let other_ref: &HashMap<K, V> = &other.map.read().unwrap();
self_ref == other_ref
}
}

/// Append-only threadsafe version of `std::vec::Vec` where
/// insertion does not require mutable access
pub struct FrozenVec<T> {
Expand Down Expand Up @@ -488,6 +497,14 @@ impl<T> Default for FrozenVec<T> {
}
}

impl PartialEq for FrozenVec<String> {
fn eq(&self, other: &Self) -> bool {
let self_ref: &Vec<String> = &self.vec.read().unwrap();
let other_ref: &Vec<String> = &other.vec.read().unwrap();
self_ref == other_ref
}
}

// The context for these functions is that we want to have a
// series of exponentially increasing buffer sizes. We want
// to maximize the total size of the buffers (since this
Expand Down Expand Up @@ -933,3 +950,11 @@ impl<K: Clone + Ord, V: StableDeref> Default for FrozenBTreeMap<K, V> {
Self::new()
}
}

impl<K: PartialEq, V: PartialEq> PartialEq for FrozenBTreeMap<K, V> {
fn eq(&self, other: &Self) -> bool {
let self_ref: &BTreeMap<K, V> = &self.0.read().unwrap();
let other_ref: &BTreeMap<K, V> = &other.0.read().unwrap();
self_ref == other_ref
}
}

0 comments on commit c5840c1

Please sign in to comment.