pub struct ConstLru<K, V, const CAP: usize, I: PrimInt + Unsigned = usize> { /* private fields */ }Expand description
Constant capacity key-addressed LRU cache.
Generics:
K. Type of key.Ordis used for lookup and to address entries.V. Type of value.CAP. Capacity of the cache.I. Type of the index used. Must be an unsigned primitive type with bitwidth <=usize’s bitwidth.
Implementations§
Source§impl<K, V, const CAP: usize, I: PrimInt + Unsigned> ConstLru<K, V, CAP, I>
impl<K, V, const CAP: usize, I: PrimInt + Unsigned> ConstLru<K, V, CAP, I>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty ConstLru on the stack
panics if
CAP > I::MAXI::MAX > usize::MAX
WARNING: this might result in runtime stack overflow errors for large CAP.
Use Self::init_at_alloc to initialize larger variants at preallocated memory
Sourcepub unsafe fn init_at_alloc(ptr: *mut Self)
pub unsafe fn init_at_alloc(ptr: *mut Self)
Initializes the ConstLru at a region of allocated memory
§Safety
ptr must point to uninitialized memory, since init()
overwrites the data at ptr
panics if
CAP > I::MAXI::MAX > usize::MAX
Example:
use const_lru::ConstLru;
use std::alloc::{alloc, Layout};
let layout = Layout::new::<ConstLru<u32, u16, 1_000, u16>>();
let container: Box<ConstLru<u32, u16, 1_000, u16>> = unsafe {
let ptr = alloc(layout) as *mut ConstLru<u32, u16, 1_000, u16>;
ConstLru::init_at_alloc(ptr);
Box::from_raw(ptr)
};Sourcepub fn iter(&self) -> Iter<'_, K, V, CAP, I> ⓘ
pub fn iter(&self) -> Iter<'_, K, V, CAP, I> ⓘ
Creates an iterator that iterates through the keys and values of the ConstLru from most-recently-used to least-recently-used
Does not change the LRU order of the elements.
Double-ended: reversing iterates from least-recently-used to most-recently-used
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V, CAP, I> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V, CAP, I> ⓘ
Creates an iterator that iterates through the keys and mutable values of the ConstLru from most-recently-used to least-recently-used
Does not change the LRU order of the elements, even if mutated.
Double-ended: reversing iterates from least-recently-used to most-recently-used
Sourcepub fn iter_key_order(&self) -> IterKeyOrder<'_, K, V, CAP, I> ⓘ
pub fn iter_key_order(&self) -> IterKeyOrder<'_, K, V, CAP, I> ⓘ
Creates an iterator that iterates through the keys and values of the ConstLru in the order of its keys
Does not change the LRU order of the elements.
Double-ended: reversing iterates from descending order of its keys
Sourcepub fn iter_key_order_mut(&mut self) -> IterKeyOrderMut<'_, K, V, CAP, I> ⓘ
pub fn iter_key_order_mut(&mut self) -> IterKeyOrderMut<'_, K, V, CAP, I> ⓘ
Creates an iterator that iterates through the keys and mutable values of the ConstLru in the order of its keys
Does not change the LRU order of the elements, even if mutated.
Double-ended: reversing iterates from descending order of its keys
Source§impl<K: Ord, V, const CAP: usize, I: PrimInt + Unsigned> ConstLru<K, V, CAP, I>
impl<K: Ord, V, const CAP: usize, I: PrimInt + Unsigned> ConstLru<K, V, CAP, I>
Sourcepub fn insert(&mut self, k: K, v: V) -> Option<InsertReplaced<K, V>>
pub fn insert(&mut self, k: K, v: V) -> Option<InsertReplaced<K, V>>
Inserts a key-value pair into the map. The entry is moved to the most-recently-used slot
If CAP == 0, None is returned.
If the map did not have this key present and is not full, None is returned.
If the map did have this key present, the value is updated, and the old value is returned in a InsertReplaced::OldValue.
The key is not updated, though; this matters for types that can be == without being identical.
If the map is full, the least-recently used key-value pair is evicted and returned in a InsertReplaced::LruEvicted.
Sourcepub fn remove<Q: Ord + ?Sized>(&mut self, k: &Q) -> Option<V>where
K: Borrow<Q>,
pub fn remove<Q: Ord + ?Sized>(&mut self, k: &Q) -> Option<V>where
K: Borrow<Q>,
Removes a key from the ConstLru, returning the value at the key if the key was previously in the ConstLru.
Sourcepub fn get<Q: Ord + ?Sized>(&mut self, k: &Q) -> Option<&V>where
K: Borrow<Q>,
pub fn get<Q: Ord + ?Sized>(&mut self, k: &Q) -> Option<&V>where
K: Borrow<Q>,
Returns a reference to the value corresponding to the key and moves entry to most-recently-used slot.
To not update to most-recently-used, use Self::get_untouched
Sourcepub fn get_mut<Q: Ord + ?Sized>(&mut self, k: &Q) -> Option<&mut V>where
K: Borrow<Q>,
pub fn get_mut<Q: Ord + ?Sized>(&mut self, k: &Q) -> Option<&mut V>where
K: Borrow<Q>,
Returns a mutable reference to the value corresponding to the key and moves entry to most-recently-used slot.
To not update to most-recently-used, use Self::get_mut_untouched
Sourcepub fn get_untouched<Q: Ord + ?Sized>(&self, k: &Q) -> Option<&V>where
K: Borrow<Q>,
pub fn get_untouched<Q: Ord + ?Sized>(&self, k: &Q) -> Option<&V>where
K: Borrow<Q>,
Returns a reference to the value corresponding to the key without updating the entry to most-recently-used slot
To update to most-recently-used, use Self::get
Sourcepub fn get_mut_untouched<Q: Ord + ?Sized>(&mut self, k: &Q) -> Option<&mut V>where
K: Borrow<Q>,
pub fn get_mut_untouched<Q: Ord + ?Sized>(&mut self, k: &Q) -> Option<&mut V>where
K: Borrow<Q>,
Returns a mutable reference to the value corresponding to the key without updating the entry to most-recently-used slot
To update to most-recently-used, use Self::get_mut
Trait Implementations§
Source§impl<K: Clone, V: Clone, const CAP: usize, I: PrimInt + Unsigned> Clone for ConstLru<K, V, CAP, I>
WARNING: this might result in runtime stack overflow errors for large CAP.
To clone a large ConstLru, use ConstLru::clone_to_alloc
impl<K: Clone, V: Clone, const CAP: usize, I: PrimInt + Unsigned> Clone for ConstLru<K, V, CAP, I>
WARNING: this might result in runtime stack overflow errors for large CAP.
To clone a large ConstLru, use ConstLru::clone_to_alloc
Source§impl<K: Debug, V: Debug, const CAP: usize, I: Debug + PrimInt + Unsigned> Debug for ConstLru<K, V, CAP, I>
impl<K: Debug, V: Debug, const CAP: usize, I: Debug + PrimInt + Unsigned> Debug for ConstLru<K, V, CAP, I>
Source§impl<K: Ord, V, const CAP: usize, I: PrimInt + Unsigned> TryFrom<[(K, V); CAP]> for ConstLru<K, V, CAP, I>
Creates a full ConstLru cache from an entries array.
impl<K: Ord, V, const CAP: usize, I: PrimInt + Unsigned> TryFrom<[(K, V); CAP]> for ConstLru<K, V, CAP, I>
Creates a full ConstLru cache from an entries array.
Assumes entries is in MRU -> LRU order.
Returns error if duplicate keys found.
WARNING: this might result in runtime stack overflow errors for large CAP.