LRUCache

Struct LRUCache 

Source
pub struct LRUCache<K: Ord + Clone, V, const MAX_SIZE: usize> {
    inner: BTreeMap<K, Node<K, V>>,
    head: Option<K>,
    tail: Option<K>,
}
Expand description

An Least Recently Used Cache with capacity MAX_SIZE.

Fields§

§inner: BTreeMap<K, Node<K, V>>§head: Option<K>§tail: Option<K>

Implementations§

Source§

impl<K: Ord + Clone, V, const MAX_SIZE: usize> LRUCache<K, V, MAX_SIZE>

Source

fn attach(&mut self, k: K) -> &mut Node<K, V>

Source

fn detach(&mut self, prev: Option<K>, next: Option<K>)

Source

pub const fn new() -> Self

Makes a new, empty LRUCache.

Does not allocate anything on its own.

Source

pub fn get(&mut self, k: K) -> Option<&mut V>

Returns a mutable reference to the value corresponding to the key and update the last access time.

Source

pub fn get_or_insert_with<E>( &mut self, k: K, f: impl FnOnce() -> Result<V, E>, ) -> Result<&mut V, E>

Inserts the value computed with f into the LRUCache if it is not present, then returns a reference to the value in the LRUCache.

Source

fn __put(&mut self, k: K, v: V) -> &mut Node<K, V>

Source

pub fn put(&mut self, k: K, v: V)

Inserts a key-value pair into the LRUCache.

If the map did have this key present, the value is updated. The key is not updated, though; this matters for types that can be == without being identical.

If the cache size is overflowed after insertion, evict the oldest accessed entry.

Source

pub fn remove(&mut self, k: &K) -> Option<V>

Removes a key from the LRUCache, returning the stored value if the key was previously in the LRUCache.

The key may be any borrowed form of the LRUCache’s key type, but the ordering on the borrowed form must match the ordering on the key type.

Source

pub fn retain(&mut self, f: impl FnMut(&K, &mut V) -> bool)

Retains only the elements specified by the predicate. In other words, remove all pairs (k, v) for which f(&k, &mut v) returns false.

Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)>

Iterates over the key-value pairs in the LRUCache.

Trait Implementations§

Source§

impl<K: Ord + Clone, V, const MAX_SIZE: usize> Default for LRUCache<K, V, MAX_SIZE>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<K, V, const MAX_SIZE: usize> Freeze for LRUCache<K, V, MAX_SIZE>
where K: Freeze,

§

impl<K, V, const MAX_SIZE: usize> RefUnwindSafe for LRUCache<K, V, MAX_SIZE>
where K: RefUnwindSafe, V: RefUnwindSafe,

§

impl<K, V, const MAX_SIZE: usize> Send for LRUCache<K, V, MAX_SIZE>
where K: Send, V: Send,

§

impl<K, V, const MAX_SIZE: usize> Sync for LRUCache<K, V, MAX_SIZE>
where K: Sync, V: Sync,

§

impl<K, V, const MAX_SIZE: usize> Unpin for LRUCache<K, V, MAX_SIZE>
where K: Unpin,

§

impl<K, V, const MAX_SIZE: usize> UnwindSafe for LRUCache<K, V, MAX_SIZE>
where K: RefUnwindSafe + UnwindSafe, V: RefUnwindSafe,

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.