Skip to content

Commit 1047668

Browse files
committed
Add determine_map(), determine_shard(), and hasher() methods to ReadOnlyView
1 parent 626b98d commit 1047668

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

src/read_only.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,84 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> ReadOnlyView<K, V, S>
149149
}
150150
}
151151
}
152+
153+
cfg_if! {
154+
if #[cfg(feature = "raw-api")] {
155+
/// Finds which shard a certain key is stored in.
156+
/// You should probably not use this unless you know what you are doing.
157+
/// Note that shard selection is dependent on the default or provided HashBuilder.
158+
///
159+
/// Requires the `raw-api` feature to be enabled.
160+
///
161+
/// # Examples
162+
///
163+
/// ```
164+
/// use dashmap::DashMap;
165+
///
166+
/// let map = DashMap::new();
167+
/// map.insert("coca-cola", 1.4);
168+
/// let read_only_map = map.into_read_only();
169+
/// println!("coca-cola is stored in shard: {}", read_only_map.determine_map("coca-cola"));
170+
/// ```
171+
#[inline]
172+
pub fn determine_map<Q>(&self, key: &Q) -> usize
173+
where
174+
K: Borrow<Q>,
175+
Q: Hash + Eq + ?Sized,
176+
{
177+
self.map.determine_map(key)
178+
}
179+
}
180+
}
181+
182+
cfg_if! {
183+
if #[cfg(feature = "raw-api")] {
184+
/// Finds which shard a certain hash is stored in.
185+
///
186+
/// Requires the `raw-api` feature to be enabled.
187+
///
188+
/// # Examples
189+
///
190+
/// ```
191+
/// use dashmap::DashMap;
192+
///
193+
/// let map: DashMap<i32, i32> = DashMap::new();
194+
/// let key = "key";
195+
/// let hash = map.hash_usize(&key);
196+
/// let read_only_map = map.into_read_only();
197+
/// println!("hash is stored in shard: {}", read_only_map.determine_shard(hash));
198+
#[inline]
199+
pub fn determine_shard(&self, hash: usize) -> usize {
200+
self.map.determine_shard(hash)
201+
}
202+
} else {
203+
#[allow(dead_code)]
204+
#[inline]
205+
pub(crate) fn determine_shard(&self, hash: usize) -> usize {
206+
self.map.determine_shard(hash)
207+
}
208+
}
209+
}
210+
211+
/// Returns a reference to the underlying map's [`BuildHasher`].
212+
///
213+
/// # Examples
214+
///
215+
/// ```rust
216+
/// use dashmap::DashMap;
217+
/// use std::collections::hash_map::RandomState;
218+
///
219+
/// let hasher = RandomState::new();
220+
/// let map: DashMap<i32, i32> = DashMap::new();
221+
/// let read_only_map = map.into_read_only();
222+
/// let hasher: &RandomState = read_only_map.hasher();
223+
/// ```
224+
///
225+
/// [`BuildHasher`]: https://doc.rust-lang.org/std/hash/trait.BuildHasher.html
226+
#[inline]
227+
pub fn hasher(&self) -> &S {
228+
&self.map.hasher
229+
}
152230
}
153231

154232
#[cfg(test)]

0 commit comments

Comments
 (0)