Skip to content

Commit

Permalink
Merge pull request #68 from aminya/eq-except-lock-free-vec
Browse files Browse the repository at this point in the history
feat: implement PartialEq for all structures except LockfreeFrozenVec
  • Loading branch information
Manishearth authored Oct 23, 2023
2 parents 00da5ab + a9f1580 commit 3750bca
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/index_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,16 @@ impl<K: Eq + Hash, V, S: Default> Default for FrozenIndexMap<K, V, S> {
}
}
}

impl<T: Hash + Eq, S: PartialEq> PartialEq for FrozenIndexMap<T, S> {
fn eq(&self, other: &Self) -> bool {
assert!(!self.in_use.get());
assert!(!other.in_use.get());
self.in_use.set(true);
other.in_use.set(true);
let ret = unsafe { *self.map.get() == *other.map.get() };
self.in_use.set(false);
other.in_use.set(false);
ret
}
}
13 changes: 13 additions & 0 deletions src/index_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,16 @@ impl<T: Eq + Hash, S: Default> Default for FrozenIndexSet<T, S> {
Self::from(IndexSet::default())
}
}

impl<T: Hash + Eq, S: BuildHasher> PartialEq for FrozenIndexSet<T, S> {
fn eq(&self, other: &Self) -> bool {
assert!(!self.in_use.get());
assert!(!other.in_use.get());
self.in_use.set(true);
other.in_use.set(true);
let ret = unsafe { *self.set.get() == *other.set.get() };
self.in_use.set(false);
other.in_use.set(false);
ret
}
}
13 changes: 13 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,16 @@ impl<K: Clone + Ord, V: StableDeref> Default for FrozenBTreeMap<K, V> {
}
}
}

impl<K: Eq + Hash, V: PartialEq + StableDeref> PartialEq for FrozenMap<K, V> {
fn eq(&self, other: &Self) -> bool {
assert!(!self.in_use.get());
assert!(!other.in_use.get());
self.in_use.set(true);
other.in_use.set(true);
let ret = self.map.get() == other.map.get();
self.in_use.set(false);
other.in_use.set(false);
ret
}
}
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::fmt;
Expand Down Expand Up @@ -414,6 +415,14 @@ impl<K, V> std::convert::AsMut<HashMap<K, V>> for FrozenMap<K, V> {
}
}

impl<K: 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 @@ -590,6 +599,14 @@ impl<T> Default for FrozenVec<T> {
}
}

impl<T: PartialEq> PartialEq for FrozenVec<T> {
fn eq(&self, other: &Self) -> bool {
let self_ref: &Vec<T> = &self.vec.read().unwrap();
let other_ref: &Vec<T> = &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 @@ -1035,3 +1052,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
}
}
9 changes: 9 additions & 0 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,15 @@ impl<'a, T: StableDeref> IntoIterator for &'a FrozenVec<T> {
}
}

impl<T: StableDeref> PartialEq for FrozenVec<T>
where
T::Target: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.vec.get() == other.vec.get()
}
}

#[test]
fn test_iteration() {
let vec = vec!["a", "b", "c", "d"];
Expand Down

0 comments on commit 3750bca

Please sign in to comment.