Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement Clone for all structures except LockfreeFrozenVec #69

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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<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;
}
}
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<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;
}
}
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 @@ -495,3 +508,16 @@ 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;
}
}
24 changes: 23 additions & 1 deletion src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,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 @@ -505,7 +513,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 @@ -590,6 +598,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 @@ -1035,3 +1051,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())
}
}
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
Loading