Skip to main content

Entry

Struct Entry 

Source
pub struct Entry<'c, K: Hash + Eq, V, P: Policy<V>> { /* private fields */ }
Expand description

A smart reference to a cached value that tracks modifications.

The Entry provides transparent access to the underlying value through Deref and DerefMut traits.

§Behavior

§Automatic Eviction Order Updates

When an Entry is dropped:

  • If the value was modified during the borrow (via DerefMut, AsMut, or value_mut()), the cache’s eviction order is updated
  • If the value was never modified, the eviction order remains unchanged

§Modification Tracking

The Entry tracks modifications through several mechanisms:

  • DerefMut: Any mutable dereference (*entry = new_value) marks as dirty
  • AsMut: Calling entry.as_mut() marks as dirty
  • value_mut(): Calling entry.value_mut() marks as dirty
  • Read-only access: Deref, AsRef, value(), and key() do not mark as dirty

§Performance

  • Lookup: O(1) average (inherits from hash map performance)
  • No modification: Zero additional overhead beyond the initial lookup
  • With modification: O(1) cache reordering when dropped

§Examples

§Read-only Access (No Eviction Order Change)

use std::num::NonZeroUsize;

use evictor::Lru;

let mut cache = Lru::new(NonZeroUsize::new(3).unwrap());
cache.insert("A", vec![1, 2, 3]);
cache.insert("B", vec![4, 5, 6]);

let original_order: Vec<_> = cache.iter().map(|(k, _)| *k).collect();

// Read-only access through Entry
if let Some(entry) = cache.peek_mut(&"A") {
    let _len = entry.len(); // Read-only
    let _first = entry.first(); // Read-only
    let _key = entry.key(); // Read-only
    let _value_ref = entry.value(); // Read-only
} // Entry dropped here - no modifications, so no cache update

let new_order: Vec<_> = cache.iter().map(|(k, _)| *k).collect();
assert_eq!(original_order, new_order); // Order unchanged

§Modification Triggers Cache Update

use std::num::NonZeroUsize;

use evictor::Lru;

let mut cache = Lru::new(NonZeroUsize::new(3).unwrap());
cache.insert("A", vec![1, 2, 3]);
cache.insert("B", vec![4, 5, 6]);
cache.insert("C", vec![7, 8, 9]);

// Before: "A" would be evicted first
assert_eq!(cache.tail().unwrap().0, &"A");

// Modify "A" through Entry
if let Some(mut entry) = cache.peek_mut(&"A") {
    entry.push(4); // Modification via DerefMut
} // Entry dropped here - modification detected, cache updated

// After: "A" is now most recently used, "B" would be evicted first
assert_eq!(cache.tail().unwrap().0, &"B");

§Different Modification Methods

use std::num::NonZeroUsize;

use evictor::Lru;

let mut cache = Lru::new(NonZeroUsize::new(2).unwrap());
cache.insert("key", String::from("hello"));

// Method 1: Direct assignment via DerefMut
if let Some(mut entry) = cache.peek_mut(&"key") {
    *entry = String::from("world"); // Triggers modification tracking
}

// Method 2: Mutable method call via DerefMut
if let Some(mut entry) = cache.peek_mut(&"key") {
    entry.push_str(" rust"); // Triggers modification tracking
}

// Method 3: Explicit mutable reference
if let Some(mut entry) = cache.peek_mut(&"key") {
    let value = entry.value_mut(); // Triggers modification tracking
    value.push('!');
}

// Method 4: AsMut trait
if let Some(mut entry) = cache.peek_mut(&"key") {
    entry.as_mut().push('?'); // Triggers modification tracking
}

Implementations§

Source§

impl<K: Hash + Eq, V, P: Policy<V>> Entry<'_, K, V, P>

Source

pub fn key(&self) -> &K

Returns a reference to the key for this cache entry.

This method provides read-only access to the key associated with the cached value. The key reference is valid for the lifetime of the Entry and accessing it does not affect the cache’s eviction order or mark the entry as modified.

§Returns

A reference to the key of type &K.

§Examples
use std::num::NonZeroUsize;

use evictor::Lru;

let mut cache = Lru::new(NonZeroUsize::new(3).unwrap());
cache.insert("hello", vec![1, 2, 3]);

if let Some(entry) = cache.peek_mut(&"hello") {
    assert_eq!(entry.key(), &"hello");
    // Key access doesn't mark entry as dirty
}
Source

pub fn value(&self) -> &V

Returns an immutable reference to the cached value.

This method provides read-only access to the cached value without affecting the cache’s eviction order or marking the entry as modified. It’s equivalent to using the Deref trait (e.g., &*entry) or the AsRef trait.

For mutable access that tracks modifications, use value_mut() instead.

§Returns

An immutable reference to the cached value of type &V.

§Examples
use std::num::NonZeroUsize;

use evictor::Lru;

let mut cache = Lru::new(NonZeroUsize::new(3).unwrap());
cache.insert("key", vec![1, 2, 3]);

if let Some(entry) = cache.peek_mut(&"key") {
    let value_ref = entry.value();
    assert_eq!(value_ref, &vec![1, 2, 3]);

    // These are equivalent ways to access the value:
    assert_eq!(entry.value(), &*entry);
    assert_eq!(entry.value(), entry.as_ref());
}
Source

pub fn value_mut(&mut self) -> &mut V

Returns a mutable reference to the cached value and marks the entry as modified.

This method provides mutable access to the cached value and automatically marks the entry as “dirty”, indicating that it has been modified. When the Entry is dropped, the cache’s eviction order will be updated to reflect this modification according to the cache policy.

