|
38 | 38 | RustcEntry::Occupied(RustcOccupiedEntry { |
39 | 39 | elem, |
40 | 40 | table: &mut self.table, |
| 41 | + uninserted_key: Some(key) |
41 | 42 | }) |
42 | 43 | } else { |
43 | 44 | // Ideally we would put this in VacantEntry::insert, but Entry is not |
@@ -85,6 +86,8 @@ pub struct RustcOccupiedEntry<'a, K, V, A = Global> |
85 | 86 | where |
86 | 87 | A: Allocator, |
87 | 88 | { |
| 89 | + /// Key that wasn't inserted because of an occupied entry. |
| 90 | + uninserted_key: Option<K>, |
88 | 91 | elem: Bucket<(K, V)>, |
89 | 92 | table: &'a mut RawTable<(K, V), A>, |
90 | 93 | } |
@@ -304,6 +307,26 @@ impl<'a, K, V, A: Allocator> RustcOccupiedEntry<'a, K, V, A> { |
304 | 307 | unsafe { &self.elem.as_ref().0 } |
305 | 308 | } |
306 | 309 |
|
| 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 | + |
307 | 330 | /// Take the ownership of the key and value from the map. |
308 | 331 | /// |
309 | 332 | /// # Examples |
@@ -534,6 +557,7 @@ impl<'a, K, V, A: Allocator> RustcVacantEntry<'a, K, V, A> { |
534 | 557 | RustcOccupiedEntry { |
535 | 558 | elem: bucket, |
536 | 559 | table: self.table, |
| 560 | + uninserted_key: None |
537 | 561 | } |
538 | 562 | } |
539 | 563 | } |
|
0 commit comments