Skip to content

Commit 29a4f19

Browse files
committed
Change OccupiedEntry::key() to return the existing key in the map
The new behavior is consistent with `HashMap` entries in the standard library, and it has been our intent to match that API when possible.
1 parent 015eeea commit 29a4f19

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/map.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,28 @@ mod tests {
16481648
assert_eq!(&mut TestEnum::DefaultValue, map.entry(2).or_default());
16491649
}
16501650

1651+
#[test]
1652+
fn occupied_entry_key() {
1653+
// These keys match hash and equality, but their addresses are distinct.
1654+
let (k1, k2) = (&mut 1, &mut 1);
1655+
let k1_ptr = k1 as *const i32;
1656+
let k2_ptr = k2 as *const i32;
1657+
assert_ne!(k1_ptr, k2_ptr);
1658+
1659+
let mut map = IndexMap::new();
1660+
map.insert(k1, "value");
1661+
match map.entry(k2) {
1662+
Entry::Occupied(ref e) => {
1663+
// `OccupiedEntry::key` should reference the key in the map,
1664+
// not the key that was used to find the entry.
1665+
let ptr = *e.key() as *const i32;
1666+
assert_eq!(ptr, k1_ptr);
1667+
assert_ne!(ptr, k2_ptr);
1668+
},
1669+
Entry::Vacant(_) => panic!(),
1670+
}
1671+
}
1672+
16511673
#[test]
16521674
fn keys() {
16531675
let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')];

src/map/core.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,8 @@ impl<'a, K, V> Entry<'a, K, V> {
464464
}
465465
}
466466

467+
/// Gets a reference to the entry's key, either within the map if occupied,
468+
/// or else the new key that was used to find the entry.
467469
pub fn key(&self) -> &K {
468470
match *self {
469471
Entry::Occupied(ref entry) => entry.key(),
@@ -583,6 +585,7 @@ pub struct VacantEntry<'a, K, V> {
583585
}
584586

585587
impl<'a, K, V> VacantEntry<'a, K, V> {
588+
/// Gets a reference to the key that was used to find the entry.
586589
pub fn key(&self) -> &K {
587590
&self.key
588591
}

src/map/core/raw.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,13 @@ unsafe impl<K: Sync, V: Sync> Sync for OccupiedEntry<'_, K, V> {}
104104

105105
// The parent module also adds methods that don't threaten the unsafe encapsulation.
106106
impl<'a, K, V> OccupiedEntry<'a, K, V> {
107+
/// Gets a reference to the entry's key in the map.
108+
///
109+
/// Note that this is not the key that was used to find the entry. There may be an observable
110+
/// difference if the key type has any distinguishing features outside of `Hash` and `Eq`, like
111+
/// extra fields or the memory address of an allocation.
107112
pub fn key(&self) -> &K {
108-
&self.key
113+
&self.map.entries[self.index()].key
109114
}
110115

111116
pub fn get(&self) -> &V {

0 commit comments

Comments
 (0)