intrusive_lru_cache

Struct SmartEntry

Source
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>

Source

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
Source

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>

Source

pub fn key(&self) -> &K

Access the key only, without updating the LRU order.

Source

pub fn peek(&self) -> (&K, &V)

Access the key-value pair immutably, without updating the LRU order.

See also SmartEntry::into_ref.

Source

pub fn peek_value(&self) -> &V

Access the value immutably, without updating the LRU order.

This is the same as SmartEntry::deref.

Source

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.

Source

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.

Source

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>

Source

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.

Trait Implementations§

Source§

impl<K, V, R> Deref for SmartEntry<'_, K, V, R>

Source§

fn deref(&self) -> &Self::Target

Dereferences the value, without updating the LRU order.

Source§

type Target = V

The resulting type after dereferencing.
Source§

impl<K, V, R> DerefMut for SmartEntry<'_, K, V, R>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value, and updates the LRU order.

Auto Trait Implementations§

§

impl<'a, K, V, R> Freeze for SmartEntry<'a, K, V, R>

§

impl<'a, K, V, R = CanRemove> !RefUnwindSafe for SmartEntry<'a, K, V, R>

§

impl<'a, K, V, R = CanRemove> !Send for SmartEntry<'a, K, V, R>

§

impl<'a, K, V, R = CanRemove> !Sync for SmartEntry<'a, K, V, R>

§

impl<'a, K, V, R> Unpin for SmartEntry<'a, K, V, R>
where R: Unpin,

§

impl<'a, K, V, R = CanRemove> !UnwindSafe for SmartEntry<'a, K, V, R>

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> 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.