Skip to main content
← PSoXide SDK guides and examples · SDK f2fec1c301f1 · All Cargo features enabled; check feature gates before use.

SlotCache

Struct SlotCache 

Source
pub struct SlotCache<V: Copy, const N: usize, const MAX_KEY: usize> { /* private fields */ }
Expand description

Keyed, fixed-capacity cache with LRU eviction and pinning.

Implementations§

Source§

impl<V: Copy, const N: usize, const MAX_KEY: usize> SlotCache<V, N, MAX_KEY>

Source

pub const fn new() -> Self

An empty cache. const so it can back a static.

Source

pub const fn capacity(&self) -> usize

Slot capacity.

Source

pub fn bump_epoch(&mut self)

Advance the LRU clock. Call once per cache cycle (e.g. per frame) so get touches and eviction comparisons share a monotonic timeline. Never returns 0, so a freshly touched slot always beats the initial last_used of an untouched one.

Source

pub const fn epoch(&self) -> u32

The current epoch (the value get stamps into last_used).

Source

pub fn slot_of(&self, key: u16) -> Option<usize>

Slot index currently mapped to key in any non-empty state, validated against the forward store (so a stale reverse entry never lies).

Source

pub fn contains_ready(&self, key: u16) -> bool

Is key resident and usable (Ready)?

Source

pub fn is_loading(&self, key: u16) -> bool

Is key reserved with a load in flight (Loading)?

Source

pub fn state_of(&self, key: u16) -> SlotState

Load state of key, or Empty if absent.

Source

pub fn get(&mut self, key: u16) -> Option<&V>

Get a Ready value, marking it most-recently-used. Returns None for an absent or still-Loading key.

Source

pub fn peek(&self, key: u16) -> Option<&V>

Like get but without touching the LRU clock.

Source

pub fn begin_load(&mut self, key: u16) -> Option<Reservation>

Reserve a slot for key and mark it Loading, evicting the least-recently-used unpinned Ready slot if the cache is full. Returns a Reservation for the caller to complete with finish_load, or None if key is out of range or every slot is pinned or already loading. If key already has a slot, a reservation for that slot is returned unchanged (no duplicate load is started).

Source

pub fn finish_load( &mut self, reservation: Reservation, value: V, ) -> Result<(), Stale>

Complete a begin_load: store the value and mark the slot Ready and most-recently-used. Returns Stale when the reservation’s key no longer owns its slot (it was evicted, and the slot may now be loading another key), and stores nothing.

Source

pub fn reserve(&mut self, key: u16) -> Option<usize>

👎Deprecated:

renamed to begin_load

Renamed to begin_load, which returns the key with the slot so a stale completion can be told from a live one.

Source

pub fn mark_ready(&mut self, slot: usize, value: V)

👎Deprecated:

renamed to finish_load, which checks the key still owns the slot

Renamed to finish_load. A bare slot index cannot tell that the slot was evicted and reused for another key since reserve, so a late completion stores its value under the wrong key; this keeps that behaviour for one stage.

Source

pub fn get_or_insert_with<F>(&mut self, key: u16, build: F) -> Option<&V>
where F: FnOnce() -> Option<V>,

Return the cached value for key, or synchronously build and insert it. Evicts LRU on a miss when full. If build returns None the reservation is released and this returns None.

Source

pub fn pin(&mut self, key: u16)

Pin key’s slot so it is never evicted. No-op if absent.

Source

pub fn unpin_all(&mut self)

Clear all pins.

Source

pub fn set_pinned(&mut self, keys: &[u16])

Replace the pin set: unpin everything, then pin each present key in keys. This is the “declare the working set” call a streaming reconcile makes each cycle.

Source

pub fn evict(&mut self, key: u16)

Evict key (any state) back to Empty. No-op if absent.

Source

pub fn evict_ready_outside(&mut self, keep: &[u16]) -> usize

Evict every Ready, unpinned slot whose key is not in keep. Used by a reconcile to drop rooms that left the desired set while protecting the pinned working set. Returns the number evicted.

Source

pub fn ready_len(&self) -> usize

Number of Ready slots.

Source

pub fn len(&self) -> usize

Number of occupied (Loading or Ready) slots.

Source

pub fn is_empty(&self) -> bool

Whether no slot is occupied.

Trait Implementations§

Source§

impl<V: Copy, const N: usize, const MAX_KEY: usize> Default for SlotCache<V, N, MAX_KEY>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<V, const N: usize, const MAX_KEY: usize> Freeze for SlotCache<V, N, MAX_KEY>
where V: Freeze,

§

impl<V, const N: usize, const MAX_KEY: usize> RefUnwindSafe for SlotCache<V, N, MAX_KEY>
where V: RefUnwindSafe,

§

impl<V, const N: usize, const MAX_KEY: usize> Send for SlotCache<V, N, MAX_KEY>
where V: Send,

§

impl<V, const N: usize, const MAX_KEY: usize> Sync for SlotCache<V, N, MAX_KEY>
where V: Sync,

§

impl<V, const N: usize, const MAX_KEY: usize> Unpin for SlotCache<V, N, MAX_KEY>
where V: Unpin,

§

impl<V, const N: usize, const MAX_KEY: usize> UnsafeUnpin for SlotCache<V, N, MAX_KEY>
where V: UnsafeUnpin,

§

impl<V, const N: usize, const MAX_KEY: usize> UnwindSafe for SlotCache<V, N, MAX_KEY>
where V: UnwindSafe,

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.