Skip to content

Commit

Permalink
Auto merge of rust-lang#32248 - dstu:master, r=alexcrichton
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Mar 18, 2016
2 parents a77d7bd + 2ddba6f commit 2de6ddd
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/libcollections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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 {
Expand Down
36 changes: 36 additions & 0 deletions src/libcollectionstest/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
1 change: 1 addition & 0 deletions src/libcollectionstest/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#![feature(fn_traits)]
#![feature(enumset)]
#![feature(iter_arith)]
#![feature(map_entry_keys)]
#![feature(pattern)]
#![feature(rand)]
#![feature(set_recovery)]
Expand Down
49 changes: 49 additions & 0 deletions src/libstd/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 2de6ddd

Please sign in to comment.