diff --git a/agb-hashmap/src/lib.rs b/agb-hashmap/src/lib.rs index 6a77a571..c2b6ddca 100644 --- a/agb-hashmap/src/lib.rs +++ b/agb-hashmap/src/lib.rs @@ -376,7 +376,25 @@ where /// Returns a mutable reference to the value corresponding to the key. Return [`None`] if /// there is no element in the map with the given key. - pub fn get_mut(&mut self, key: &K) -> Option<&mut V> { + /// + /// # Example + /// ``` + /// use agb_hashmap::HashMap; + /// + /// let mut map = HashMap::new(); + /// map.insert("a".to_string(), "A"); + /// + /// if let Some(x) = map.get_mut("a") { + /// *x = "b"; + /// } + /// + /// assert_eq!(map["a"], "b"); + /// ``` + pub fn get_mut(&mut self, key: &Q) -> Option<&mut V> + where + K: Borrow, + Q: Hash + Eq + ?Sized, + { let hash = self.hash(key); if let Some(location) = self.nodes.location(key, hash) { @@ -732,28 +750,18 @@ where } } -impl Index<&K> for HashMap +impl Index<&Q> for HashMap where - K: Eq + Hash, + K: Eq + Hash + Borrow, + Q: Eq + Hash + ?Sized, { type Output = V; - fn index(&self, key: &K) -> &V { + fn index(&self, key: &Q) -> &V { self.get(key).expect("no entry found for key") } } -impl Index for HashMap -where - K: Eq + Hash, -{ - type Output = V; - - fn index(&self, key: K) -> &V { - self.get(&key).expect("no entry found for key") - } -} - const fn number_before_resize(capacity: usize) -> usize { capacity * 85 / 100 }