From 37244076e30085c861cf662f7b87c0c77b390678 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Tue, 4 Jul 2023 01:39:56 -0700 Subject: [PATCH] feat: implement clone for all the containers --- src/index_map.rs | 9 +++++++++ src/index_set.rs | 9 +++++++++ src/map.rs | 18 ++++++++++++++++++ src/sync.rs | 36 ++++++++++++++++++++++++++++++++++++ src/vec.rs | 9 +++++++++ 5 files changed, 81 insertions(+) diff --git a/src/index_map.rs b/src/index_map.rs index fb70b7f..50d021f 100644 --- a/src/index_map.rs +++ b/src/index_map.rs @@ -235,3 +235,12 @@ impl Default for FrozenIndexMap { } } } + +impl Clone for FrozenIndexMap { + fn clone(&self) -> Self { + Self { + map: unsafe { self.map.get().as_ref().unwrap() }.clone().into(), + in_use: self.in_use.clone(), + } + } +} diff --git a/src/index_set.rs b/src/index_set.rs index 9e99632..28819d0 100644 --- a/src/index_set.rs +++ b/src/index_set.rs @@ -180,3 +180,12 @@ impl Default for FrozenIndexSet { Self::from(IndexSet::default()) } } + +impl Clone for FrozenIndexSet { + fn clone(&self) -> Self { + Self { + set: unsafe { self.set.get().as_ref().unwrap() }.clone().into(), + in_use: self.in_use.clone(), + } + } +} diff --git a/src/map.rs b/src/map.rs index 147a2ec..e8cf7c1 100644 --- a/src/map.rs +++ b/src/map.rs @@ -270,6 +270,15 @@ impl Default for FrozenMap { } } +impl Clone for FrozenMap { + 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 { @@ -495,3 +504,12 @@ impl Default for FrozenBTreeMap { } } } + +impl Clone for FrozenBTreeMap { + fn clone(&self) -> Self { + Self { + map: unsafe { self.map.get().as_ref().unwrap() }.clone().into(), + in_use: self.in_use.clone(), + } + } +} diff --git a/src/sync.rs b/src/sync.rs index a551b9a..c2e0a69 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -381,6 +381,14 @@ impl std::convert::AsMut> for FrozenMap { } } +impl Clone for FrozenMap { + 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 { @@ -488,6 +496,14 @@ impl Default for FrozenVec { } } +impl Clone for FrozenVec { + 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 @@ -713,6 +729,20 @@ fn test_non_lockfree_unchecked() { LockFreeFrozenVec::<()>::new(); } +impl Clone for LockFreeFrozenVec { + 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)] @@ -933,3 +963,9 @@ impl Default for FrozenBTreeMap { Self::new() } } + +impl Clone for FrozenBTreeMap { + fn clone(&self) -> Self { + Self(self.0.read().unwrap().clone().into()) + } +} diff --git a/src/vec.rs b/src/vec.rs index 33b6e6a..891a89c 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -216,6 +216,15 @@ impl Default for FrozenVec { } } +impl Clone for FrozenVec { + fn clone(&self) -> Self { + Self { + vec: unsafe { self.vec.get().as_ref().unwrap() }.clone().into(), + } + } +} + + impl From> for FrozenVec { fn from(vec: Vec) -> Self { Self {