Skip to main content

LruMap

Struct LruMap 

Source
pub struct LruMap<K, V, S = DefaultHashBuilder> { /* private fields */ }
Expand description

A hash map holding at most max_length entries that evicts the least recently used entry.

get and the insert methods mark an entry as most recently used, the peek methods don’t.

Implementations§

Source§

impl<K, V> LruMap<K, V>

Source

pub fn new(max_length: u32) -> Self

Creates an empty map that holds at most max_length entries, using the default hasher. Does not allocate.

Source§

impl<K, V, S> LruMap<K, V, S>

Source

pub const fn with_hasher(max_length: u32, hasher: S) -> Self

Creates an empty map that holds at most max_length entries, using hasher to hash keys. Does not allocate.

Source

pub const fn max_length(&self) -> u32

Returns the maximum number of entries.

Source

pub const fn len(&self) -> usize

Returns the number of entries.

Source

pub const fn is_empty(&self) -> bool

Returns true if the map holds no entries.

Source

pub fn memory_usage(&self) -> usize

Returns the number of heap bytes allocated by the map, excluding heap memory owned by keys and values.

Source

pub fn peek_oldest(&self) -> Option<(&K, &V)>

Returns the least recently used entry without changing its position.

Source

pub fn peek_newest(&self) -> Option<(&K, &V)>

Returns the most recently used entry without changing its position.

Source

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

Iterates from the most to the least recently used entry.

Source

pub fn clear(&mut self)

Removes all entries, keeping the allocated memory.

Source§

impl<K, V, S> LruMap<K, V, S>
where K: Hash + Eq, S: BuildHasher,

Source

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

Returns the value for key and marks it as the most recently used.

Source

pub fn peek<Q>(&self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns the value for key without changing its position.

Source

pub fn peek_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a mutable reference to the value for key without changing its position.

Source

pub fn contains<Q>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns true if the map contains key, without changing its position.

Source

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

Inserts value for key, or replaces the current value, and marks key as the most recently used.

Returns the least recently used entry if it was evicted to make room. If max_length is zero, nothing is stored and the given entry is returned instead.

Source

pub fn get_or_insert(&mut self, key: K, f: impl FnOnce() -> V) -> Option<&mut V>

Returns the value for key and marks it as the most recently used, first inserting the value returned by f if key is absent.

Returns None only if max_length is zero.

Source

pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes key and returns its value.

Source

pub fn pop_oldest(&mut self) -> Option<(K, V)>

Removes and returns the least recently used entry.

Trait Implementations§

Source§

impl<K, V, S> Debug for LruMap<K, V, S>

Source§

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

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

impl<K, V, S: Default> Default for LruMap<K, V, S>

Source§

fn default() -> Self

Returns an empty map with max_length zero, which stores nothing.

Source§

impl<K, V, S> Drop for LruMap<K, V, S>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics #130494)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<'a, K, V, S> IntoIterator for &'a LruMap<K, V, S>

Source§

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

The type of the elements being iterated over.
Source§

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

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<K, V, S> Freeze for LruMap<K, V, S>
where Vec<Node<K, V>>: Freeze, S: Freeze,

§

impl<K, V, S> RefUnwindSafe for LruMap<K, V, S>
where Vec<Node<K, V>>: RefUnwindSafe, S: RefUnwindSafe,

§

impl<K, V, S> Send for LruMap<K, V, S>
where Vec<Node<K, V>>: Send, S: Send,

§

impl<K, V, S> Sync for LruMap<K, V, S>
where Vec<Node<K, V>>: Sync, S: Sync,

§

impl<K, V, S> Unpin for LruMap<K, V, S>
where Vec<Node<K, V>>: Unpin, S: Unpin,

§

impl<K, V, S> UnsafeUnpin for LruMap<K, V, S>
where Vec<Node<K, V>>: UnsafeUnpin, S: UnsafeUnpin,

§

impl<K, V, S> UnwindSafe for LruMap<K, V, S>
where Vec<Node<K, V>>: UnwindSafe, S: 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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.
§

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

§

fn vzip(self) -> V

Layout§

Note: Unable to compute type layout, possibly due to this type having generic parameters. Layout can only be computed for concrete, fully-instantiated types.