Important: Unlike DerefMut which only marks as dirty when actually dereferenced mutably, this method always marks the entry as dirty, even if you don’t actually modify the returned reference.

For read-only access that doesn’t affect eviction order, use value() instead.

§Returns

A mutable reference to the cached value of type &mut V.

§Behavior
  • Immediately marks the entry as modified (dirty flag set to true)
  • When the Entry is dropped, the cache’s eviction order will be updated
  • The specific eviction order update depends on the cache policy (Lru, Mru, Lfu)
§Examples
use std::num::NonZeroUsize;

use evictor::Lru;

let mut cache = Lru::new(NonZeroUsize::new(3).unwrap());
cache.insert("A", vec![1, 2, 3]);
cache.insert("B", vec![4, 5, 6]);
cache.insert("C", vec![7, 8, 9]);

// Before modification: "A" would be evicted first (least recently used)
assert_eq!(cache.tail().unwrap().0, &"A");

if let Some(mut entry) = cache.peek_mut(&"A") {
    let value = entry.value_mut(); // Marks as dirty immediately
    value.push(4); // Modify the value
} // Entry dropped here - cache eviction order updated

// After modification: "A" is now most recently used
assert_eq!(cache.tail().unwrap().0, &"B");
§Comparison with Other Access Methods
use std::num::NonZeroUsize;

use evictor::Lru;

let mut cache = Lru::new(NonZeroUsize::new(2).unwrap());
cache.insert("key", String::from("hello"));

if let Some(mut entry) = cache.peek_mut(&"key") {
    // Method 1: value_mut() - always marks as dirty
    let value = entry.value_mut();
    value.push_str(" world");

    // Method 2: DerefMut - only marks dirty when actually used mutably
    entry.push_str("!"); // Equivalent to (*entry).push_str("!")

    // Method 3: AsMut trait
    entry.as_mut().push_str("?");
}

Trait Implementations§

Source§

impl<K: Hash + Eq, V, P: Policy<V>> AsMut<V> for Entry<'_, K, V, P>

Source§

fn as_mut(&mut self) -> &mut V

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<K: Hash + Eq, V, P: Policy<V>> AsRef<V> for Entry<'_, K, V, P>

Source§

fn as_ref(&self) -> &V

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<K: Hash + Eq, V, P: Policy<V>> Deref for Entry<'_, K, V, P>

Source§

type Target = V

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<K: Hash + Eq, V, P: Policy<V>> DerefMut for Entry<'_, K, V, P>

Source§

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

Mutably dereferences the value.
Source§

impl<K: Hash + Eq, V, P: Policy<V>> Drop for Entry<'_, K, V, P>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'c, K, V, P> Freeze for Entry<'c, K, V, P>

§

impl<'c, K, V, P> !RefUnwindSafe for Entry<'c, K, V, P>

§

impl<'c, K, V, P> !Send for Entry<'c, K, V, P>

§

impl<'c, K, V, P> !Sync for Entry<'c, K, V, P>

§

impl<'c, K, V, P> Unpin for Entry<'c, K, V, P>

§

impl<'c, K, V, P> UnsafeUnpin for Entry<'c, K, V, P>

§

impl<'c, K, V, P> !UnwindSafe for Entry<'c, K, V, P>

Blanket Implementations§

Source§

impl<R> TryRngCore for R
where R: TryRng,

Source§

type Error = <R as TryRng>::Error

👎Deprecated since 0.10.0:

use TryRng instead

Error type.
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<R> Rng for R
where R: TryRng<Error = Infallible> + ?Sized,

Source§

fn next_u32(&mut self) -> u32

Return the next random u32.
Source§

fn next_u64(&mut self) -> u64

Return the next random u64.
Source§

fn fill_bytes(&mut self, dst: &mut [u8])

Fill dest with random data. Read more
Source§

impl<R> RngExt for R
where R: Rng + ?Sized,

Source§

fn random<T>(&mut self) -> T

Return a random value via the StandardUniform distribution. Read more
Source§

fn random_iter<T>(self) -> Iter<StandardUniform, Self, T>

Return an iterator over random variates Read more
Source§

fn random_range<T, R>(&mut self, range: R) -> T
where T: SampleUniform, R: SampleRange<T>,

Generate a random value in the given range. Read more
Source§

fn random_bool(&mut self, p: f64) -> bool

Return a bool with a probability p of being true. Read more
Source§

fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool

Return a bool with a probability of numerator/denominator of being true. Read more
Source§

fn sample<T, D>(&mut self, distr: D) -> T
where D: Distribution<T>,

Sample a new value, using the given distribution. Read more
Source§

fn sample_iter<T, D>(self, distr: D) -> Iter<D, Self, T>
where D: Distribution<T>, Self: Sized,

Create an iterator that generates values using the given distribution. Read more
Source§

fn fill<T>(&mut self, dest: &mut [T])
where T: Fill,

Fill any type implementing Fill with random data 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<R> TryRng for R
where R: DerefMut, <R as Deref>::Target: TryRng,

Source§

type Error = <<R as Deref>::Target as TryRng>::Error

The type returned in the event of a RNG error. Read more
Source§

fn try_next_u32(&mut self) -> Result<u32, <R as TryRng>::Error>

Return the next random u32.
Source§

fn try_next_u64(&mut self) -> Result<u64, <R as TryRng>::Error>

Return the next random u64.
Source§

fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), <R as TryRng>::Error>

Fill dst entirely with random data.
Source§

impl<R> CryptoRng for R
where R: TryCryptoRng<Error = Infallible> + ?Sized,

Source§

impl<R> RngCore for R
where R: Rng,

Source§

impl<R> TryCryptoRng for R
where R: DerefMut, <R as Deref>::Target: TryCryptoRng,