From 363f3f182e2e18335eb7ec66d98e6dc64675b8fd Mon Sep 17 00:00:00 2001 From: xacrimon Date: Sun, 6 Feb 2022 16:14:01 +0100 Subject: [PATCH 1/3] optional send guard --- Cargo.toml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1af69cac..38b8b827 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,12 +13,13 @@ keywords = ["atomic", "concurrent", "hashmap"] categories = ["concurrency", "algorithms", "data-structures"] [features] -default = [] +default = ["send_guard"] raw-api = [] +send_guard = ["parking_lot/send_guard"] [dependencies] num_cpus = "1.13.0" -parking_lot = { version = "0.11.2", features = ["send_guard"] } +parking_lot = "0.11.2" serde = { version = "1.0.131", optional = true, features = ["derive"] } cfg-if = "1.0.0" rayon = { version = "1.5.1", optional = true } From 7197fa332375237bd175e51c097cc218ef1b8048 Mon Sep 17 00:00:00 2001 From: xacrimon Date: Sun, 6 Feb 2022 16:15:08 +0100 Subject: [PATCH 2/3] doc --- Cargo.toml | 2 +- README.md | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 38b8b827..6496f0e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,4 +25,4 @@ cfg-if = "1.0.0" rayon = { version = "1.5.1", optional = true } [package.metadata.docs.rs] -features = ["rayon", "raw-api", "serde"] +features = ["rayon", "raw-api", "serde", "send_guard"] diff --git a/README.md b/README.md index 1e518679..577df0df 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ If you have any suggestions or tips do not hesitate to open an issue or a PR. - `rayon` - Enables rayon support. +- `send_guard` - Enables the `send_guard` feature of `parking_lot`, making `Ref*` guards `Send`. This is on by default. + ## Contributing DashMap gladly accepts contributions! From e530e80da0cb19991db580e96a8ca8ffc250e479 Mon Sep 17 00:00:00 2001 From: xacrimon Date: Sun, 6 Feb 2022 16:17:38 +0100 Subject: [PATCH 3/3] lint + fmt --- src/lib.rs | 18 ++++++++++++++---- src/t.rs | 10 ++++++++-- src/try_result.rs | 19 +++---------------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f1c1848e..9b1acdcf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -732,13 +732,19 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> Map<'a, K, V, S> self.shards.get_unchecked(i).write() } - unsafe fn _try_yield_read_shard(&'a self, i: usize) -> Option>> { + unsafe fn _try_yield_read_shard( + &'a self, + i: usize, + ) -> Option>> { debug_assert!(i < self.shards.len()); self.shards.get_unchecked(i).try_read() } - unsafe fn _try_yield_write_shard(&'a self, i: usize) -> Option>> { + unsafe fn _try_yield_write_shard( + &'a self, + i: usize, + ) -> Option>> { debug_assert!(i < self.shards.len()); self.shards.get_unchecked(i).try_write() @@ -906,7 +912,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> Map<'a, K, V, S> fn _try_get_mut(&'a self, key: &Q) -> TryResult> where K: Borrow, - Q: Hash + Eq + ?Sized + Q: Hash + Eq + ?Sized, { let hash = self.hash_usize(&key); @@ -1013,7 +1019,11 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> Map<'a, K, V, S> let vptr = &mut *vptr.as_ptr(); - Some(Entry::Occupied(OccupiedEntry::new(shard, key, (kptr, vptr)))) + Some(Entry::Occupied(OccupiedEntry::new( + shard, + key, + (kptr, vptr), + ))) } } else { Some(Entry::Vacant(VacantEntry::new(shard, key))) diff --git a/src/t.rs b/src/t.rs index 40259832..48eaec50 100644 --- a/src/t.rs +++ b/src/t.rs @@ -31,12 +31,18 @@ pub trait Map<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + Clone + BuildHasher> { /// # Safety /// /// The index must not be out of bounds. - unsafe fn _try_yield_read_shard(&'a self, i: usize) -> Option>>; + unsafe fn _try_yield_read_shard( + &'a self, + i: usize, + ) -> Option>>; /// # Safety /// /// The index must not be out of bounds. - unsafe fn _try_yield_write_shard(&'a self, i: usize) -> Option>>; + unsafe fn _try_yield_write_shard( + &'a self, + i: usize, + ) -> Option>>; fn _insert(&self, key: K, value: V) -> Option; diff --git a/src/try_result.rs b/src/try_result.rs index 082c6827..aa93d3b2 100644 --- a/src/try_result.rs +++ b/src/try_result.rs @@ -1,4 +1,3 @@ - /// Represents the result of a non-blocking read from a [DashMap](crate::DashMap). #[derive(Debug)] pub enum TryResult { @@ -12,30 +11,18 @@ pub enum TryResult { impl TryResult { /// Returns `true` if the value was present in the map, and the lock for the shard was successfully obtained. - #[inline] pub fn is_present(&self) -> bool { - match self { - TryResult::Present(_) => true, - _ => false, - } + matches!(self, TryResult::Present(_)) } /// Returns `true` if the shard wasn't locked, and the value wasn't present in the map. - #[inline] pub fn is_absent(&self) -> bool { - match self { - TryResult::Absent => true, - _ => false, - } + matches!(self, TryResult::Absent) } /// Returns `true` if the shard was locked. - #[inline] pub fn is_locked(&self) -> bool { - match self { - TryResult::Locked => true, - _ => false, - } + matches!(self, TryResult::Locked) } /// If `self` is [Present](TryResult::Present), returns the reference to the value in the map.