From 749c1dac5f4f2b0bb9a0a7b709d7c3073bfb6247 Mon Sep 17 00:00:00 2001 From: Acrimon Date: Wed, 26 Feb 2020 13:06:37 +0100 Subject: [PATCH] Small fixes and doc improvements. v3.5.1 --- Cargo.toml | 2 +- src/lib.rs | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index affd7c89..3ee6e757 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dashmap" -version = "3.5.0" +version = "3.5.1" authors = ["Acrimon "] edition = "2018" license = "MIT" diff --git a/src/lib.rs b/src/lib.rs index ed09e232..6c037043 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -266,7 +266,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap { /// ``` /// use dashmap::DashMap; /// - /// let map = DashMap::with_capacity(2); + /// let map = DashMap::new(); /// map.insert("I am the key!", "And I am the value!"); /// ``` #[inline] @@ -281,7 +281,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap { /// ``` /// use dashmap::DashMap; /// - /// let soccer_team = DashMap::with_capacity(2); + /// let soccer_team = DashMap::new(); /// soccer_team.insert("Jack", "Goalie"); /// assert_eq!(soccer_team.remove("Jack").unwrap().1, "Goalie"); /// ``` @@ -294,6 +294,34 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap { self._remove(key) } + /// Removes an entry from the map, returning the key and value + /// if the entry existed and the provided conditional function returned true. + /// + /// ``` + /// use dashmap::DashMap; + /// + /// let soccer_team = DashMap::new(); + /// soccer_team.insert("Sam", "Forward"); + /// soccer_team.remove_if("Sam", |_, position| position == &"Goalie"); + /// assert!(soccer_team.contains_key("Sam")); + /// ``` + /// ``` + /// use dashmap::DashMap; + /// + /// let soccer_team = DashMap::new(); + /// soccer_team.insert("Sam", "Forward"); + /// soccer_team.remove_if("Sam", |_, position| position == &"Forward"); + /// assert!(!soccer_team.contains_key("Sam")); + /// ``` + #[inline] + pub fn remove_if(&self, key: &Q, f: impl FnOnce(&K, &V) -> bool) -> Option<(K, V)> + where + K: Borrow, + Q: Hash + Eq + ?Sized, + { + self._remove_if(key, f) + } + /// Creates an iterator over a DashMap yielding immutable references. /// /// # Examples