Skip to content

Commit 2de6ddd

Browse files
committed
Auto merge of rust-lang#32248 - dstu:master, r=alexcrichton
Expose the key of Entry variants for HashMap and BTreeMap. This PR addresses [issue 1541](rust-lang/rfcs#1541) by exposing the key of `HashMap` and `BTreeMap` entry variants. Basic tests are provided.
2 parents a77d7bd + 2ddba6f commit 2de6ddd

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

src/libcollections/btree/map.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,13 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
14651465
}
14661466

14671467
impl<'a, K: Ord, V> VacantEntry<'a, K, V> {
1468+
/// Gets a reference to the key that would be used when inserting a value
1469+
/// through the VacantEntry.
1470+
#[unstable(feature = "map_entry_keys", issue = "32281")]
1471+
pub fn key(&self) -> &K {
1472+
&self.key
1473+
}
1474+
14681475
/// Sets the value of the entry with the VacantEntry's key,
14691476
/// and returns a mutable reference to it.
14701477
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1509,6 +1516,12 @@ impl<'a, K: Ord, V> VacantEntry<'a, K, V> {
15091516
}
15101517

15111518
impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
1519+
/// Gets a reference to the key in the entry.
1520+
#[unstable(feature = "map_entry_keys", issue = "32281")]
1521+
pub fn key(&self) -> &K {
1522+
self.handle.reborrow().into_kv().0
1523+
}
1524+
15121525
/// Gets a reference to the value in the entry.
15131526
#[stable(feature = "rust1", since = "1.0.0")]
15141527
pub fn get(&self) -> &V {

src/libcollectionstest/btree/map.rs

+36
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,42 @@ fn test_variance() {
395395
fn vals<'a, 'new>(v: Values<'a, (), &'static str>) -> Values<'a, (), &'new str> { v }
396396
}
397397

398+
#[test]
399+
fn test_occupied_entry_key() {
400+
let mut a = BTreeMap::new();
401+
let key = "hello there";
402+
let value = "value goes here";
403+
assert!(a.is_empty());
404+
a.insert(key.clone(), value.clone());
405+
assert_eq!(a.len(), 1);
406+
assert_eq!(a[key], value);
407+
408+
match a.entry(key.clone()) {
409+
Vacant(_) => panic!(),
410+
Occupied(e) => assert_eq!(key, *e.key()),
411+
}
412+
assert_eq!(a.len(), 1);
413+
assert_eq!(a[key], value);
414+
}
415+
416+
#[test]
417+
fn test_vacant_entry_key() {
418+
let mut a = BTreeMap::new();
419+
let key = "hello there";
420+
let value = "value goes here";
421+
422+
assert!(a.is_empty());
423+
match a.entry(key.clone()) {
424+
Occupied(_) => panic!(),
425+
Vacant(e) => {
426+
assert_eq!(key, *e.key());
427+
e.insert(value.clone());
428+
},
429+
}
430+
assert_eq!(a.len(), 1);
431+
assert_eq!(a[key], value);
432+
}
433+
398434
mod bench {
399435
use std::collections::BTreeMap;
400436
use std::__rand::{Rng, thread_rng};

src/libcollectionstest/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#![feature(fn_traits)]
2222
#![feature(enumset)]
2323
#![feature(iter_arith)]
24+
#![feature(map_entry_keys)]
2425
#![feature(pattern)]
2526
#![feature(rand)]
2627
#![feature(set_recovery)]

src/libstd/collections/hash/map.rs

+49
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,12 @@ impl<'a, K, V> Entry<'a, K, V> {
15541554
}
15551555

15561556
impl<'a, K, V> OccupiedEntry<'a, K, V> {
1557+
/// Gets a reference to the key in the entry.
1558+
#[unstable(feature = "map_entry_keys", issue = "32281")]
1559+
pub fn key(&self) -> &K {
1560+
self.elem.read().0
1561+
}
1562+
15571563
/// Gets a reference to the value in the entry.
15581564
#[stable(feature = "rust1", since = "1.0.0")]
15591565
pub fn get(&self) -> &V {
@@ -1589,6 +1595,13 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
15891595
}
15901596

15911597
impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> {
1598+
/// Gets a reference to the key that would be used when inserting a value
1599+
/// through the VacantEntry.
1600+
#[unstable(feature = "map_entry_keys", issue = "32281")]
1601+
pub fn key(&self) -> &K {
1602+
&self.key
1603+
}
1604+
15921605
/// Sets the value of the entry with the VacantEntry's key,
15931606
/// and returns a mutable reference to it
15941607
#[stable(feature = "rust1", since = "1.0.0")]
@@ -2434,4 +2447,40 @@ mod test_map {
24342447
a.insert(item, 0);
24352448
assert!(a.capacity() > a.len());
24362449
}
2450+
2451+
#[test]
2452+
fn test_occupied_entry_key() {
2453+
let mut a = HashMap::new();
2454+
let key = "hello there";
2455+
let value = "value goes here";
2456+
assert!(a.is_empty());
2457+
a.insert(key.clone(), value.clone());
2458+
assert_eq!(a.len(), 1);
2459+
assert_eq!(a[key], value);
2460+
2461+
match a.entry(key.clone()) {
2462+
Vacant(_) => panic!(),
2463+
Occupied(e) => assert_eq!(key, *e.key()),
2464+
}
2465+
assert_eq!(a.len(), 1);
2466+
assert_eq!(a[key], value);
2467+
}
2468+
2469+
#[test]
2470+
fn test_vacant_entry_key() {
2471+
let mut a = HashMap::new();
2472+
let key = "hello there";
2473+
let value = "value goes here";
2474+
2475+
assert!(a.is_empty());
2476+
match a.entry(key.clone()) {
2477+
Occupied(_) => panic!(),
2478+
Vacant(e) => {
2479+
assert_eq!(key, *e.key());
2480+
e.insert(value.clone());
2481+
},
2482+
}
2483+
assert_eq!(a.len(), 1);
2484+
assert_eq!(a[key], value);
2485+
}
24372486
}

0 commit comments

Comments
 (0)