Skip to content

Commit

Permalink
Add shards_mut() and into_shards() to DashMap and shards() to…
Browse files Browse the repository at this point in the history
… `ReadOnlyView` (#237)
  • Loading branch information
s-arash committed Jan 20, 2023
1 parent 83a2cf4 commit 8a0149d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,51 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap<K, V, S> {
pub fn shards(&self) -> &[RwLock<HashMap<K, V, S>>] {
&self.shards
}

/// Provides mutable access to the inner shards that store your data.
/// You should probably not use this unless you know what you are doing.
///
/// Requires the `raw-api` feature to be enabled.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
/// use dashmap::SharedValue;
///
/// let mut map = DashMap::<i32, &'static str>::new();
/// let shard_ind = map.determine_map(&42);
/// map.shards_mut()[shard_ind].get_mut().insert(42, SharedValue::new("forty two"));
/// assert_eq!(*map.get(&42).unwrap(), "forty two");
/// ```
pub fn shards_mut(&mut self) -> &mut [RwLock<HashMap<K, V, S>>] {
&mut self.shards
}

/// Consumes this `DashMap` and returns the inner shards.
/// You should probably not use this unless you know what you are doing.
///
/// Requires the `raw-api` feature to be enabled.
///
/// See [`DashMap::shards()`] and [`DashMap::shards_mut()`] for more information.
pub fn into_shards(self) -> Box<[RwLock<HashMap<K, V, S>>]> {
self.shards
}
} else {
#[allow(dead_code)]
pub(crate) fn shards(&self) -> &[RwLock<HashMap<K, V, S>>] {
&self.shards
}

#[allow(dead_code)]
pub(crate) fn shards_mut(&mut self) -> &mut [RwLock<HashMap<K, V, S>>] {
&mut self.shards
}

#[allow(dead_code)]
pub(crate) fn into_shards(self) -> Box<[RwLock<HashMap<K, V, S>>]> {
self.shards
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions src/read_only.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::lock::RwLock;
use crate::t::Map;
use crate::{DashMap, HashMap};
use cfg_if::cfg_if;
use core::borrow::Borrow;
use core::fmt;
use core::hash::{BuildHasher, Hash};
Expand Down Expand Up @@ -121,6 +123,32 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> ReadOnlyView<K, V, S>
.flat_map(|shard| shard.values())
.map(|v| v.get())
}

cfg_if! {
if #[cfg(feature = "raw-api")] {
/// Allows you to peek at the inner shards that store your data.
/// You should probably not use this unless you know what you are doing.
///
/// Requires the `raw-api` feature to be enabled.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let map = DashMap::<(), ()>::new().into_read_only();
/// println!("Amount of shards: {}", map.shards().len());
/// ```
pub fn shards(&self) -> &[RwLock<HashMap<K, V, S>>] {
&self.map.shards
}
} else {
#[allow(dead_code)]
pub(crate) fn shards(&self) -> &[RwLock<HashMap<K, V, S>>] {
&self.map.shards
}
}
}
}

#[cfg(test)]
Expand Down

0 comments on commit 8a0149d

Please sign in to comment.