rune_alloc/btree/map/
entry.rs

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