Skip to main content

alloc/collections/btree/map/
entry.rs

1use core::fmt::{self, Debug};
2use core::marker::PhantomData;
3use core::mem;
4
5use Entry::*;
6
7use super::super::borrow::DormantMutRef;
8use super::super::node::{Handle, NodeRef, marker};
9use super::BTreeMap;
10use crate::alloc::{Allocator, Global};
11
12/// A view into a single entry in a map, which may either be vacant or occupied.
13///
14/// This `enum` is constructed from the [`entry`] method on [`BTreeMap`].
15///
16/// [`entry`]: BTreeMap::entry
17#[stable(feature = "rust1", since = "1.0.0")]
18#[cfg_attr(not(test), rustc_diagnostic_item = "BTreeEntry")]
19pub enum Entry<
20    'a,
21    K: 'a,
22    V: 'a,
23    #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + Clone = Global,
24> {
25    /// A vacant entry.
26    #[stable(feature = "rust1", since = "1.0.0")]
27    Vacant(#[stable(feature = "rust1", since = "1.0.0")] VacantEntry<'a, K, V, A>),
28
29    /// An occupied entry.
30    #[stable(feature = "rust1", since = "1.0.0")]
31    Occupied(#[stable(feature = "rust1", since = "1.0.0")] OccupiedEntry<'a, K, V, A>),
32}
33
34#[stable(feature = "debug_btree_map", since = "1.12.0")]
35impl<K: Debug + Ord, V: Debug, A: Allocator + Clone> Debug for Entry<'_, K, V, A> {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match *self {
38            Vacant(ref v) => f.debug_tuple("Entry").field(v).finish(),
39            Occupied(ref o) => f.debug_tuple("Entry").field(o).finish(),
40        }
41    }
42}
43
44/// A view into a vacant entry in a `BTreeMap`.
45/// It is part of the [`Entry`] enum.
46#[stable(feature = "rust1", since = "1.0.0")]
47pub struct VacantEntry<
48    'a,
49    K,
50    V,
51    #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + Clone = Global,
52> {
53    pub(super) key: K,
54    /// `None` for a (empty) map without root
55    pub(super) handle: Option<Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge>>,
56    pub(super) dormant_map: DormantMutRef<'a, BTreeMap<K, V, A>>,
57
58    /// The BTreeMap will outlive this IntoIter so we don't care about drop order for `alloc`.
59    pub(super) alloc: A,
60
61    // Be invariant in `K` and `V`
62    pub(super) _marker: PhantomData<&'a mut (K, V)>,
63}
64
65#[stable(feature = "debug_btree_map", since = "1.12.0")]
66impl<K: Debug + Ord, V, A: Allocator + Clone> Debug for VacantEntry<'_, K, V, A> {
67    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68        f.debug_tuple("VacantEntry").field(self.key()).finish()
69    }
70}
71
72/// A view into an occupied entry in a `BTreeMap`.
73/// It is part of the [`Entry`] enum.
74#[stable(feature = "rust1", since = "1.0.0")]
75pub struct OccupiedEntry<
76    'a,
77    K,
78    V,
79    #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + Clone = Global,
80> {
81    pub(super) handle: Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal>, marker::KV>,
82    pub(super) dormant_map: DormantMutRef<'a, BTreeMap<K, V, A>>,
83
84    /// The BTreeMap will outlive this IntoIter so we don't care about drop order for `alloc`.
85    pub(super) alloc: A,
86
87    // Be invariant in `K` and `V`
88    pub(super) _marker: PhantomData<&'a mut (K, V)>,
89}
90
91#[stable(feature = "debug_btree_map", since = "1.12.0")]
92impl<K: Debug + Ord, V: Debug, A: Allocator + Clone> Debug for OccupiedEntry<'_, K, V, A> {
93    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94        f.debug_struct("OccupiedEntry").field("key", self.key()).field("value", self.get()).finish()
95    }
96}
97
98/// The error returned by [`try_insert`](BTreeMap::try_insert) when the key already exists.
99///
100/// Contains the occupied entry, key, and the value that was not inserted.
101#[unstable(feature = "map_try_insert", issue = "82766")]
102#[non_exhaustive]
103pub struct OccupiedError<
104    'a,
105    K: 'a,
106    V: 'a,
107    #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + Clone = Global,
108> {
109    /// The entry in the map that was already occupied.
110    pub entry: OccupiedEntry<'a, K, V, A>,
111    /// The key which was not inserted, because the entry was already occupied.
112    pub key: K,
113    /// The value which was not inserted, because the entry was already occupied.
114    pub value: V,
115}
116
117#[unstable(feature = "map_try_insert", issue = "82766")]
118impl<K: Debug + Ord, V: Debug, A: Allocator + Clone> Debug for OccupiedError<'_, K, V, A> {
119    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
120        f.debug_struct("OccupiedError")
121            .field("key", self.entry.key())
122            .field("uninserted_key", &self.key)
123            .field("old_value", self.entry.get())
124            .field("new_value", &self.value)
125            .finish()
126    }
127}
128
129impl<'a, K: Ord, V, A: Allocator + Clone> Entry<'a, K, V, A> {
130    /// Ensures a value is in the entry by inserting the default if empty, and returns
131    /// a mutable reference to the value in the entry.
132    ///
133    /// # Examples
134    ///
135    /// ```
136    /// use std::collections::BTreeMap;
137    ///
138    /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
139    /// map.entry("poneyland").or_insert(12);
140    ///
141    /// assert_eq!(map["poneyland"], 12);
142    /// ```
143    #[stable(feature = "rust1", since = "1.0.0")]
144    pub fn or_insert(self, default: V) -> &'a mut V {
145        match self {
146            Occupied(entry) => entry.into_mut(),
147            Vacant(entry) => entry.insert(default),
148        }
149    }
150
151    /// Ensures a value is in the entry by inserting the result of the default function if empty,
152    /// and returns a mutable reference to the value in the entry.
153    ///
154    /// # Examples
155    ///
156    /// ```
157    /// use std::collections::BTreeMap;
158    ///
159    /// let mut map: BTreeMap<&str, String> = BTreeMap::new();
160    /// let s = "hoho".to_string();
161    ///
162    /// map.entry("poneyland").or_insert_with(|| s);
163    ///
164    /// assert_eq!(map["poneyland"], "hoho".to_string());
165    /// ```
166    #[stable(feature = "rust1", since = "1.0.0")]
167    pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
168        self.or_try_insert_with(|| Result::<_, !>::Ok(default())).unwrap()
169    }
170
171    /// Ensures a value is in the entry by inserting the result of a fallible default function
172    /// if empty, and returns a mutable reference to the value in the entry.
173    ///
174    /// This method works identically to [`or_insert_with`] except that the default function
175    /// should return a `Result` and, in the case of an error, the error is propagated.
176    ///
177    /// [`or_insert_with`]: Self::or_insert_with
178    ///
179    /// # Examples
180    ///
181    /// ```
182    /// #![feature(try_entry)]
183    /// # fn main() -> Result<(), std::num::ParseIntError> {
184    /// use std::collections::BTreeMap;
185    ///
186    /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
187    /// let value = "42";
188    ///
189    /// map.entry("poneyland").or_try_insert_with(|| value.parse())?;
190    ///
191    /// assert_eq!(map["poneyland"], 42);
192    /// # Ok(())
193    /// # }
194    /// ```
195    #[inline]
196    #[unstable(feature = "try_entry", issue = "157354")]
197    pub fn or_try_insert_with<F: FnOnce() -> Result<V, E>, E>(
198        self,
199        default: F,
200    ) -> Result<&'a mut V, E> {
201        match self {
202            Occupied(entry) => Ok(entry.into_mut()),
203            Vacant(entry) => Ok(entry.insert(default()?)),
204        }
205    }
206
207    /// Ensures a value is in the entry by inserting, if empty, the result of the default function.
208    ///
209    /// This method allows for generating key-derived values for insertion by providing the default
210    /// function a reference to the key that was moved during the `.entry(key)` method call.
211    ///
212    /// The reference to the moved key is provided so that cloning or copying the key is
213    /// unnecessary, unlike with `.or_insert_with(|| ... )`.
214    ///
215    /// # Examples
216    ///
217    /// ```
218    /// use std::collections::BTreeMap;
219    ///
220    /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
221    ///
222    /// map.entry("poneyland").or_insert_with_key(|key| key.chars().count());
223    ///
224    /// assert_eq!(map["poneyland"], 9);
225    /// ```
226    #[inline]
227    #[stable(feature = "or_insert_with_key", since = "1.50.0")]
228    pub fn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V {
229        self.or_try_insert_with_key(|k| Result::<_, !>::Ok(default(k))).into_ok()
230    }
231
232    /// Ensures a value is in the entry by inserting, if empty, the result of the default function.
233    /// This method allows for generating key-derived values for insertion by providing the default
234    /// function a reference to the key that was moved during the `entry(key)` method call.
235    ///
236    /// This method works identically to [`or_insert_with_key`] except that the default function
237    /// should return a `Result` and, in the case of an error, the error is propagated.
238    ///
239    /// [`or_insert_with_key`]: Self::or_insert_with_key
240    ///
241    /// # Examples
242    ///
243    /// ```
244    /// #![feature(try_entry)]
245    /// # fn main() -> Result<(), std::num::ParseIntError> {
246    /// use std::collections::BTreeMap;
247    ///
248    /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
249    ///
250    /// map.entry("42").or_try_insert_with_key(|key| key.parse())?;
251    ///
252    /// assert_eq!(map["42"], 42);
253    /// # Ok(())
254    /// # }
255    /// ```
256    #[inline]
257    #[unstable(feature = "try_entry", issue = "157354")]
258    pub fn or_try_insert_with_key<F: FnOnce(&K) -> Result<V, E>, E>(
259        self,
260        default: F,
261    ) -> Result<&'a mut V, E> {
262        match self {
263            Occupied(entry) => Ok(entry.into_mut()),
264            Vacant(entry) => {
265                let value = default(entry.key())?;
266                Ok(entry.insert(value))
267            }
268        }
269    }
270
271    /// Returns a reference to this entry's key.
272    ///
273    /// # Examples
274    ///
275    /// ```
276    /// use std::collections::BTreeMap;
277    ///
278    /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
279    /// assert_eq!(map.entry("poneyland").key(), &"poneyland");
280    /// ```
281    #[stable(feature = "map_entry_keys", since = "1.10.0")]
282    pub fn key(&self) -> &K {
283        match *self {
284            Occupied(ref entry) => entry.key(),
285            Vacant(ref entry) => entry.key(),
286        }
287    }
288
289    /// Provides in-place mutable access to an occupied entry before any
290    /// potential inserts into the map.
291    ///
292    /// # Examples
293    ///
294    /// ```
295    /// use std::collections::BTreeMap;
296    ///
297    /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
298    ///
299    /// map.entry("poneyland")
300    ///    .and_modify(|e| { *e += 1 })
301    ///    .or_insert(42);
302    /// assert_eq!(map["poneyland"], 42);
303    ///
304    /// map.entry("poneyland")
305    ///    .and_modify(|e| { *e += 1 })
306    ///    .or_insert(42);
307    /// assert_eq!(map["poneyland"], 43);
308    /// ```
309    #[stable(feature = "entry_and_modify", since = "1.26.0")]
310    pub fn and_modify<F>(self, f: F) -> Self
311    where
312        F: FnOnce(&mut V),
313    {
314        match self {
315            Occupied(mut entry) => {
316                f(entry.get_mut());
317                Occupied(entry)
318            }
319            Vacant(entry) => Vacant(entry),
320        }
321    }
322
323    /// Sets the value of the entry, and returns an `OccupiedEntry`.
324    ///
325    /// # Examples
326    ///
327    /// ```
328    /// use std::collections::BTreeMap;
329    ///
330    /// let mut map: BTreeMap<&str, String> = BTreeMap::new();
331    /// let entry = map.entry("poneyland").insert_entry("hoho".to_string());
332    ///
333    /// assert_eq!(entry.key(), &"poneyland");
334    /// ```
335    #[inline]
336    #[stable(feature = "btree_entry_insert", since = "1.92.0")]
337    pub fn insert_entry(self, value: V) -> OccupiedEntry<'a, K, V, A> {
338        match self {
339            Occupied(mut entry) => {
340                entry.insert(value);
341                entry
342            }
343            Vacant(entry) => entry.insert_entry(value),
344        }
345    }
346}
347
348impl<'a, K: Ord, V: Default, A: Allocator + Clone> Entry<'a, K, V, A> {
349    #[stable(feature = "entry_or_default", since = "1.28.0")]
350    /// Ensures a value is in the entry by inserting the default value if empty,
351    /// and returns a mutable reference to the value in the entry.
352    ///
353    /// # Examples
354    ///
355    /// ```
356    /// use std::collections::BTreeMap;
357    ///
358    /// let mut map: BTreeMap<&str, Option<usize>> = BTreeMap::new();
359    /// map.entry("poneyland").or_default();
360    ///
361    /// assert_eq!(map["poneyland"], None);
362    /// ```
363    pub fn or_default(self) -> &'a mut V {
364        match self {
365            Occupied(entry) => entry.into_mut(),
366            Vacant(entry) => entry.insert(Default::default()),
367        }
368    }
369}
370
371impl<'a, K: Ord, V, A: Allocator + Clone> VacantEntry<'a, K, V, A> {
372    /// Gets a reference to the key that would be used when inserting a value
373    /// through the VacantEntry.
374    ///
375    /// # Examples
376    ///
377    /// ```
378    /// use std::collections::BTreeMap;
379    ///
380    /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
381    /// assert_eq!(map.entry("poneyland").key(), &"poneyland");
382    /// ```
383    #[stable(feature = "map_entry_keys", since = "1.10.0")]
384    pub fn key(&self) -> &K {
385        &self.key
386    }
387
388    /// Take ownership of the key.
389    ///
390    /// # Examples
391    ///
392    /// ```
393    /// use std::collections::BTreeMap;
394    /// use std::collections::btree_map::Entry;
395    ///
396    /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
397    ///
398    /// if let Entry::Vacant(v) = map.entry("poneyland") {
399    ///     v.into_key();
400    /// }
401    /// ```
402    #[stable(feature = "map_entry_recover_keys2", since = "1.12.0")]
403    pub fn into_key(self) -> K {
404        self.key
405    }
406
407    /// Sets the value of the entry with the `VacantEntry`'s key,
408    /// and returns a mutable reference to it.
409    ///
410    /// # Examples
411    ///
412    /// ```
413    /// use std::collections::BTreeMap;
414    /// use std::collections::btree_map::Entry;
415    ///
416    /// let mut map: BTreeMap<&str, u32> = BTreeMap::new();
417    ///
418    /// if let Entry::Vacant(o) = map.entry("poneyland") {
419    ///     o.insert(37);
420    /// }
421    /// assert_eq!(map["poneyland"], 37);
422    /// ```
423    #[stable(feature = "rust1", since = "1.0.0")]
424    #[rustc_confusables("push", "put")]
425    pub fn insert(self, value: V) -> &'a mut V {
426        self.insert_entry(value).into_mut()
427    }
428
429    /// Sets the value of the entry with the `VacantEntry`'s key,
430    /// and returns an `OccupiedEntry`.
431    ///
432    /// # Examples
433    ///
434    /// ```
435    /// use std::collections::BTreeMap;
436    /// use std::collections::btree_map::Entry;
437    ///
438    /// let mut map: BTreeMap<&str, u32> = BTreeMap::new();
439    ///
440    /// if let Entry::Vacant(o) = map.entry("poneyland") {
441    ///     let entry = o.insert_entry(37);
442    ///     assert_eq!(entry.get(), &37);
443    /// }
444    /// assert_eq!(map["poneyland"], 37);
445    /// ```
446    #[stable(feature = "btree_entry_insert", since = "1.92.0")]
447    pub fn insert_entry(mut self, value: V) -> OccupiedEntry<'a, K, V, A> {
448        let handle = match self.handle {
449            None => {
450                // SAFETY: There is no tree yet so no reference to it exists.
451                let map = unsafe { self.dormant_map.reborrow() };
452                let root = map.root.insert(NodeRef::new_leaf(self.alloc.clone()).forget_type());
453                // SAFETY: We *just* created the root as a leaf, and we're
454                // stacking the new handle on the original borrow lifetime.
455                unsafe {
456                    let mut leaf = root.borrow_mut().cast_to_leaf_unchecked();
457                    leaf.push_with_handle(self.key, value)
458                }
459            }
460            Some(handle) => handle.insert_recursing(self.key, value, self.alloc.clone(), |ins| {
461                drop(ins.left);
462                // SAFETY: Pushing a new root node doesn't invalidate
463                // handles to existing nodes.
464                let map = unsafe { self.dormant_map.reborrow() };
465                let root = map.root.as_mut().unwrap(); // same as ins.left
466                root.push_internal_level(self.alloc.clone()).push(ins.kv.0, ins.kv.1, ins.right)
467            }),
468        };
469
470        // SAFETY: modifying the length doesn't invalidate handles to existing nodes.
471        unsafe { self.dormant_map.reborrow().length += 1 };
472
473        OccupiedEntry {
474            handle: handle.forget_node_type(),
475            dormant_map: self.dormant_map,
476            alloc: self.alloc,
477            _marker: PhantomData,
478        }
479    }
480}
481
482impl<'a, K: Ord, V, A: Allocator + Clone> OccupiedEntry<'a, K, V, A> {
483    /// Gets a reference to the key in the entry.
484    ///
485    /// # Examples
486    ///
487    /// ```
488    /// use std::collections::BTreeMap;
489    ///
490    /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
491    /// map.entry("poneyland").or_insert(12);
492    /// assert_eq!(map.entry("poneyland").key(), &"poneyland");
493    /// ```
494    #[must_use]
495    #[stable(feature = "map_entry_keys", since = "1.10.0")]
496    pub fn key(&self) -> &K {
497        self.handle.reborrow().into_kv().0
498    }
499
500    /// Converts the entry into a reference to its key.
501    pub(crate) fn into_key(self) -> &'a K {
502        self.handle.into_kv_mut().0
503    }
504
505    /// Take ownership of the key and value from the map.
506    ///
507    /// # Examples
508    ///
509    /// ```
510    /// use std::collections::BTreeMap;
511    /// use std::collections::btree_map::Entry;
512    ///
513    /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
514    /// map.entry("poneyland").or_insert(12);
515    ///
516    /// if let Entry::Occupied(o) = map.entry("poneyland") {
517    ///     // We delete the entry from the map.
518    ///     o.remove_entry();
519    /// }
520    ///
521    /// // If now try to get the value, it will panic:
522    /// // println!("{}", map["poneyland"]);
523    /// ```
524    #[stable(feature = "map_entry_recover_keys2", since = "1.12.0")]
525    pub fn remove_entry(self) -> (K, V) {
526        self.remove_kv()
527    }
528
529    /// Gets a reference to the value in the entry.
530    ///
531    /// # Examples
532    ///
533    /// ```
534    /// use std::collections::BTreeMap;
535    /// use std::collections::btree_map::Entry;
536    ///
537    /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
538    /// map.entry("poneyland").or_insert(12);
539    ///
540    /// if let Entry::Occupied(o) = map.entry("poneyland") {
541    ///     assert_eq!(o.get(), &12);
542    /// }
543    /// ```
544    #[must_use]
545    #[stable(feature = "rust1", since = "1.0.0")]
546    pub fn get(&self) -> &V {
547        self.handle.reborrow().into_kv().1
548    }
549
550    /// Gets a mutable reference to the value in the entry.
551    ///
552    /// If you need a reference to the `OccupiedEntry` that may outlive the
553    /// destruction of the `Entry` value, see [`into_mut`].
554    ///
555    /// [`into_mut`]: OccupiedEntry::into_mut
556    ///
557    /// # Examples
558    ///
559    /// ```
560    /// use std::collections::BTreeMap;
561    /// use std::collections::btree_map::Entry;
562    ///
563    /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
564    /// map.entry("poneyland").or_insert(12);
565    ///
566    /// assert_eq!(map["poneyland"], 12);
567    /// if let Entry::Occupied(mut o) = map.entry("poneyland") {
568    ///     *o.get_mut() += 10;
569    ///     assert_eq!(*o.get(), 22);
570    ///
571    ///     // We can use the same Entry multiple times.
572    ///     *o.get_mut() += 2;
573    /// }
574    /// assert_eq!(map["poneyland"], 24);
575    /// ```
576    #[stable(feature = "rust1", since = "1.0.0")]
577    pub fn get_mut(&mut self) -> &mut V {
578        self.handle.kv_mut().1
579    }
580
581    /// Converts the entry into a mutable reference to its value.
582    ///
583    /// If you need multiple references to the `OccupiedEntry`, see [`get_mut`].
584    ///
585    /// [`get_mut`]: OccupiedEntry::get_mut
586    ///
587    /// # Examples
588    ///
589    /// ```
590    /// use std::collections::BTreeMap;
591    /// use std::collections::btree_map::Entry;
592    ///
593    /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
594    /// map.entry("poneyland").or_insert(12);
595    ///
596    /// assert_eq!(map["poneyland"], 12);
597    /// if let Entry::Occupied(o) = map.entry("poneyland") {
598    ///     *o.into_mut() += 10;
599    /// }
600    /// assert_eq!(map["poneyland"], 22);
601    /// ```
602    #[must_use = "`self` will be dropped if the result is not used"]
603    #[stable(feature = "rust1", since = "1.0.0")]
604    pub fn into_mut(self) -> &'a mut V {
605        self.handle.into_val_mut()
606    }
607
608    /// Sets the value of the entry with the `OccupiedEntry`'s key,
609    /// and returns the entry's old value.
610    ///
611    /// # Examples
612    ///
613    /// ```
614    /// use std::collections::BTreeMap;
615    /// use std::collections::btree_map::Entry;
616    ///
617    /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
618    /// map.entry("poneyland").or_insert(12);
619    ///
620    /// if let Entry::Occupied(mut o) = map.entry("poneyland") {
621    ///     assert_eq!(o.insert(15), 12);
622    /// }
623    /// assert_eq!(map["poneyland"], 15);
624    /// ```
625    #[stable(feature = "rust1", since = "1.0.0")]
626    #[rustc_confusables("push", "put")]
627    pub fn insert(&mut self, value: V) -> V {
628        mem::replace(self.get_mut(), value)
629    }
630
631    /// Takes the value of the entry out of the map, and returns it.
632    ///
633    /// # Examples
634    ///
635    /// ```
636    /// use std::collections::BTreeMap;
637    /// use std::collections::btree_map::Entry;
638    ///
639    /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
640    /// map.entry("poneyland").or_insert(12);
641    ///
642    /// if let Entry::Occupied(o) = map.entry("poneyland") {
643    ///     assert_eq!(o.remove(), 12);
644    /// }
645    /// // If we try to get "poneyland"'s value, it'll panic:
646    /// // println!("{}", map["poneyland"]);
647    /// ```
648    #[stable(feature = "rust1", since = "1.0.0")]
649    #[rustc_confusables("delete", "take")]
650    pub fn remove(self) -> V {
651        self.remove_kv().1
652    }
653
654    // Body of `remove_entry`, probably separate because the name reflects the returned pair.
655    pub(super) fn remove_kv(self) -> (K, V) {
656        let mut emptied_internal_root = false;
657        let (old_kv, _) =
658            self.handle.remove_kv_tracking(|| emptied_internal_root = true, self.alloc.clone());
659        // SAFETY: we consumed the intermediate root borrow, `self.handle`.
660        let map = unsafe { self.dormant_map.awaken() };
661        map.length -= 1;
662        if emptied_internal_root {
663            let root = map.root.as_mut().unwrap();
664            root.pop_internal_level(self.alloc);
665        }
666        old_kv
667    }
668}