ConstLru

Struct ConstLru 

Source
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. Ord is 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>

Source

pub fn new() -> Self

Creates a new empty ConstLru on the stack

panics if

  • CAP > I::MAX
  • I::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

Source

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::MAX
  • I::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)
};
Source

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

Source

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

Source

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

Source

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

pub fn clear(&mut self)

Clears the ConstLru, removing all key-value pairs.

Source

pub fn cap(&self) -> I

Returns the maximum number of elements this ConstLru can hold

Source

pub fn is_empty(&self) -> bool

Returns true if the ConstLru contains no elements.

Source

pub fn is_full(&self) -> bool

Returns true if the ConstLru has reached max capacity.

Source

pub fn len(&self) -> I

Returns the number of elements in the ConstLru.

Source§

impl<K: Ord, V, const CAP: usize, I: PrimInt + Unsigned> ConstLru<K, V, CAP, I>

Source

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.

Source

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.

Source

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

Source

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

Source

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

Source

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

Source

pub fn entry(&mut self, k: K) -> Entry<'_, K, V, CAP, I>

Gets the given key’s corresponding entry in the map for in-place manipulation.

panics if CAP == 0

Source§

impl<K: Clone, V: Clone, const CAP: usize, I: PrimInt + Unsigned> ConstLru<K, V, CAP, I>

Source

pub unsafe fn clone_to_alloc(&self, dst: *mut Self)

Clones the ConstLru to a region of allocated memory

§Safety

dst must point to uninitialized memory, since this overwrites the data at dst

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

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug, V: Debug, const CAP: usize, I: Debug + PrimInt + Unsigned> Debug for ConstLru<K, V, CAP, I>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K, V, const CAP: usize, I: PrimInt + Unsigned> Default for ConstLru<K, V, CAP, I>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<K, V, const CAP: usize, I: PrimInt + Unsigned> Drop for ConstLru<K, V, CAP, I>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<K, V, const CAP: usize, I: PrimInt + Unsigned> IntoIterator for ConstLru<K, V, CAP, I>

Source§

type Item = <IntoIter<K, V, CAP, I> as Iterator>::Item

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V, CAP, I>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
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.

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.

Source§

type Error = DuplicateKeysError<K>

The type returned in the event of a conversion error.
Source§

fn try_from(entries: [(K, V); CAP]) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<K, V, const CAP: usize, I> Freeze for ConstLru<K, V, CAP, I>
where I: Freeze, K: Freeze, V: Freeze,

§

impl<K, V, const CAP: usize, I> RefUnwindSafe for ConstLru<K, V, CAP, I>

§

impl<K, V, const CAP: usize, I> Send for ConstLru<K, V, CAP, I>
where I: Send, K: Send, V: Send,

§

impl<K, V, const CAP: usize, I> Sync for ConstLru<K, V, CAP, I>
where I: Sync, K: Sync, V: Sync,

§

impl<K, V, const CAP: usize, I> Unpin for ConstLru<K, V, CAP, I>
where I: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, const CAP: usize, I> UnwindSafe for ConstLru<K, V, CAP, I>
where I: UnwindSafe, K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.