Skip to main content

SlotCache

Struct SlotCache 

Source
pub struct SlotCache<const N: usize> { /* private fields */ }
Available on crate feature fs only.
Expand description

Bounded LRU image cache backed by a fixed-size array.

§Const-generic parameter

N is the number of slots. Choose a value appropriate to the platform’s available RAM: on the STM32H747I-DISCO a value of 8–16 is typical.

§Eviction policy

On insertion when all N slots are occupied, the slot with the smallest (oldest) ts value is evicted. On a cache hit, the matching slot’s ts is updated to the current counter value and the counter increments.

§Determinism

Given a fixed sequence of get/insert calls starting from a freshly constructed SlotCache, the eviction order is fully deterministic. Tests that require reproducible cache behaviour should create a fresh SlotCache::new() and drive calls in a known order.

§Handle numbering

CacheHandle values are assigned sequentially starting at 1 (0 is reserved). Within a session handles are never reused.

Implementations§

Source§

impl<const N: usize> SlotCache<N>

Source

pub const fn new() -> Self

Create an empty cache with all slots vacant.

Source

pub fn get(&mut self, handle: CacheHandle) -> Option<&ImageDescriptor<'static>>

Look up handle in the cache.

On a hit the slot’s timestamp is updated (LRU touch) and a reference to the cached ImageDescriptor is returned. On a miss None is returned.

Source

pub fn insert(&mut self, descriptor: ImageDescriptor<'static>) -> CacheHandle

Insert descriptor into the cache and return its CacheHandle.

If all slots are occupied, the least-recently-used entry is evicted first. The returned handle is unique within the session.

Source

pub fn evict(&mut self, handle: CacheHandle)

Evict the entry identified by handle if it is present.

After eviction the decoded pixels are dropped. The next resolve_image call for the same asset will re-decode from the source.

Source

pub fn len(&self) -> usize

Return the number of occupied slots.

Source

pub fn is_empty(&self) -> bool

Return true if no slots are occupied.

Trait Implementations§

Source§

impl<const N: usize> Default for SlotCache<N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<const N: usize> ImageCache<'static> for SlotCache<N>

Source§

fn get(&self, _handle: CacheHandle) -> Option<&ImageDescriptor<'static>>

Return the descriptor associated with handle, if it is still cached.
Source§

fn put(&mut self, descriptor: ImageDescriptor<'static>) -> CacheHandle

Register descriptor with the cache and return its handle.

Auto Trait Implementations§

§

impl<const N: usize> Freeze for SlotCache<N>

§

impl<const N: usize> RefUnwindSafe for SlotCache<N>

§

impl<const N: usize> Send for SlotCache<N>

§

impl<const N: usize> Sync for SlotCache<N>

§

impl<const N: usize> Unpin for SlotCache<N>

§

impl<const N: usize> UnsafeUnpin for SlotCache<N>

§

impl<const N: usize> UnwindSafe for SlotCache<N>

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> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
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<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
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<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.