const_lru/entry/
occupied.rs1use num_traits::{PrimInt, Unsigned};
2
3use crate::ConstLru;
4
5#[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 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 pub fn get_untouched(&self) -> &V {
38 self.const_lru.get_by_index(self.index)
39 }
40
41 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 pub fn get_mut_untouched(&mut self) -> &mut V {
51 self.const_lru.get_mut_by_index(self.index)
52 }
53
54 pub fn insert(&mut self, v: V) -> V {
56 self.const_lru.insert_replace_value(self.index, v)
57 }
58
59 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 pub fn into_mut_untouched(self) -> &'a mut V {
70 self.const_lru.get_mut_by_index(self.index)
71 }
72
73 pub fn key(&self) -> &K {
75 &self.key
76 }
77
78 pub fn remove(self) -> V {
80 self.const_lru.remove_by_index((self.index, self.bs_i)).1
81 }
82
83 pub fn remove_entry(self) -> (K, V) {
85 self.const_lru.remove_by_index((self.index, self.bs_i))
86 }
87}