From 60fcc1a890937ce04593e8f14d20f456c1c07e77 Mon Sep 17 00:00:00 2001 From: Diggory Blake Date: Mon, 25 Dec 2017 00:00:04 +0000 Subject: [PATCH 1/2] Implement get_pair for HashMap --- src/libstd/collections/hash/map.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 82a687ae5e493..da669bcf375c7 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -1121,6 +1121,36 @@ impl HashMap self.search(k).into_occupied_bucket().map(|bucket| bucket.into_refs().1) } + /// Returns a references to the key-value pair corresponding to the supplied + /// key. + /// + /// The supplied key may be any borrowed form of the map's key type, but + /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for + /// the key type. + /// + /// [`Eq`]: ../../std/cmp/trait.Eq.html + /// [`Hash`]: ../../std/hash/trait.Hash.html + /// + /// # Examples + /// + /// ``` + /// #![feature(hashmap_get_pair)] + /// use std::collections::HashMap; + /// + /// let mut map = HashMap::new(); + /// map.insert(1, "a"); + /// assert_eq!(map.get_pair(&1), Some((&1, &"a"))); + /// assert_eq!(map.get_pair(&2), None); + /// ``` + #[unstable(feature = "hashmap_get_pair", issue = "43143")] + #[inline] + pub fn get_pair(&self, k: &Q) -> Option<(&K, &V)> + where K: Borrow, + Q: Hash + Eq + { + self.search(k).into_occupied_bucket().map(|bucket| bucket.into_refs()) + } + /// Returns true if the map contains a value for the specified key. /// /// The key may be any borrowed form of the map's key type, but From 3342ae7643b2604a3c6ffb3602a62173e3b0f2a1 Mon Sep 17 00:00:00 2001 From: Diggory Blake Date: Wed, 31 Jan 2018 00:58:57 +0000 Subject: [PATCH 2/2] Implement get_pair for BTreeMap --- src/liballoc/btree/map.rs | 29 +++++++++++++++++++++++++++++ src/libstd/collections/hash/map.rs | 4 ++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/liballoc/btree/map.rs b/src/liballoc/btree/map.rs index b320bed54320a..eaf110706ca0a 100644 --- a/src/liballoc/btree/map.rs +++ b/src/liballoc/btree/map.rs @@ -576,6 +576,35 @@ impl BTreeMap { } } + /// Returns a references to the key-value pair corresponding to the supplied + /// key. + /// + /// The supplied key may be any borrowed form of the map's key type, but the ordering + /// on the borrowed form *must* match the ordering on the key type. + /// + /// # Examples + /// + /// ``` + /// #![feature(map_get_pair)] + /// use std::collections::BTreeMap; + /// + /// let mut map = BTreeMap::new(); + /// map.insert(1, "a"); + /// assert_eq!(map.get_pair(&1), Some((&1, &"a"))); + /// assert_eq!(map.get_pair(&2), None); + /// ``` + #[unstable(feature = "map_get_pair", issue = "43143")] + #[inline] + pub fn get_pair(&self, k: &Q) -> Option<(&K, &V)> + where K: Borrow, + Q: Ord + { + match search::search_tree(self.root.as_ref(), key) { + Found(handle) => Some(handle.into_kv()), + GoDown(_) => None, + } + } + /// Returns `true` if the map contains a value for the specified key. /// /// The key may be any borrowed form of the map's key type, but the ordering diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index da669bcf375c7..46d77b8b9e029 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -1134,7 +1134,7 @@ impl HashMap /// # Examples /// /// ``` - /// #![feature(hashmap_get_pair)] + /// #![feature(map_get_pair)] /// use std::collections::HashMap; /// /// let mut map = HashMap::new(); @@ -1142,7 +1142,7 @@ impl HashMap /// assert_eq!(map.get_pair(&1), Some((&1, &"a"))); /// assert_eq!(map.get_pair(&2), None); /// ``` - #[unstable(feature = "hashmap_get_pair", issue = "43143")] + #[unstable(feature = "map_get_pair", issue = "43143")] #[inline] pub fn get_pair(&self, k: &Q) -> Option<(&K, &V)> where K: Borrow,