LruTextureCache

Struct LruTextureCache 

Source
pub struct LruTextureCache<K: Hash + Eq, V> { /* private fields */ }
Expand description

A LRU texture cache to cache rects in a texture.

§Algorithm

This works by dividing the texture in rows. When adding a rect it check if it fit in any of the existing rows. If not, a new row is created, with the height of the added rect. If there is no space for new rows, a range of unused rows with a height that fit the rect is remove, and a new row that fit the rect is added. If no row is find, the entire cache is cleared.

When adding multiple rects, all rows that contains any of the already added rects are marked as used, and the remain are the unused ones.

Implementations§

Source§

impl<K: Hash + Eq + Copy + 'static, V: Clone> LruTextureCache<K, V>

Source

pub fn new(width: u32, height: u32) -> Self

Create a new empty cache, with the given width and height.

Source

pub fn width(&self) -> u32

The width of the cache

Source

pub fn height(&self) -> u32

The height of the cache

Source

pub fn min_height(&self) -> u32

The height of the occupied area of the cache.

Cached rects are placed at the top, and grows to bottom, this is the position of the bottom of the lowest rect.

Source

pub fn len(&self) -> usize

The number of rects currently in the cache

Source

pub fn is_empty(&self) -> bool

Return true if there is no rect in the cache.

Source

pub fn clear(&mut self)

Clears the cache, leaving it as it if were new.

Source

pub fn contains(&self, key: &K) -> bool

Return true if there is a cached rect associated with the given Key. Otherwise, return false.

Source

pub fn cache_rects<D>( &mut self, rects: &mut [RectEntry<K, V, D>], ) -> Result<Cached, CacheErr>

Cache the given slice of rects.

The given slice of rects is reorder so that it start with every rect that was added to the cache. This way you can iterate over them by subslicing rects (rects[0..len], where len is Cached::len or CacheErr::len) and draw them to the texture cache.

If return Ok, it is guaranteed that each one of the given rects will be present in the cache after this call, but any rects from previous calls could have been removed.

Source

pub fn get_rect(&self, key: &K) -> Option<Rect<V>>

Return the rect where the texture with the given key is stored in the texture.

Auto Trait Implementations§

§

impl<K, V> Freeze for LruTextureCache<K, V>

§

impl<K, V> RefUnwindSafe for LruTextureCache<K, V>

§

impl<K, V> Send for LruTextureCache<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for LruTextureCache<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for LruTextureCache<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for LruTextureCache<K, V>
where K: UnwindSafe, V: UnwindSafe,

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> 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<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.