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, S> LruMap<K, V, S>
impl<K, V, S> LruMap<K, V, S>
Sourcepub const fn with_hasher(max_length: u32, hasher: S) -> Self
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.
Sourcepub const fn max_length(&self) -> u32
pub const fn max_length(&self) -> u32
Returns the maximum number of entries.
Sourcepub fn memory_usage(&self) -> usize
pub fn memory_usage(&self) -> usize
Returns the number of heap bytes allocated by the map, excluding heap memory owned by keys and values.
Sourcepub fn peek_oldest(&self) -> Option<(&K, &V)>
pub fn peek_oldest(&self) -> Option<(&K, &V)>
Returns the least recently used entry without changing its position.
Sourcepub fn peek_newest(&self) -> Option<(&K, &V)>
pub fn peek_newest(&self) -> Option<(&K, &V)>
Returns the most recently used entry without changing its position.
Source§impl<K, V, S> LruMap<K, V, S>
impl<K, V, S> LruMap<K, V, S>
Sourcepub fn get<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get<Q>(&mut self, key: &Q) -> Option<&mut V>
Returns the value for key and marks it as the most recently used.
Sourcepub fn peek<Q>(&self, key: &Q) -> Option<&V>
pub fn peek<Q>(&self, key: &Q) -> Option<&V>
Returns the value for key without changing its position.
Sourcepub fn peek_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn peek_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
Returns a mutable reference to the value for key without changing its position.
Sourcepub fn contains<Q>(&self, key: &Q) -> bool
pub fn contains<Q>(&self, key: &Q) -> bool
Returns true if the map contains key, without changing its position.
Sourcepub fn insert(&mut self, key: K, value: V) -> Option<(K, V)>
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.
Sourcepub fn get_or_insert(&mut self, key: K, f: impl FnOnce() -> V) -> Option<&mut V>
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.
Sourcepub fn pop_oldest(&mut self) -> Option<(K, V)>
pub fn pop_oldest(&mut self) -> Option<(K, V)>
Removes and returns the least recently used entry.
Trait Implementations§
Source§impl<'a, K, V, S> IntoIterator for &'a LruMap<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a LruMap<K, V, S>
Auto Trait Implementations§
impl<K, V, S> Freeze for LruMap<K, V, S>
impl<K, V, S> RefUnwindSafe for LruMap<K, V, S>
impl<K, V, S> Send for LruMap<K, V, S>
impl<K, V, S> Sync for LruMap<K, V, S>
impl<K, V, S> Unpin for LruMap<K, V, S>
impl<K, V, S> UnsafeUnpin for LruMap<K, V, S>
impl<K, V, S> UnwindSafe for LruMap<K, V, S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
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.