LruCache

Struct LruCache 

Source
pub struct LruCache<K, V> { /* private fields */ }
Expand description

An LRU (least-recently-used) cache.

Implemented by maintaining a doubly-linked list of cache entries. On access, entries are moved to the head of list. Once the cache reaches capacity, entries at the back of the list will be evicted first to make room for newer entries.

Implementations§

Source§

impl<K, V> LruCache<K, V>
where K: Eq + Hash,

Source

pub fn new(capacity: usize) -> Self

Creates a new LRU cache with the given capacity.

Source

pub fn capacity(&self) -> usize

The capacity of the cache.

Source

pub fn len(&self) -> usize

The length of the map.

Source

pub fn put(&mut self, key: K, value: V) -> Option<V>

Puts a key-value pair into the cache.

If the key already exists in the cache, it is updated and the old value is returned. Otherwise, None is returned.

Source

pub fn push(&mut self, key: K, value: V) -> Option<(K, V)>

Pushes a key-value pair into the cache.

If the key already exists in the cache or another entry is removed (due to capacity), then the old key-value pair is returned. Otherwise, returns None.

Source

pub fn contains_key<'a, L>(&'a self, key: &L) -> bool
where K: Borrow<L>, L: Eq + Hash + ?Sized,

Checks if the given key is contained in the cache.

Source

pub fn get<'a, L>(&'a mut self, key: &L) -> Option<&'a V>
where K: Borrow<L>, L: Eq + Hash + ?Sized,

Returns a reference to the value associated with the given key.

Moves the key to the head of the LRU list if it exists. Otherwise, returns None.

Source

pub fn get_mut<'a, L>(&'a mut self, key: &L) -> Option<&'a mut V>
where K: Borrow<L>, L: Eq + Hash + ?Sized,

Returns a mutable reference to the value associated with the given key.

Moves the key to the head of the LRU list if it exists. Otherwise, returns None.

Source

pub fn iter(&self) -> Iter<'_, K, V>

Returns an iterator visiting all entries in most-recently used order.

Source

pub fn iter_mut(&self) -> IterMut<'_, K, V>

Returns an iterator visiting all entries in most-recently used order, with a mutable reference to the value.

Trait Implementations§

Source§

impl<K, V> Clone for LruCache<K, V>
where K: PartialEq + Eq + Hash + Clone, V: Clone,

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, V> Drop for LruCache<K, V>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a, K, V> IntoIterator for &'a LruCache<K, V>
where K: Eq + Hash,

Source§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, K, V>

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<'a, K, V> IntoIterator for &'a mut LruCache<K, V>
where K: Eq + Hash,

Source§

type Item = (&'a K, &'a mut V)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, K, V>

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: Send, V: Send> Send for LruCache<K, V>

Source§

impl<K: Sync, V: Sync> Sync for LruCache<K, V>

Auto Trait Implementations§

§

impl<K, V> Freeze for LruCache<K, V>

§

impl<K, V> RefUnwindSafe for LruCache<K, V>

§

impl<K, V> Unpin for LruCache<K, V>

§

impl<K, V> UnwindSafe for LruCache<K, V>

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<'a, 'b, T> UnsafelyDetachBorrow<'a, 'b, T> for T

Source§

unsafe fn unsafely_detach_borrow(&'a self) -> &'b T

Unsafely detaches an immutable borrow, attaching a new lifetime. Read more
Source§

impl<'a, 'b, T> UnsafelyDetachBorrowMut<'a, 'b, T> for T

Source§

unsafe fn unsafely_detach_borrow_mut(&'a mut self) -> &'b mut T

Unsafely detaches a mutable borrow, attaching a new lifetime. Read more
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<'a, T> Captures<'a> for T
where T: ?Sized,