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§
Sourcefn entry_or_insert_with<F>(&mut self, idx: Idx, value: F) -> &mut Twhere
F: FnOnce(Idx) -> T,
fn entry_or_insert_with<F>(&mut self, idx: Idx, value: F) -> &mut Twhere
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.
Provided Methods§
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.