orx_v

Trait Cache

Source
pub trait Cache<Idx, T>: Default {
    // Required methods
    fn len(&self) -> usize;
    fn entry_or_insert_with<F>(&mut self, idx: Idx, value: F) -> &mut T
       where F: FnOnce(Idx) -> T;
    fn clear(&mut self);

    // Provided method
    fn is_empty(&self) -> bool { ... }
}
Expand description

A simple map of indices to values.

Note that HashMap (with std) or BTreeMap (when no-std) satisfy the requirements and implement the Cache trait, and hence, can be used as the caching storage in cached vectors.

Alternatively, more advanced caches can be provided depending on the use case, such as least recently used (LRU) caches.

Required Methods§

Source

fn len(&self) -> usize

Number of (idx, value) pairs in the cache.

Source

fn entry_or_insert_with<F>(&mut self, idx: Idx, value: F) -> &mut T
where F: FnOnce(Idx) -> T,

If the cache contains an element with the given idx, the method returns a mutable reference to the value.

Otherwise,

  • calls the value() method to create the value,
  • inserts (idx, value()) to the cache, and
  • returns a mutable reference to the newly inserted value.
Source

fn clear(&mut self)

Clears the cache.

Provided Methods§

Source

fn is_empty(&self) -> bool

Returns true if the cache is empty.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<Idx: Eq + Hash + Copy, T> Cache<Idx, T> for HashMap<Idx, T>

Source§

fn len(&self) -> usize

Source§

fn clear(&mut self)

Source§

fn entry_or_insert_with<F>(&mut self, idx: Idx, value: F) -> &mut T
where F: FnOnce(Idx) -> T,

Source§

impl<Idx: Ord + Copy, T> Cache<Idx, T> for BTreeMap<Idx, T>

Source§

fn len(&self) -> usize

Source§

fn clear(&mut self)

Source§

fn entry_or_insert_with<F>(&mut self, idx: Idx, value: F) -> &mut T
where F: FnOnce(Idx) -> T,

Implementors§