ringmap/map/core/
entry.rs

1use super::{equivalent, Entries, OffsetIndex, RefMut, RingMapCore};
2use crate::HashValue;
3use core::cmp::Ordering;
4use core::{fmt, mem};
5use hashbrown::hash_table;
6
7impl<K, V> RingMapCore<K, V> {
8    pub(crate) fn entry(&mut self, hash: HashValue, key: K) -> Entry<'_, K, V>
9    where
10        K: Eq,
11    {
12        let entries = &mut self.entries;
13        let offset = &mut self.offset;
14        let eq = equivalent(&key, entries, *offset);
15        match self.indices.find_entry(hash.get(), eq) {
16            Ok(index) => Entry::Occupied(OccupiedEntry {
17                entries,
18                index,
19                offset,
20            }),
21            Err(absent) => Entry::Vacant(VacantEntry {
22                map: RefMut::new(absent.into_table(), entries, offset),
23                hash,
24                key,
25            }),
26        }
27    }
28}
29
30/// Entry for an existing key-value pair in an [`RingMap`][crate::RingMap]
31/// or a vacant location to insert one.
32pub enum Entry<'a, K, V> {
33    /// Existing slot with equivalent key.
34    Occupied(OccupiedEntry<'a, K, V>),
35    /// Vacant slot (no equivalent key in the map).
36    Vacant(VacantEntry<'a, K, V>),
37}
38
39impl<'a, K, V> Entry<'a, K, V> {
40    /// Return the index where the key-value pair exists or will be inserted.
41    pub fn index(&self) -> usize {
42        match *self {
43            Entry::Occupied(ref entry) => entry.index(),
44            Entry::Vacant(ref entry) => entry.index(),
45        }
46    }
47
48    /// Sets the value of the entry (after inserting if vacant), and returns an `OccupiedEntry`.
49    ///
50    /// Computes in **O(1)** time (amortized average).
51    pub fn insert_entry(self, value: V) -> OccupiedEntry<'a, K, V> {
52        match self {
53            Entry::Occupied(mut entry) => {
54                entry.insert(value);
55                entry
56            }
57            Entry::Vacant(entry) => entry.insert_entry(value),
58        }
59    }
60
61    /// Inserts the given default value in the entry if it is vacant and returns a mutable
62    /// reference to it. Otherwise a mutable reference to an already existent value is returned.
63    ///
64    /// Computes in **O(1)** time (amortized average).
65    pub fn or_insert(self, default: V) -> &'a mut V {
66        match self {
67            Entry::Occupied(entry) => entry.into_mut(),
68            Entry::Vacant(entry) => entry.insert(default),
69        }
70    }
71
72    /// Appends the given default value in the entry if it is vacant and returns a mutable
73    /// reference to it. Otherwise a mutable reference to an already existent value is returned.
74    ///
75    /// Computes in **O(1)** time (amortized average).
76    pub fn or_push_back(self, default: V) -> &'a mut V {
77        match self {
78            Entry::Occupied(entry) => entry.into_mut(),
79            Entry::Vacant(entry) => entry.push_back(default),
80        }
81    }
82
83    /// Prepends the given default value in the entry if it is vacant and returns a mutable
84    /// reference to it. Otherwise a mutable reference to an already existent value is returned.
85    ///
86    /// Computes in **O(1)** time (amortized average).
87    pub fn or_push_front(self, default: V) -> &'a mut V {
88        match self {
89            Entry::Occupied(entry) => entry.into_mut(),
90            Entry::Vacant(entry) => entry.push_front(default),
91        }
92    }
93
94    /// Inserts the result of the `call` function in the entry if it is vacant and returns a mutable
95    /// reference to it. Otherwise a mutable reference to an already existent value is returned.
96    ///
97    /// Computes in **O(1)** time (amortized average).
98    pub fn or_insert_with<F>(self, call: F) -> &'a mut V
99    where
100        F: FnOnce() -> V,
101    {
102        match self {
103            Entry::Occupied(entry) => entry.into_mut(),
104            Entry::Vacant(entry) => entry.insert(call()),
105        }
106    }
107
108    /// Inserts the result of the `call` function with a reference to the entry's key if it is
109    /// vacant, and returns a mutable reference to the new value. Otherwise a mutable reference to
110    /// an already existent value is returned.
111    ///
112    /// Computes in **O(1)** time (amortized average).
113    pub fn or_insert_with_key<F>(self, call: F) -> &'a mut V
114    where
115        F: FnOnce(&K) -> V,
116    {
117        match self {
118            Entry::Occupied(entry) => entry.into_mut(),
119            Entry::Vacant(entry) => {
120                let value = call(&entry.key);
121                entry.insert(value)
122            }
123        }
124    }
125
126    /// Gets a reference to the entry's key, either within the map if occupied,
127    /// or else the new key that was used to find the entry.
128    pub fn key(&self) -> &K {
129        match *self {
130            Entry::Occupied(ref entry) => entry.key(),
131            Entry::Vacant(ref entry) => entry.key(),
132        }
133    }
134
135    /// Modifies the entry if it is occupied.
136    pub fn and_modify<F>(mut self, f: F) -> Self
137    where
138        F: FnOnce(&mut V),
139    {
140        if let Entry::Occupied(entry) = &mut self {
141            f(entry.get_mut());
142        }
143        self
144    }
145
146    /// Inserts a default-constructed value in the entry if it is vacant and returns a mutable
147    /// reference to it. Otherwise a mutable reference to an already existent value is returned.
148    ///
149    /// Computes in **O(1)** time (amortized average).
150    pub fn or_default(self) -> &'a mut V
151    where
152        V: Default,
153    {
154        match self {
155            Entry::Occupied(entry) => entry.into_mut(),
156            Entry::Vacant(entry) => entry.insert(V::default()),
157        }
158    }
159}
160
161impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Entry<'_, K, V> {
162    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163        let mut tuple = f.debug_tuple("Entry");
164        match self {
165            Entry::Vacant(v) => tuple.field(v),
166            Entry::Occupied(o) => tuple.field(o),
167        };
168        tuple.finish()
169    }
170}
171
172/// A view into an occupied entry in an [`RingMap`][crate::RingMap].
173/// It is part of the [`Entry`] enum.
174pub struct OccupiedEntry<'a, K, V> {
175    entries: &'a mut Entries<K, V>,
176    offset: &'a mut usize,
177    index: hash_table::OccupiedEntry<'a, OffsetIndex>,
178}
179
180impl<'a, K, V> OccupiedEntry<'a, K, V> {
181    pub(super) fn new(
182        entries: &'a mut Entries<K, V>,
183        offset: &'a mut usize,
184        index: hash_table::OccupiedEntry<'a, OffsetIndex>,
185    ) -> Self {
186        Self {
187            entries,
188            offset,
189            index,
190        }
191    }
192
193    /// Return the index of the key-value pair
194    #[inline]
195    pub fn index(&self) -> usize {
196        self.index.get().get(*self.offset)
197    }
198
199    #[inline]
200    fn into_ref_mut(self) -> RefMut<'a, K, V> {
201        RefMut::new(self.index.into_table(), self.entries, self.offset)
202    }
203
204    /// Gets a reference to the entry's key in the map.
205    ///
206    /// Note that this is not the key that was used to find the entry. There may be an observable
207    /// difference if the key type has any distinguishing features outside of `Hash` and `Eq`, like
208    /// extra fields or the memory address of an allocation.
209    pub fn key(&self) -> &K {
210        &self.entries[self.index()].key
211    }
212
213    pub(crate) fn key_mut(&mut self) -> &mut K {
214        let index = self.index();
215        &mut self.entries[index].key
216    }
217
218    /// Gets a reference to the entry's value in the map.
219    pub fn get(&self) -> &V {
220        &self.entries[self.index()].value
221    }
222
223    /// Gets a mutable reference to the entry's value in the map.
224    ///
225    /// If you need a reference which may outlive the destruction of the
226    /// [`Entry`] value, see [`into_mut`][Self::into_mut].
227    pub fn get_mut(&mut self) -> &mut V {
228        let index = self.index();
229        &mut self.entries[index].value
230    }
231
232    /// Converts into a mutable reference to the entry's value in the map,
233    /// with a lifetime bound to the map itself.
234    pub fn into_mut(self) -> &'a mut V {
235        let index = self.index();
236        &mut self.entries[index].value
237    }
238
239    pub(super) fn into_muts(self) -> (&'a mut K, &'a mut V) {
240        let index = self.index();
241        self.entries[index].muts()
242    }
243
244    /// Sets the value of the entry to `value`, and returns the entry's old value.
245    pub fn insert(&mut self, value: V) -> V {
246        mem::replace(self.get_mut(), value)
247    }
248
249    /// Remove the key, value pair stored in the map for this entry, and return the value.
250    ///
251    /// Like [`VecDeque::remove`][super::VecDeque::remove], the pair is removed by shifting all of
252    /// the elements either before or after it, preserving their relative order.
253    /// **This perturbs the index of all of the following elements!**
254    ///
255    /// Computes in **O(n)** time (average).
256    pub fn remove(self) -> V {
257        self.remove_entry().1
258    }
259
260    /// Remove and return the key, value pair stored in the map for this entry
261    ///
262    /// Like [`VecDeque::remove`][super::VecDeque::remove], the pair is removed by shifting all of
263    /// the elements either before or after it, preserving their relative order.
264    /// **This perturbs the index of all of the following elements!**
265    ///
266    /// Computes in **O(n)** time (average).
267    pub fn remove_entry(self) -> (K, V) {
268        let (index, entry) = self.index.remove();
269        let index = index.get(*self.offset);
270        RefMut::new(entry.into_table(), self.entries, self.offset).shift_remove_finish(index)
271    }
272
273    /// Remove the key, value pair stored in the map for this entry, and return the value.
274    ///
275    /// Like [`VecDeque::swap_remove_back`][super::VecDeque::swap_remove_back], the pair is removed
276    /// by swapping it with the last element of the map and popping it off.
277    /// **This perturbs the position of what used to be the last element!**
278    ///
279    /// Computes in **O(1)** time (average).
280    pub fn swap_remove_back(self) -> V {
281        self.swap_remove_back_entry().1
282    }
283
284    /// Remove and return the key, value pair stored in the map for this entry
285    ///
286    /// Like [`VecDeque::swap_remove_back`][super::VecDeque::swap_remove_back], the pair is removed
287    /// by swapping it with the last element of the map and popping it off.
288    /// **This perturbs the position of what used to be the last element!**
289    ///
290    /// Computes in **O(1)** time (average).
291    pub fn swap_remove_back_entry(self) -> (K, V) {
292        let (index, entry) = self.index.remove();
293        let index = index.get(*self.offset);
294        RefMut::new(entry.into_table(), self.entries, self.offset).swap_remove_back_finish(index)
295    }
296
297    /// Remove the key, value pair stored in the map for this entry, and return the value.
298    ///
299    /// Like [`VecDeque::swap_remove_front`][super::VecDeque::swap_remove_front], the pair is removed
300    /// by swapping it with the front element of the map and popping it off.
301    /// **This perturbs the position of what used to be the front element!**
302    ///
303    /// Computes in **O(1)** time (average).
304    pub fn swap_remove_front(self) -> V {
305        self.swap_remove_front_entry().1
306    }
307
308    /// Remove and return the key, value pair stored in the map for this entry
309    ///
310    /// Like [`VecDeque::swap_remove_front`][super::VecDeque::swap_remove_front], the pair is removed
311    /// by swapping it with the front element of the map and popping it off.
312    /// **This perturbs the position of what used to be the front element!**
313    ///
314    /// Computes in **O(1)** time (average).
315    pub fn swap_remove_front_entry(self) -> (K, V) {
316        let (index, entry) = self.index.remove();
317        let index = index.get(*self.offset);
318        RefMut::new(entry.into_table(), self.entries, self.offset).swap_remove_front_finish(index)
319    }
320
321    /// Moves the position of the entry to a new index
322    /// by shifting all other entries in-between.
323    ///
324    /// This is equivalent to [`RingMap::move_index`][`crate::RingMap::move_index`]
325    /// coming `from` the current [`.index()`][Self::index].
326    ///
327    /// * If `self.index() < to`, the other pairs will shift down while the targeted pair moves up.
328    /// * If `self.index() > to`, the other pairs will shift up while the targeted pair moves down.
329    ///
330    /// ***Panics*** if `to` is out of bounds.
331    ///
332    /// Computes in **O(n)** time (average).
333    #[track_caller]
334    pub fn move_index(self, to: usize) {
335        let index = self.index();
336        self.into_ref_mut().move_index(index, to);
337    }
338
339    /// Swaps the position of entry with another.
340    ///
341    /// This is equivalent to [`RingMap::swap_indices`][`crate::RingMap::swap_indices`]
342    /// with the current [`.index()`][Self::index] as one of the two being swapped.
343    ///
344    /// ***Panics*** if the `other` index is out of bounds.
345    ///
346    /// Computes in **O(1)** time (average).
347    #[track_caller]
348    pub fn swap_indices(self, other: usize) {
349        let index = self.index();
350        self.into_ref_mut().swap_indices(index, other);
351    }
352}
353
354impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for OccupiedEntry<'_, K, V> {
355    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
356        f.debug_struct("OccupiedEntry")
357            .field("key", self.key())
358            .field("value", self.get())
359            .finish()
360    }
361}
362
363impl<'a, K, V> From<IndexedEntry<'a, K, V>> for OccupiedEntry<'a, K, V> {
364    fn from(other: IndexedEntry<'a, K, V>) -> Self {
365        let IndexedEntry {
366            map:
367                RefMut {
368                    indices,
369                    entries,
370                    offset,
371                },
372            index,
373        } = other;
374        let hash = entries[index].hash;
375        let needle = OffsetIndex::new(index, *offset);
376        Self {
377            entries,
378            offset,
379            index: indices
380                .find_entry(hash.get(), move |&i| i == needle)
381                .expect("index not found"),
382        }
383    }
384}
385
386/// A view into a vacant entry in an [`RingMap`][crate::RingMap].
387/// It is part of the [`Entry`] enum.
388pub struct VacantEntry<'a, K, V> {
389    map: RefMut<'a, K, V>,
390    hash: HashValue,
391    key: K,
392}
393
394impl<'a, K, V> VacantEntry<'a, K, V> {
395    /// Return the index where a key-value pair may be inserted.
396    pub fn index(&self) -> usize {
397        self.map.indices.len()
398    }
399
400    /// Gets a reference to the key that was used to find the entry.
401    pub fn key(&self) -> &K {
402        &self.key
403    }
404
405    pub(crate) fn key_mut(&mut self) -> &mut K {
406        &mut self.key
407    }
408
409    /// Takes ownership of the key, leaving the entry vacant.
410    pub fn into_key(self) -> K {
411        self.key
412    }
413
414    /// Inserts the entry's key and the given value into the map,
415    /// and returns a mutable reference to the value.
416    pub fn insert(self, value: V) -> &'a mut V {
417        // this is now redundant...
418        self.push_back(value)
419    }
420
421    /// Inserts the entry's key and the given value into the map, and returns an `OccupiedEntry`.
422    ///
423    /// Computes in **O(1)** time (amortized average).
424    pub fn insert_entry(self, value: V) -> OccupiedEntry<'a, K, V> {
425        self.map.push_back_unique(self.hash, self.key, value)
426    }
427
428    /// Prepends the entry's key and the given value onto the map,
429    /// and returns a mutable reference to the value.
430    pub fn push_front(self, value: V) -> &'a mut V {
431        self.map
432            .push_front_unique(self.hash, self.key, value)
433            .into_mut()
434    }
435
436    /// Appends the entry's key and the given value onto the map,
437    /// and returns a mutable reference to the value.
438    pub fn push_back(self, value: V) -> &'a mut V {
439        self.map
440            .push_back_unique(self.hash, self.key, value)
441            .into_mut()
442    }
443
444    /// Inserts the entry's key and the given value into the map at its ordered
445    /// position among sorted keys, and returns the new index and a mutable
446    /// reference to the value.
447    ///
448    /// If the existing keys are **not** already sorted, then the insertion
449    /// index is unspecified (like [`slice::binary_search`]), but the key-value
450    /// pair is inserted at that position regardless.
451    ///
452    /// Computes in **O(n)** time (average).
453    pub fn insert_sorted(self, value: V) -> (usize, &'a mut V)
454    where
455        K: Ord,
456    {
457        let i = self.map.binary_search_keys(&self.key).unwrap_err();
458        (i, self.shift_insert(i, value))
459    }
460
461    /// Inserts the entry's key and the given value into the map at its ordered
462    /// position among keys sorted by `cmp`, and returns the new index and a
463    /// mutable reference to the value.
464    ///
465    /// If the existing keys are **not** already sorted, then the insertion
466    /// index is unspecified (like [`slice::binary_search`]), but the key-value
467    /// pair is inserted at that position regardless.
468    ///
469    /// Computes in **O(n)** time (average).
470    pub fn insert_sorted_by<F>(self, value: V, mut cmp: F) -> (usize, &'a mut V)
471    where
472        K: Ord,
473        F: FnMut(&K, &V, &K, &V) -> Ordering,
474    {
475        let (Ok(i) | Err(i)) = self
476            .map
477            .binary_search_by(|k, v| cmp(k, v, &self.key, &value));
478        (i, self.shift_insert(i, value))
479    }
480
481    /// Inserts the entry's key and the given value into the map at its ordered
482    /// position using a sort-key extraction function, and returns the new index
483    /// and a mutable reference to the value.
484    ///
485    /// If the existing keys are **not** already sorted, then the insertion
486    /// index is unspecified (like [`slice::binary_search`]), but the key-value
487    /// pair is inserted at that position regardless.
488    ///
489    /// Computes in **O(n)** time (average).
490    pub fn insert_sorted_by_key<B, F>(self, value: V, mut sort_key: F) -> (usize, &'a mut V)
491    where
492        B: Ord,
493        F: FnMut(&K, &V) -> B,
494    {
495        let search_key = sort_key(&self.key, &value);
496        let (Ok(i) | Err(i)) = self.map.binary_search_by_key(&search_key, sort_key);
497        (i, self.shift_insert(i, value))
498    }
499
500    /// Inserts the entry's key and the given value into the map at the given index,
501    /// shifting others to the right, and returns a mutable reference to the value.
502    ///
503    /// ***Panics*** if `index` is out of bounds.
504    ///
505    /// Computes in **O(n)** time (average).
506    #[track_caller]
507    pub fn shift_insert(mut self, index: usize, value: V) -> &'a mut V {
508        self.map
509            .shift_insert_unique(index, self.hash, self.key, value);
510        &mut self.map.entries[index].value
511    }
512
513    /// Replaces the key at the given index with this entry's key, returning the
514    /// old key and an `OccupiedEntry` for that index.
515    ///
516    /// ***Panics*** if `index` is out of bounds.
517    ///
518    /// Computes in **O(1)** time (average).
519    #[track_caller]
520    pub fn replace_index(self, index: usize) -> (K, OccupiedEntry<'a, K, V>) {
521        self.map.replace_index_unique(index, self.hash, self.key)
522    }
523}
524
525impl<K: fmt::Debug, V> fmt::Debug for VacantEntry<'_, K, V> {
526    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
527        f.debug_tuple("VacantEntry").field(self.key()).finish()
528    }
529}
530
531/// A view into an occupied entry in an [`RingMap`][crate::RingMap] obtained by index.
532///
533/// This `struct` is created from the [`get_index_entry`][crate::RingMap::get_index_entry] method.
534pub struct IndexedEntry<'a, K, V> {
535    map: RefMut<'a, K, V>,
536    // We have a mutable reference to the map, which keeps the index
537    // valid and pointing to the correct entry.
538    index: usize,
539}
540
541impl<'a, K, V> IndexedEntry<'a, K, V> {
542    pub(crate) fn new(map: &'a mut RingMapCore<K, V>, index: usize) -> Self {
543        Self {
544            map: map.borrow_mut(),
545            index,
546        }
547    }
548
549    /// Return the index of the key-value pair
550    #[inline]
551    pub fn index(&self) -> usize {
552        self.index
553    }
554
555    /// Gets a reference to the entry's key in the map.
556    pub fn key(&self) -> &K {
557        &self.map.entries[self.index].key
558    }
559
560    pub(crate) fn key_mut(&mut self) -> &mut K {
561        &mut self.map.entries[self.index].key
562    }
563
564    /// Gets a reference to the entry's value in the map.
565    pub fn get(&self) -> &V {
566        &self.map.entries[self.index].value
567    }
568
569    /// Gets a mutable reference to the entry's value in the map.
570    ///
571    /// If you need a reference which may outlive the destruction of the
572    /// `IndexedEntry` value, see [`into_mut`][Self::into_mut].
573    pub fn get_mut(&mut self) -> &mut V {
574        &mut self.map.entries[self.index].value
575    }
576
577    /// Sets the value of the entry to `value`, and returns the entry's old value.
578    pub fn insert(&mut self, value: V) -> V {
579        mem::replace(self.get_mut(), value)
580    }
581
582    /// Converts into a mutable reference to the entry's value in the map,
583    /// with a lifetime bound to the map itself.
584    pub fn into_mut(self) -> &'a mut V {
585        &mut self.map.entries[self.index].value
586    }
587
588    /// Remove and return the key, value pair stored in the map for this entry
589    ///
590    /// Like [`VecDeque::remove`][super::VecDeque::remove], the pair is removed by shifting all of the
591    /// elements either before or after it, preserving their relative order.
592    /// **This perturbs the index of all of the following elements!**
593    ///
594    /// Computes in **O(n)** time (average).
595    pub fn remove_entry(mut self) -> (K, V) {
596        self.map.shift_remove_index(self.index).unwrap()
597    }
598
599    /// Remove the key, value pair stored in the map for this entry, and return the value.
600    ///
601    /// Like [`VecDeque::remove`][super::VecDeque::remove], the pair is removed by shifting all of the
602    /// elements either before or after it, preserving their relative order.
603    /// **This perturbs the index of all of the following elements!**
604    ///
605    /// Computes in **O(n)** time (average).
606    pub fn remove(self) -> V {
607        self.remove_entry().1
608    }
609
610    /// Remove and return the key, value pair stored in the map for this entry
611    ///
612    /// Like [`VecDeque::swap_remove_back`][super::VecDeque::swap_remove_back], the pair is removed
613    /// by swapping it with the last element of the map and popping it off.
614    /// **This perturbs the position of what used to be the last element!**
615    ///
616    /// Computes in **O(1)** time (average).
617    pub fn swap_remove_back_entry(mut self) -> (K, V) {
618        self.map.swap_remove_back_index(self.index).unwrap()
619    }
620
621    /// Remove the key, value pair stored in the map for this entry, and return the value.
622    ///
623    /// Like [`VecDeque::swap_remove_back`][super::VecDeque::swap_remove_back], the pair is removed
624    /// by swapping it with the last element of the map and popping it off.
625    /// **This perturbs the position of what used to be the last element!**
626    ///
627    /// Computes in **O(1)** time (average).
628    pub fn swap_remove_back(self) -> V {
629        self.swap_remove_back_entry().1
630    }
631
632    /// Remove and return the key, value pair stored in the map for this entry
633    ///
634    /// Like [`VecDeque::swap_remove_front`][super::VecDeque::swap_remove_front], the pair is removed
635    /// by swapping it with the front element of the map and popping it off.
636    /// **This perturbs the position of what used to be the front element!**
637    ///
638    /// Computes in **O(1)** time (average).
639    pub fn swap_remove_front_entry(mut self) -> (K, V) {
640        self.map.swap_remove_front_index(self.index).unwrap()
641    }
642
643    /// Remove the key, value pair stored in the map for this entry, and return the value.
644    ///
645    /// Like [`VecDeque::swap_remove_front`][super::VecDeque::swap_remove_front], the pair is removed
646    /// by swapping it with the front element of the map and popping it off.
647    /// **This perturbs the position of what used to be the front element!**
648    ///
649    /// Computes in **O(1)** time (average).
650    pub fn swap_remove_front(self) -> V {
651        self.swap_remove_front_entry().1
652    }
653
654    /// Moves the position of the entry to a new index
655    /// by shifting all other entries in-between.
656    ///
657    /// This is equivalent to [`RingMap::move_index`][`crate::RingMap::move_index`]
658    /// coming `from` the current [`.index()`][Self::index].
659    ///
660    /// * If `self.index() < to`, the other pairs will shift down while the targeted pair moves up.
661    /// * If `self.index() > to`, the other pairs will shift up while the targeted pair moves down.
662    ///
663    /// ***Panics*** if `to` is out of bounds.
664    ///
665    /// Computes in **O(n)** time (average).
666    #[track_caller]
667    pub fn move_index(mut self, to: usize) {
668        self.map.move_index(self.index, to);
669    }
670
671    /// Swaps the position of entry with another.
672    ///
673    /// This is equivalent to [`RingMap::swap_indices`][`crate::RingMap::swap_indices`]
674    /// with the current [`.index()`][Self::index] as one of the two being swapped.
675    ///
676    /// ***Panics*** if the `other` index is out of bounds.
677    ///
678    /// Computes in **O(1)** time (average).
679    #[track_caller]
680    pub fn swap_indices(mut self, other: usize) {
681        self.map.swap_indices(self.index, other);
682    }
683}
684
685impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IndexedEntry<'_, K, V> {
686    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
687        f.debug_struct("IndexedEntry")
688            .field("index", &self.index)
689            .field("key", self.key())
690            .field("value", self.get())
691            .finish()
692    }
693}
694
695impl<'a, K, V> From<OccupiedEntry<'a, K, V>> for IndexedEntry<'a, K, V> {
696    fn from(other: OccupiedEntry<'a, K, V>) -> Self {
697        Self {
698            index: other.index(),
699            map: other.into_ref_mut(),
700        }
701    }
702}