From 70981b79e7858bc86709ffcfa64647b0fe472c9a Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Tue, 4 Jul 2023 01:39:56 -0700 Subject: [PATCH 1/2] feat: implement clone for the containers (except LockfreeFrozenVec) --- src/index_map.rs | 9 +++++++++ src/index_set.rs | 9 +++++++++ src/map.rs | 18 ++++++++++++++++++ src/sync.rs | 24 +++++++++++++++++++++++- src/vec.rs | 9 +++++++++ 5 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/index_map.rs b/src/index_map.rs index ba28139..d9683c6 100644 --- a/src/index_map.rs +++ b/src/index_map.rs @@ -288,3 +288,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 5614056..21098da 100644 --- a/src/index_set.rs +++ b/src/index_set.rs @@ -249,3 +249,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 2f3d435..ed1b540 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -414,6 +414,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 { @@ -505,7 +513,7 @@ impl FrozenVec { /// Returns an iterator over the vector. pub fn iter(&self) -> Iter<'_, T> { self.into_iter() - } + } } /// Iterator over FrozenVec, obtained via `.iter()` @@ -590,6 +598,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 @@ -1035,3 +1051,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 { From 0a330c0ee0c6078621a3612fc937233f8a1ef35b Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Wed, 9 Aug 2023 20:02:22 -0700 Subject: [PATCH 2/2] fix: set in_use to true while cloning --- src/index_map.rs | 10 +++++++--- src/index_set.rs | 10 +++++++--- src/map.rs | 20 ++++++++++++++------ src/vec.rs | 1 - 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/index_map.rs b/src/index_map.rs index d9683c6..e3a23d1 100644 --- a/src/index_map.rs +++ b/src/index_map.rs @@ -291,9 +291,13 @@ impl Default for FrozenIndexMap { impl Clone for FrozenIndexMap { fn clone(&self) -> 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: self.in_use.clone(), - } + in_use: Cell::from(false), + }; + self.in_use.set(false); + return self_clone; } } diff --git a/src/index_set.rs b/src/index_set.rs index 21098da..1ad5820 100644 --- a/src/index_set.rs +++ b/src/index_set.rs @@ -252,9 +252,13 @@ impl Default for FrozenIndexSet { impl Clone for FrozenIndexSet { fn clone(&self) -> 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: self.in_use.clone(), - } + in_use: Cell::from(false), + }; + self.in_use.set(false); + return self_clone; } } diff --git a/src/map.rs b/src/map.rs index e8cf7c1..1c94868 100644 --- a/src/map.rs +++ b/src/map.rs @@ -272,10 +272,14 @@ impl Default for FrozenMap { impl Clone for FrozenMap { fn clone(&self) -> 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: self.in_use.clone(), - } + in_use: Cell::from(false), + }; + self.in_use.set(false); + return self_clone; } } @@ -507,9 +511,13 @@ impl Default for FrozenBTreeMap { impl Clone for FrozenBTreeMap { fn clone(&self) -> 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: self.in_use.clone(), - } + in_use: Cell::from(false), + }; + self.in_use.set(false); + return self_clone; } } diff --git a/src/vec.rs b/src/vec.rs index 891a89c..592e443 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -224,7 +224,6 @@ impl Clone for FrozenVec { } } - impl From> for FrozenVec { fn from(vec: Vec) -> Self { Self {