Struct uluru::LRUCache[][src]

pub struct LRUCache<T, const N: usize> { /* fields omitted */ }

A LRU cache using a statically-sized array for storage.

LRUCache uses a fixed-capacity array for storage. It provides O(1) insertion, and O(n) lookup.

All items are stored inline within the LRUCache, so it does not impose any heap allocation or indirection. A linked list is used to record the cache order, so the items themselves do not need to be moved when the order changes. (This is important for speed if the items are large.)

Example

use uluru::LRUCache;

struct MyValue {
    id: u32,
    name: &'static str,
}

// A cache with a capacity of three.
type MyCache = LRUCache<MyValue, 3>;

// Create an empty cache, then insert some items.
let mut cache = MyCache::default();
cache.insert(MyValue { id: 1, name: "Mercury" });
cache.insert(MyValue { id: 2, name: "Venus" });
cache.insert(MyValue { id: 3, name: "Earth" });

// Use the `find` method to retrieve an item from the cache.
// This also "touches" the item, marking it most-recently-used.
let item = cache.find(|x| x.id == 1);
assert_eq!(item.unwrap().name, "Mercury");

// If the cache is full, inserting a new item evicts the least-recently-used item:
cache.insert(MyValue { id: 4, name: "Mars" });
assert!(cache.find(|x| x.id == 2).is_none());

Implementations

impl<T, const N: usize> LRUCache<T, N>[src]

pub fn insert(&mut self, val: T)[src]

Insert a given key in the cache.

This item becomes the front (most-recently-used) item in the cache. If the cache is full, the back (least-recently-used) item will be removed.

pub fn find<F>(&mut self, pred: F) -> Option<&mut T> where
    F: FnMut(&T) -> bool, 
[src]

Returns the first item in the cache that matches the given predicate. Touches the result on a hit.

pub fn lookup<F, R>(&mut self, test_one: F) -> Option<R> where
    F: FnMut(&mut T) -> Option<R>, 
[src]

Performs a lookup on the cache with the given test routine. Touches the result on a hit.

pub fn len(&self) -> usize[src]

Returns the number of elements in the cache.

pub fn clear(&mut self)[src]

Evict all elements from the cache.

pub fn front(&self) -> Option<&T>[src]

Returns the front entry in the list (most recently used).

pub fn front_mut(&mut self) -> Option<&mut T>[src]

Returns a mutable reference to the front entry in the list (most recently used).

pub fn touch<F>(&mut self, pred: F) -> bool where
    F: FnMut(&T) -> bool, 
[src]

Touches the first item in the cache that matches the given predicate. Returns true on a hit, false if no matches.

Trait Implementations

impl<T, const N: usize> Clone for LRUCache<T, N> where
    T: Clone
[src]

impl<T, const N: usize> Debug for LRUCache<T, N> where
    T: Debug
[src]

impl<T, const N: usize> Default for LRUCache<T, N>[src]

Auto Trait Implementations

impl<T, const N: usize> Send for LRUCache<T, N> where
    T: Send

impl<T, const N: usize> Sync for LRUCache<T, N> where
    T: Sync

impl<T, const N: usize> Unpin for LRUCache<T, N> where
    T: Unpin

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.