Skip to content

Commit

Permalink
Add clone impl.
Browse files Browse the repository at this point in the history
  • Loading branch information
xacrimon committed Jan 17, 2020
1 parent b7d79cb commit 9cfbcf8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ pub struct DashMap<K, V, S = FxBuildHasher> {
hasher: S,
}

impl<K: Eq + Hash + Clone, V: Clone, S: Clone> Clone for DashMap<K, V, S> {
fn clone(&self) -> Self {
let mut inner_shards = Vec::new();
for shard in self.shards.iter() {
let shard = shard.read();
inner_shards.push(RwLock::new((*shard).clone()));
}
Self {
ncb: self.ncb,
shards: inner_shards.into_boxed_slice(),
hasher: self.hasher.clone(),
}
}
}

impl<K, V> Default for DashMap<K, V>
where
K: Eq + Hash,
Expand Down
15 changes: 12 additions & 3 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,28 @@ pub unsafe fn change_lifetime_mut<'a, 'b, T>(x: &'a mut T) -> &'b mut T {
}

/// A simple wrapper around `T`
///
///
/// This is to prevent UB when using `HashMap::get_key_value`, because
/// `HashMap` doesn't expose an api to get the key and value, where
/// the value is a `&mut T`.
///
///
/// See [#10](https://github.com/xacrimon/dashmap/issues/10) for details
///
///
/// This type is meant to be an implementation detail, but must be exposed due to the `Dashmap::shards`
#[repr(transparent)]
pub struct SharedValue<T> {
value: UnsafeCell<T>,
}

impl<T: Clone> Clone for SharedValue<T> {
fn clone(&self) -> Self {
let inner = self.get().clone();
Self {
value: UnsafeCell::new(inner),
}
}
}

unsafe impl<T: Send> Send for SharedValue<T> {}
unsafe impl<T: Sync> Sync for SharedValue<T> {}

Expand Down

0 comments on commit 9cfbcf8

Please sign in to comment.