pub struct SlotCache<const N: usize> { /* private fields */ }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>
impl<const N: usize> SlotCache<N>
Sourcepub fn get(&mut self, handle: CacheHandle) -> Option<&ImageDescriptor<'static>>
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.
Sourcepub fn insert(&mut self, descriptor: ImageDescriptor<'static>) -> CacheHandle
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.
Sourcepub fn evict(&mut self, handle: CacheHandle)
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.
Trait Implementations§
Source§impl<const N: usize> ImageCache<'static> for SlotCache<N>
impl<const N: usize> ImageCache<'static> for SlotCache<N>
Source§fn get(&self, _handle: CacheHandle) -> Option<&ImageDescriptor<'static>>
fn get(&self, _handle: CacheHandle) -> Option<&ImageDescriptor<'static>>
handle, if it is still cached.Source§fn put(&mut self, descriptor: ImageDescriptor<'static>) -> CacheHandle
fn put(&mut self, descriptor: ImageDescriptor<'static>) -> CacheHandle
descriptor with the cache and return its handle.