mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-01 12:37:37 +00:00
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.
This commit is contained in:
commit
2de6ddd75e
@ -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 {
|
||||
|
@ -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};
|
||||
|
@ -21,6 +21,7 @@
|
||||
#![feature(fn_traits)]
|
||||
#![feature(enumset)]
|
||||
#![feature(iter_arith)]
|
||||
#![feature(map_entry_keys)]
|
||||
#![feature(pattern)]
|
||||
#![feature(rand)]
|
||||
#![feature(set_recovery)]
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user