Skip to content

Commit 82a45ea

Browse files
committed
uninserted key
1 parent 867db72 commit 82a45ea

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

src/rustc_entry.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ where
3838
RustcEntry::Occupied(RustcOccupiedEntry {
3939
elem,
4040
table: &mut self.table,
41+
uninserted_key: Some(key)
4142
})
4243
} else {
4344
// Ideally we would put this in VacantEntry::insert, but Entry is not
@@ -85,6 +86,8 @@ pub struct RustcOccupiedEntry<'a, K, V, A = Global>
8586
where
8687
A: Allocator,
8788
{
89+
/// Key that wasn't inserted because of an occupied entry.
90+
uninserted_key: Option<K>,
8891
elem: Bucket<(K, V)>,
8992
table: &'a mut RawTable<(K, V), A>,
9093
}
@@ -304,6 +307,26 @@ impl<'a, K, V, A: Allocator> RustcOccupiedEntry<'a, K, V, A> {
304307
unsafe { &self.elem.as_ref().0 }
305308
}
306309

310+
/// Gets a reference to the key that wasn't inserted because of an occupied entry.
311+
///
312+
/// # Examples
313+
///
314+
/// ```
315+
/// let mut map: HashMap<String, u32> = HashMap::new();
316+
///
317+
/// // Pre-insert so we get an occupied entry
318+
/// map.insert("poneyland".to_string(), 12);
319+
///
320+
/// if let RustcEntry::Occupied(o) = map.rustc_entry("poneyland".to_string()) {
321+
/// let key = o.uninserted_key();
322+
/// println!("Got key back: {:?}", key);
323+
/// }
324+
/// ```
325+
#[cfg_attr(feature = "inline-more", inline)]
326+
pub fn uninserted_key(&mut self) -> &mut Option<K> {
327+
&mut self.uninserted_key
328+
}
329+
307330
/// Take the ownership of the key and value from the map.
308331
///
309332
/// # Examples
@@ -534,6 +557,7 @@ impl<'a, K, V, A: Allocator> RustcVacantEntry<'a, K, V, A> {
534557
RustcOccupiedEntry {
535558
elem: bucket,
536559
table: self.table,
560+
uninserted_key: None
537561
}
538562
}
539563
}

0 commit comments

Comments
 (0)