pub struct SmartEntry<'a, K, V, R = CanRemove> { /* private fields */ }
Expand description
An entry in the cache that can be used for for reading or writing, only updating the LRU order when the value is accessed mutably.
The Deref
and DerefMut
implementations allow for easy access to the value,
without or with updating the LRU order, respectively. Accessing the value mutably
via DerefMut
will update the LRU order.
See SmartEntry::peek
and SmartEntry::get
for more information.
Implementations§
Source§impl<'a, K, V, R> SmartEntry<'a, K, V, R>
impl<'a, K, V, R> SmartEntry<'a, K, V, R>
Sourcepub fn into_mut(self) -> (&'a K, &'a mut V)
pub fn into_mut(self) -> (&'a K, &'a mut V)
With the lifetime of the LRUCache
itself,
access the key-value pair and update the LRU order.
It’s not possible to have multiple SmartEntry
instances, nor
more than one mutable reference to the cache value at a time,
but this allows the reference to outlive the SmartEntry
itself.
See also SmartEntry::get
.
§Error Example
With the returned references being tied to the LRU cache itself,
it’s not possible to borrow it mutably more than once at a time,
even after dropping the SmartEntry
.
let mut entry = lru.smart_get(&1).unwrap();
let (key, value) = entry.into_mut();
// error[E0499]: cannot borrow `lru` as mutable more than once at a tim
let another_entry = lru.smart_get(&1).unwrap();
drop((key, value)); // pretend to use the value later
Sourcepub fn into_ref(self) -> (&'a K, &'a V)
pub fn into_ref(self) -> (&'a K, &'a V)
With the lifetime of the LRUCache
itself,
access the key-value pair without updating the LRU order.
See also SmartEntry::peek
and SmartEntry::into_mut
.
Source§impl<K, V, R> SmartEntry<'_, K, V, R>
impl<K, V, R> SmartEntry<'_, K, V, R>
Sourcepub fn peek(&self) -> (&K, &V)
pub fn peek(&self) -> (&K, &V)
Access the key-value pair immutably, without updating the LRU order.
See also SmartEntry::into_ref
.
Sourcepub fn peek_value(&self) -> &V
pub fn peek_value(&self) -> &V
Access the value immutably, without updating the LRU order.
This is the same as SmartEntry::deref
.
Sourcepub fn get(&mut self) -> (&K, &mut V)
pub fn get(&mut self) -> (&K, &mut V)
Access the key-value pair, and update the LRU order.
The LRU order is updated every time this method is called, as it is assumed that the caller is actively using the value.
See also SmartEntry::into_mut
.
Sourcepub fn get_value(&mut self) -> &mut V
pub fn get_value(&mut self) -> &mut V
Access the value mutably, and update the LRU order.
This LRU order is updated every time this method is called, as it is assumed that the caller is actively using the value.
The DerefMut
implementation invokes this method to access the value,
updating the LRU order in the process.
Sourcepub fn is_most_recent(&self) -> bool
pub fn is_most_recent(&self) -> bool
Returns true
if this entry is the most recently used in the cache.
Source§impl<K, V> SmartEntry<'_, K, V, CanRemove>
impl<K, V> SmartEntry<'_, K, V, CanRemove>
Sourcepub fn remove(self) -> (K, V)
pub fn remove(self) -> (K, V)
Removes the key-value pair from the cache, and returns the key and value.
This cannot be called on every SmartEntry
. For example, LRUCache::smart_iter
does not allow for removal of entries, so the SmartEntry
it yields will not have
this method available. It can, however, still update the LRU order on mutable access.
Consider using LRUCache::retain
to remove entries based on a predicate.