diff --git a/src/lib.rs b/src/lib.rs index 2a9c6206..9812987c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -112,7 +112,7 @@ where } } -impl<'a, K: 'a + Eq + Hash, V: 'a> DashMap { +impl DashMap { /// Creates a new DashMap with a capacity of 0. /// /// # Examples @@ -259,21 +259,15 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap { /// mappings.insert(8, 16); /// ``` pub fn with_capacity_and_hasher_and_shard_amount( - mut capacity: usize, + capacity: usize, hasher: S, shard_amount: usize, ) -> Self { assert!(shard_amount > 1); assert!(shard_amount.is_power_of_two()); - let shift = util::ptr_size_bits() - ncb(shard_amount); - - if capacity != 0 { - capacity = (capacity + (shard_amount - 1)) & !(shard_amount - 1); - } - - let cps = capacity / shard_amount; - + let shift = usize::BITS as usize - ncb(shard_amount); + let cps = (capacity + (shard_amount - 1)) / shard_amount; let shards = (0..shard_amount) .map(|_| CachePadded::new(RwLock::new(HashMap::with_capacity(cps)))) .collect(); @@ -1094,9 +1088,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> DashMap fn _shrink_to_fit(&self) { self.shards.iter().for_each(|s| { - let mut shard = s.write(); - let size = shard.len(); - shard.shrink_to(size, |(k, _v)| { + s.write().shrink_to_fit(|(k, _v)| { let mut hasher = self.hasher.build_hasher(); k.hash(&mut hasher); hasher.finish() @@ -1172,10 +1164,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> DashMap let idx = self.determine_shard(hash as usize); - let shard = match self.shards[idx].try_write() { - Some(shard) => shard, - None => return None, - }; + let shard = self.shards[idx].try_write()?; // SAFETY: The data will not outlive the guard, since we pass the guard to `Entry`. let (guard, shard) = unsafe { RwLockWriteGuardDetached::detach_from(shard) }; @@ -1235,10 +1224,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> DashMap let idx = self.determine_shard(hash as usize); - let shard = match self.shards[idx].try_write() { - Some(shard) => shard, - None => return None, - }; + let shard = self.shards[idx].try_write()?; // SAFETY: The data will not outlive the guard, since we pass the guard to `Entry`. let (guard, shard) = unsafe { RwLockWriteGuardDetached::detach_from(shard) }; @@ -1261,7 +1247,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> DashMap } fn _clear(&self) { - self._retain(|_, _| false) + self.shards.iter().for_each(|s| s.write().clear()); } fn _contains_key(&'a self, key: &Q) -> bool @@ -1344,20 +1330,16 @@ where } } -impl<'a, K: 'a + Eq + Hash, V: 'a + PartialEq, S: BuildHasher + Clone> PartialEq - for DashMap -{ +impl PartialEq for DashMap { fn eq(&self, other: &Self) -> bool { self.len() == other.len() - && self.iter().all(|r| { - other - .get(r.key()) - .map_or(false, |ro| r.value() == ro.value()) - }) + && self + .iter() + .all(|r| other.get(r.key()).is_some_and(|ro| r.value() == ro.value())) } } -impl<'a, K: 'a + Eq + Hash, V: 'a + Eq, S: BuildHasher + Clone> Eq for DashMap {} +impl Eq for DashMap {} impl IntoIterator for DashMap { type Item = (K, V); diff --git a/src/set.rs b/src/set.rs index 6165ed84..4ca919f8 100644 --- a/src/set.rs +++ b/src/set.rs @@ -50,7 +50,7 @@ where } } -impl<'a, K: 'a + Eq + Hash> DashSet { +impl DashSet { /// Creates a new DashSet with a capacity of 0. /// /// # Examples diff --git a/src/util.rs b/src/util.rs index 6464aa6d..3b0aee27 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,12 +1,8 @@ -use core::{mem, ptr}; +use core::ptr; use std::{marker::PhantomData, mem::ManuallyDrop}; use lock_api::{RawRwLock, RawRwLockDowngrade, RwLockReadGuard, RwLockWriteGuard}; -pub const fn ptr_size_bits() -> usize { - mem::size_of::() * 8 -} - pub fn map_in_place_2 T>((k, v): (U, &mut T), f: F) { unsafe { // # Safety