use num_traits::{PrimInt, Unsigned};
use crate::ConstLru;
#[derive(Debug)]
pub struct OccupiedEntry<'a, K, V, const CAP: usize, I: PrimInt + Unsigned> {
const_lru: &'a mut ConstLru<K, V, CAP, I>,
key: K,
index: I,
bs_i: I,
}
impl<'a, K, V, const CAP: usize, I: PrimInt + Unsigned> OccupiedEntry<'a, K, V, CAP, I> {
pub(crate) fn new(
const_lru: &'a mut ConstLru<K, V, CAP, I>,
key: K,
(index, bs_i): (I, I),
) -> Self {
Self {
const_lru,
key,
index,
bs_i,
}
}
pub fn get(&mut self) -> &V {
self.const_lru.move_to_head(self.index);
self.const_lru.get_by_index(self.index)
}
pub fn get_untouched(&self) -> &V {
self.const_lru.get_by_index(self.index)
}
pub fn get_mut(&mut self) -> &mut V {
self.const_lru.move_to_head(self.index);
self.const_lru.get_mut_by_index(self.index)
}
pub fn get_mut_untouched(&mut self) -> &mut V {
self.const_lru.get_mut_by_index(self.index)
}
pub fn insert(&mut self, v: V) -> V {
self.const_lru.insert_replace_value(self.index, v)
}
pub fn into_mut(self) -> &'a mut V {
self.const_lru.move_to_head(self.index);
self.const_lru.get_mut_by_index(self.index)
}
pub fn into_mut_untouched(self) -> &'a mut V {
self.const_lru.get_mut_by_index(self.index)
}
pub fn key(&self) -> &K {
&self.key
}
pub fn remove(self) -> V {
self.const_lru.remove_by_index((self.index, self.bs_i)).1
}
pub fn remove_entry(self) -> (K, V) {
self.const_lru.remove_by_index((self.index, self.bs_i))
}
}