Skip to content

Commit

Permalink
feat: implement clone for all the containers
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Sep 23, 2023
1 parent cef5f40 commit 3724407
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/index_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,12 @@ 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 {
Self {
map: unsafe { self.map.get().as_ref().unwrap() }.clone().into(),
in_use: self.in_use.clone(),
}
}
}
9 changes: 9 additions & 0 deletions src/index_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,12 @@ impl<T: Eq + Hash, S: Default> Default for FrozenIndexSet<T, S> {
Self::from(IndexSet::default())
}
}

impl<K: Clone, V: Clone> Clone for FrozenIndexSet<K, V> {
fn clone(&self) -> Self {
Self {
set: unsafe { self.set.get().as_ref().unwrap() }.clone().into(),
in_use: self.in_use.clone(),
}
}
}
18 changes: 18 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,15 @@ 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 {
Self {
map: unsafe { self.map.get().as_ref().unwrap() }.clone().into(),
in_use: self.in_use.clone(),
}
}
}

/// Append-only version of `std::collections::BTreeMap` where
/// insertion does not require mutable access
pub struct FrozenBTreeMap<K, V> {
Expand Down Expand Up @@ -495,3 +504,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 {
map: unsafe { self.map.get().as_ref().unwrap() }.clone().into(),
in_use: self.in_use.clone(),
}
}
}
36 changes: 36 additions & 0 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,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(),
}
}
}

/// 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 +496,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(),
}
}
}

// 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 @@ -713,6 +729,20 @@ fn test_non_lockfree_unchecked() {
LockFreeFrozenVec::<()>::new();
}

impl<T: Copy + Clone> Clone for LockFreeFrozenVec<T> {
fn clone(&self) -> Self {
let cap = self.cap.load(Ordering::Acquire);
let len = self.len.load(Ordering::Acquire);

let new_vec = Self::with_capacity(cap);
for i in 0..len {
new_vec.push(self.get(i).unwrap());
}

new_vec
}
}

#[test]
fn test_non_lockfree() {
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -933,3 +963,9 @@ impl<K: Clone + Ord, V: StableDeref> Default for FrozenBTreeMap<K, V> {
Self::new()
}
}

impl<K: Clone, V: Clone> Clone for FrozenBTreeMap<K, V> {
fn clone(&self) -> Self {
Self(self.0.read().unwrap().clone().into())
}
}
9 changes: 9 additions & 0 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ 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 3724407

Please sign in to comment.