pub struct LRUCache<T, M> {
    entries: FreeList<LRUCacheEntry<T>, M>,
    lru: Vec<LRUTracker<FreeListHandle<M>>>,
}
Expand description

The main public interface to the LRU cache

Fields§

§entries: FreeList<LRUCacheEntry<T>, M>

A free list of cache entries, and indices into the LRU tracking list

§lru: Vec<LRUTracker<FreeListHandle<M>>>

The LRU tracking list, allowing O(1) access to the oldest element

Implementations§

source§

impl<T, M> LRUCache<T, M>

source

pub fn new(lru_partition_count: usize) -> Self

Construct a new LRU cache

source

pub fn push_new( &mut self, partition_index: u8, value: T ) -> WeakFreeListHandle<M>

Insert a new element into the cache. Returns a weak handle for callers to access the data, since the lifetime is managed by the LRU algorithm and it may be evicted at any time.

source

pub fn get_opt(&self, handle: &WeakFreeListHandle<M>) -> Option<&T>

Get immutable access to the data at a given slot. Since this takes a weak handle, it may have been evicted, so returns an Option.

source

pub fn get_opt_mut(&mut self, handle: &WeakFreeListHandle<M>) -> Option<&mut T>

Get mutable access to the data at a given slot. Since this takes a weak handle, it may have been evicted, so returns an Option.

source

pub fn peek_oldest(&self, partition_index: u8) -> Option<&T>

Return a reference to the oldest item in the cache, keeping it in the cache. If the cache is empty, this will return None.

source

pub fn pop_oldest(&mut self, partition_index: u8) -> Option<T>

Remove the oldest item from the cache. This is used to select elements to be evicted. If the cache is empty, this will return None.

source

pub fn replace_or_insert( &mut self, handle: &mut WeakFreeListHandle<M>, partition_index: u8, data: T ) -> Option<T>

This is a special case of push_new, which is a requirement for the texture cache. Sometimes, we want to replace the content of an existing handle if it exists, or insert a new element if the handle is invalid (for example, if an image is resized and it moves to a new location in the texture atlas). This method returns the old cache entry if it existed, so it can be freed by the caller.

source

pub fn remove(&mut self, handle: &WeakFreeListHandle<M>) -> Option<T>

Manually evict a specific item.

source

pub fn touch(&mut self, handle: &WeakFreeListHandle<M>) -> Option<&mut T>

This is used by the calling code to signal that the element that this handle references has been used on this frame. Internally, it updates the links in the LRU tracking element to move this item to the end of the LRU list. Returns the underlying data in case the client wants to mutate it.

Trait Implementations§

source§

impl<T, M> MallocSizeOf for LRUCache<T, M>where T: MallocSizeOf, M: MallocSizeOf,

source§

fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize

Measure the heap usage of all descendant heap-allocated structures, but not the space taken up by the value itself.
source§

impl<T, M> Serialize for LRUCache<T, M>where T: Serialize, M: Serialize,

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<T, M> RefUnwindSafe for LRUCache<T, M>where M: RefUnwindSafe, T: RefUnwindSafe,

§

impl<T, M> Send for LRUCache<T, M>where M: Send, T: Send,

§

impl<T, M> Sync for LRUCache<T, M>where M: Sync, T: Sync,

§

impl<T, M> Unpin for LRUCache<T, M>where M: Unpin, T: Unpin,

§

impl<T, M> UnwindSafe for LRUCache<T, M>where M: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.