Skip to content

Commit

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

impl<K: Clone, V: Clone, S: Clone> Clone for FrozenIndexMap<K, V, S> {
fn clone(&self) -> Self {
assert!(!self.in_use.get());
self.in_use.set(true);
let self_clone = Self {
map: unsafe { self.map.get().as_ref().unwrap() }.clone().into(),
in_use: Cell::from(false),
};
self.in_use.set(false);
return self_clone;
}
}

impl<T: Hash + Eq, S: PartialEq> PartialEq for FrozenIndexMap<T, S> {
fn eq(&self, other: &Self) -> bool {
assert!(!self.in_use.get());
Expand Down
13 changes: 13 additions & 0 deletions src/index_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,19 @@ impl<T: Eq + Hash, S: Default> Default for FrozenIndexSet<T, S> {
}
}

impl<K: Clone, V: Clone> Clone for FrozenIndexSet<K, V> {
fn clone(&self) -> Self {
assert!(!self.in_use.get());
self.in_use.set(true);
let self_clone = Self {
set: unsafe { self.set.get().as_ref().unwrap() }.clone().into(),
in_use: Cell::from(false),
};
self.in_use.set(false);
return self_clone;
}
}

impl<T: Hash + Eq, S: BuildHasher> PartialEq for FrozenIndexSet<T, S> {
fn eq(&self, other: &Self) -> bool {
assert!(!self.in_use.get());
Expand Down
26 changes: 26 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,19 @@ impl<K: Eq + Hash, V, S: Default> Default for FrozenMap<K, V, S> {
}
}

impl<K: Clone, V: Clone, S: Clone> Clone for FrozenMap<K, V, S> {
fn clone(&self) -> Self {
assert!(!self.in_use.get());
self.in_use.set(true);
let self_clone = Self {
map: unsafe { self.map.get().as_ref().unwrap() }.clone().into(),
in_use: Cell::from(false),
};
self.in_use.set(false);
return self_clone;
}
}

/// Append-only version of `std::collections::BTreeMap` where
/// insertion does not require mutable access
pub struct FrozenBTreeMap<K, V> {
Expand Down Expand Up @@ -496,6 +509,19 @@ impl<K: Clone + Ord, V: StableDeref> Default for FrozenBTreeMap<K, V> {
}
}

impl<K: Clone, V: Clone> Clone for FrozenBTreeMap<K, V> {
fn clone(&self) -> Self {
assert!(!self.in_use.get());
self.in_use.set(true);
let self_clone = Self {
map: unsafe { self.map.get().as_ref().unwrap() }.clone().into(),
in_use: Cell::from(false),
};
self.in_use.set(false);
return self_clone;
}
}

impl<K: Eq + Hash, V: PartialEq + StableDeref> PartialEq for FrozenMap<K, V> {
fn eq(&self, other: &Self) -> bool {
assert!(!self.in_use.get());
Expand Down
24 changes: 23 additions & 1 deletion src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,14 @@ impl<K, V> std::convert::AsMut<HashMap<K, V>> for FrozenMap<K, V> {
}
}

impl<K: Clone, V: Clone> Clone for FrozenMap<K, V> {
fn clone(&self) -> Self {
Self {
map: self.map.read().unwrap().clone().into(),
}
}
}

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();
Expand Down Expand Up @@ -514,7 +522,7 @@ impl<T: StableDeref> FrozenVec<T> {
/// Returns an iterator over the vector.
pub fn iter(&self) -> Iter<'_, T> {
self.into_iter()
}
}
}

/// Iterator over FrozenVec, obtained via `.iter()`
Expand Down Expand Up @@ -599,6 +607,14 @@ impl<T> Default for FrozenVec<T> {
}
}

impl<T: Clone> Clone for FrozenVec<T> {
fn clone(&self) -> Self {
Self {
vec: self.vec.read().unwrap().clone().into(),
}
}
}

impl<T: PartialEq> PartialEq for FrozenVec<T> {
fn eq(&self, other: &Self) -> bool {
let self_ref: &Vec<T> = &self.vec.read().unwrap();
Expand Down Expand Up @@ -1053,6 +1069,12 @@ impl<K: Clone + Ord, V: StableDeref> Default for FrozenBTreeMap<K, V> {
}
}

impl<K: Clone, V: Clone> Clone for FrozenBTreeMap<K, V> {
fn clone(&self) -> Self {
Self(self.0.read().unwrap().clone().into())
}
}

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();
Expand Down
8 changes: 8 additions & 0 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ impl<T> Default for FrozenVec<T> {
}
}

impl<T: Clone> Clone for FrozenVec<T> {
fn clone(&self) -> Self {
Self {
vec: unsafe { self.vec.get().as_ref().unwrap() }.clone().into(),
}
}
}

impl<T> From<Vec<T>> for FrozenVec<T> {
fn from(vec: Vec<T>) -> Self {
Self {
Expand Down

0 comments on commit e02c1b2

Please sign in to comment.