diff options
| author | bors <bors@rust-lang.org> | 2016-03-18 02:46:35 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2016-03-18 02:46:35 -0700 |
| commit | 2de6ddd75e202acdedfcd05b51a863dcc10459ca (patch) | |
| tree | 556c7c429f526d4b94f6e3c1f09c9691e6d6600a | |
| parent | a77d7bde60fd2ba5d55809a44df0e162a901dbfc (diff) | |
| parent | 2ddba6f361cc8c6c08a1d5a655b11d32935c5bda (diff) | |
| download | rust-2de6ddd75e202acdedfcd05b51a863dcc10459ca.tar.gz rust-2de6ddd75e202acdedfcd05b51a863dcc10459ca.zip | |
Auto merge of #32248 - dstu:master, r=alexcrichton
Expose the key of Entry variants for HashMap and BTreeMap. This PR addresses [issue 1541](https://github.com/rust-lang/rfcs/issues/1541) by exposing the key of `HashMap` and `BTreeMap` entry variants. Basic tests are provided.
| -rw-r--r-- | src/libcollections/btree/map.rs | 13 | ||||
| -rw-r--r-- | src/libcollectionstest/btree/map.rs | 36 | ||||
| -rw-r--r-- | src/libcollectionstest/lib.rs | 1 | ||||
| -rw-r--r-- | src/libstd/collections/hash/map.rs | 49 |
4 files changed, 99 insertions, 0 deletions
diff --git a/src/libcollections/btree/map.rs b/src/libcollections/btree/map.rs index 1967f83ab27..7819c7ef796 100644 --- a/src/libcollections/btree/map.rs +++ b/src/libcollections/btree/map.rs @@ -1465,6 +1465,13 @@ impl<'a, K: Ord, V> Entry<'a, K, V> { } impl<'a, K: Ord, V> VacantEntry<'a, K, V> { + /// Gets a reference to the key that would be used when inserting a value + /// through the VacantEntry. + #[unstable(feature = "map_entry_keys", issue = "32281")] + pub fn key(&self) -> &K { + &self.key + } + /// Sets the value of the entry with the VacantEntry's key, /// and returns a mutable reference to it. #[stable(feature = "rust1", since = "1.0.0")] @@ -1509,6 +1516,12 @@ impl<'a, K: Ord, V> VacantEntry<'a, K, V> { } impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> { + /// Gets a reference to the key in the entry. + #[unstable(feature = "map_entry_keys", issue = "32281")] + pub fn key(&self) -> &K { + self.handle.reborrow().into_kv().0 + } + /// Gets a reference to the value in the entry. #[stable(feature = "rust1", since = "1.0.0")] pub fn get(&self) -> &V { diff --git a/src/libcollectionstest/btree/map.rs b/src/libcollectionstest/btree/map.rs index 7f368f0205b..a27c2847a98 100644 --- a/src/libcollectionstest/btree/map.rs +++ b/src/libcollectionstest/btree/map.rs @@ -395,6 +395,42 @@ fn test_variance() { fn vals<'a, 'new>(v: Values<'a, (), &'static str>) -> Values<'a, (), &'new str> { v } } +#[test] +fn test_occupied_entry_key() { + let mut a = BTreeMap::new(); + let key = "hello there"; + let value = "value goes here"; + assert!(a.is_empty()); + a.insert(key.clone(), value.clone()); + assert_eq!(a.len(), 1); + assert_eq!(a[key], value); + + match a.entry(key.clone()) { + Vacant(_) => panic!(), + Occupied(e) => assert_eq!(key, *e.key()), + } + assert_eq!(a.len(), 1); + assert_eq!(a[key], value); +} + +#[test] +fn test_vacant_entry_key() { + let mut a = BTreeMap::new(); + let key = "hello there"; + let value = "value goes here"; + + assert!(a.is_empty()); + match a.entry(key.clone()) { + Occupied(_) => panic!(), + Vacant(e) => { + assert_eq!(key, *e.key()); + e.insert(value.clone()); + }, + } + assert_eq!(a.len(), 1); + assert_eq!(a[key], value); +} + mod bench { use std::collections::BTreeMap; use std::__rand::{Rng, thread_rng}; diff --git a/src/libcollectionstest/lib.rs b/src/libcollectionstest/lib.rs index 3596b8306a8..62fefaa10f6 100644 --- a/src/libcollectionstest/lib.rs +++ b/src/libcollectionstest/lib.rs @@ -21,6 +21,7 @@ #![feature(fn_traits)] #![feature(enumset)] #![feature(iter_arith)] +#![feature(map_entry_keys)] #![feature(pattern)] #![feature(rand)] #![feature(set_recovery)] diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index ec5629038a4..56d95401a9a 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -1554,6 +1554,12 @@ impl<'a, K, V> Entry<'a, K, V> { } impl<'a, K, V> OccupiedEntry<'a, K, V> { + /// Gets a reference to the key in the entry. + #[unstable(feature = "map_entry_keys", issue = "32281")] + pub fn key(&self) -> &K { + self.elem.read().0 + } + /// Gets a reference to the value in the entry. #[stable(feature = "rust1", since = "1.0.0")] pub fn get(&self) -> &V { @@ -1589,6 +1595,13 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> { } impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> { + /// Gets a reference to the key that would be used when inserting a value + /// through the VacantEntry. + #[unstable(feature = "map_entry_keys", issue = "32281")] + pub fn key(&self) -> &K { + &self.key + } + /// Sets the value of the entry with the VacantEntry's key, /// and returns a mutable reference to it #[stable(feature = "rust1", since = "1.0.0")] @@ -2434,4 +2447,40 @@ mod test_map { a.insert(item, 0); assert!(a.capacity() > a.len()); } + + #[test] + fn test_occupied_entry_key() { + let mut a = HashMap::new(); + let key = "hello there"; + let value = "value goes here"; + assert!(a.is_empty()); + a.insert(key.clone(), value.clone()); + assert_eq!(a.len(), 1); + assert_eq!(a[key], value); + + match a.entry(key.clone()) { + Vacant(_) => panic!(), + Occupied(e) => assert_eq!(key, *e.key()), + } + assert_eq!(a.len(), 1); + assert_eq!(a[key], value); + } + + #[test] + fn test_vacant_entry_key() { + let mut a = HashMap::new(); + let key = "hello there"; + let value = "value goes here"; + + assert!(a.is_empty()); + match a.entry(key.clone()) { + Occupied(_) => panic!(), + Vacant(e) => { + assert_eq!(key, *e.key()); + e.insert(value.clone()); + }, + } + assert_eq!(a.len(), 1); + assert_eq!(a[key], value); + } } |
