const_lru/entry/
occupied.rs

1use num_traits::{PrimInt, Unsigned};
2
3use crate::ConstLru;
4
5/// A view into an occupied entry in a ConstLru. It is part of the Entry enum.
6#[derive(Debug)]
7pub struct OccupiedEntry<'a, K, V, const CAP: usize, I: PrimInt + Unsigned> {
8    const_lru: &'a mut ConstLru<K, V, CAP, I>,
9    key: K,
10    index: I,
11    bs_i: I,
12}
13
14impl<'a, K, V, const CAP: usize, I: PrimInt + Unsigned> OccupiedEntry<'a, K, V, CAP, I> {
15    pub(crate) fn new(
16        const_lru: &'a mut ConstLru<K, V, CAP, I>,
17        key: K,
18        (index, bs_i): (I, I),
19    ) -> Self {
20        Self {
21            const_lru,
22            key,
23            index,
24            bs_i,
25        }
26    }
27
28    /// Gets a reference to the value in the entry and moves entry to most recently used slot
29    ///
30    /// To not update to most-recently-used, use [`Self::get_untouched`]
31    pub fn get(&mut self) -> &V {
32        self.const_lru.move_to_head(self.index);
33        self.const_lru.get_by_index(self.index)
34    }
35
36    /// Gets a reference to the value in the entry
37    pub fn get_untouched(&self) -> &V {
38        self.const_lru.get_by_index(self.index)
39    }
40
41    /// Gets a mutable reference to the value in the entry and moves entry to most recently used slot
42    ///
43    /// To not update to most-recently-used, use [`Self::get_mut_untouched`]
44    pub fn get_mut(&mut self) -> &mut V {
45        self.const_lru.move_to_head(self.index);
46        self.const_lru.get_mut_by_index(self.index)
47    }
48
49    /// Gets a mutable reference to the value in the entry
50    pub fn get_mut_untouched(&mut self) -> &mut V {
51        self.const_lru.get_mut_by_index(self.index)
52    }
53
54    /// Sets the value of the entry, and returns the entry's old value.
55    pub fn insert(&mut self, v: V) -> V {
56        self.const_lru.insert_replace_value(self.index, v)
57    }
58
59    /// Converts the `OccupiedEntry` into a mutable reference to the value in the entry with a lifetime bound to the ConstLru itself.
60    /// Also moves the entry to most recently used slot
61    ///
62    /// To not update to most-recently-used, use [`Self::into_mut_untouched`]
63    pub fn into_mut(self) -> &'a mut V {
64        self.const_lru.move_to_head(self.index);
65        self.const_lru.get_mut_by_index(self.index)
66    }
67
68    /// Converts the `OccupiedEntry` into a mutable reference to the value in the entry with a lifetime bound to the ConstLru itself.
69    pub fn into_mut_untouched(self) -> &'a mut V {
70        self.const_lru.get_mut_by_index(self.index)
71    }
72
73    /// Gets a reference to the key in the entry.
74    pub fn key(&self) -> &K {
75        &self.key
76    }
77
78    /// Takes the value out of the entry, and returns it.
79    pub fn remove(self) -> V {
80        self.const_lru.remove_by_index((self.index, self.bs_i)).1
81    }
82
83    /// Take the ownership of the key and value from the ConstLru
84    pub fn remove_entry(self) -> (K, V) {
85        self.const_lru.remove_by_index((self.index, self.bs_i))
86    }
87}