Struct EvictingCacheMap

Source
pub struct EvictingCacheMap<K, V, const N: usize, F, S = DefaultHashBuilder>
where F: Fn(K, V),
{ /* private fields */ }
Expand description

An evicting cache map.

Backed by a HashMap but maintains order and a maximum capacity. Upon inserting beyond capacity the cache map will remove the eldest elements to make room. Each removal or prune will call the provided prune hook with the values.

Being backed by a HashMap, this container has all the same requirements on its keys as HashMap. Specifically, Keys must implement Eq and Hash. You can typically derive these for types with #[derive(PartialEq, Eq, hash)]. If you implement these yourself you must ensure the following holds:

k1 == k2 -> hash(k1) == hash(k2)

You are also responsible to ensure Keys do not mutate such that their hash can change while in the map. For more information, check HashMap.

Implementations§

Source§

impl<K, V, const N: usize> EvictingCacheMap<K, V, N, fn(K, V), DefaultHashBuilder>

Source

pub fn new() -> Self

Creates a new map

Default prune hook simply drops K and V.

Source§

impl<K, V, const N: usize, F> EvictingCacheMap<K, V, N, F, DefaultHashBuilder>
where F: Fn(K, V),

Source

pub fn with_prune_hook(func: F) -> Self

Create a new map with the provided prune hook

Examples found in repository?
examples/channels.rs (line 17)
14fn main() {
15    let (tx, rx) = mpsc::channel();
16    let mut cachemap: EvictingCacheMap<String, u8, 10, _> =
17        EvictingCacheMap::with_prune_hook(move |k, v| tx.send((k, v)).unwrap());
18    let send = thread::spawn(move || {
19        for x in 0..20 {
20            cachemap.insert(x.to_string(), x)
21        }
22    });
23
24    let recv = thread::spawn(move || {
25        while let Ok((k, v)) = rx.recv() {
26            println!("{k}:{v}")
27        }
28    });
29
30    let _ = send.join();
31    let _ = recv.join();
32}
Source§

impl<K, V, const N: usize, S> EvictingCacheMap<K, V, N, fn(K, V), S>

Source

pub fn with_hasher(hash_builder: S) -> Self

Create a new map with provided hasher

Source§

impl<K, V, const N: usize, F, S> EvictingCacheMap<K, V, N, F, S>
where F: Fn(K, V),

Source

pub fn with_hasher_and_prune_hook(hash_builer: S, func: F) -> Self

Create a new map with the provided hasher and prune hook

Source§

impl<K, V, const N: usize, F> EvictingCacheMap<K, V, N, F>
where K: Eq + Hash, F: Fn(K, V),

Source

pub fn prune_by(&mut self, len: usize)

Remove elements and call prune hook

Removes len elements from the cache and calls the prune hook for each. If len is greater than the len() of the cache it will leave it empty.

Source

pub fn prune_by_with(&mut self, len: usize, f: impl Fn(K, V))

Remove elements and calls provided prune hook

Removes len elements from the cache and calls the prune hook for each. If len is greater than the len() of th cache it will leave it empty.

Source

pub fn prune_to(&mut self, len: usize)

Remove elements and call the prune hook

Removes elements until the cache is the provided size.

Source

pub fn prune_to_with(&mut self, len: usize, f: impl Fn(K, V))

Removeelements and call the provided prune hook

Removes elements until the cache is the provided size.

Source

pub fn promote<Q>(&mut self, key: &Q)
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Promote the element if it exists

This moves an element to the top of the cache as-if removed and re-inserted but does not call the prune hook.

Source

pub fn contains_key<Q>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Checks if cache contains this key

Does not promote the key.

Source

pub fn insert(&mut self, key: K, value: V)

Insert element to the top of the cache

If the container is full this will prune a single element, calling the prune hook.

Examples found in repository?
examples/channels.rs (line 20)
14fn main() {
15    let (tx, rx) = mpsc::channel();
16    let mut cachemap: EvictingCacheMap<String, u8, 10, _> =
17        EvictingCacheMap::with_prune_hook(move |k, v| tx.send((k, v)).unwrap());
18    let send = thread::spawn(move || {
19        for x in 0..20 {
20            cachemap.insert(x.to_string(), x)
21        }
22    });
23
24    let recv = thread::spawn(move || {
25        while let Ok((k, v)) = rx.recv() {
26            println!("{k}:{v}")
27        }
28    });
29
30    let _ = send.join();
31    let _ = recv.join();
32}
Source

pub fn get<Q>(&mut self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Get a reference to the corresponding element and promotes it to the top of the cache.

Source

pub fn get_no_promote<Q>(&mut self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Get a reference to the corresponding element without promoting it.

Source

pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Get a mutable reference to the corresponding element and promote it to the top of the cache.

Source

pub fn get_mut_no_promote<Q>(&mut self, key: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Get a mutable reference to the corresponding element without promoting it.

Source

pub fn get_key_value<Q>(&mut self, key: &Q) -> Option<(&K, &V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Get a reference to the key-value pair and promote it to the top of the cache.

Source

pub fn get_key_value_no_promote<Q>(&mut self, key: &Q) -> Option<(&K, &V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Get a reference to the key-value pair without promoting it.

Source

pub fn get_key_value_mut<Q>(&mut self, key: &Q) -> Option<(&K, &mut V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Get a mutable reference to the key-value pair and promote it to the top of the cache.

Source

pub fn get_key_value_mut_no_promote<Q>( &mut self, key: &Q, ) -> Option<(&K, &mut V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Get a mutable reference to the key-value pair without promoting it.

Source

pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Remove and return a value from the map.

Does not call the prune hook.

Source

pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Remove and return a key-value pair from the map.

Does not call the prune hook.

Source

pub fn len(&self) -> usize

Retrieve the current number of elements in the cache.

Source

pub fn is_empty(&self) -> bool

Check whether the cache is empty.

Source

pub fn clear(&mut self)

Clear all elements from the map and call the prune hook for each element.

Source

pub fn iter(&self) -> Iter<'_, K, V>

Iterator over the values in the map

Source

pub fn iter_mut(&mut self) -> IterMut<'_, K, V>

Mutable Iterator for the values in the map

Note: mutating a value through this iterator does not promote keys.

Trait Implementations§

Source§

impl<K, V, const N: usize, F, S> Debug for EvictingCacheMap<K, V, N, F, S>
where K: Debug, V: Debug, F: Fn(K, V),

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K, V, const N: usize> Default for EvictingCacheMap<K, V, N, fn(K, V), DefaultHashBuilder>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a, K, V, const N: usize, F, S> IntoIterator for &'a EvictingCacheMap<K, V, N, F, S>
where F: Fn(K, V),

Source§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, K, V, const N: usize, F, S> IntoIterator for &'a mut EvictingCacheMap<K, V, N, F, S>
where F: Fn(K, V),

Source§

type Item = (&'a K, &'a mut V)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<K, V, const N: usize, F, S> Freeze for EvictingCacheMap<K, V, N, F, S>
where F: Freeze, S: Freeze,

§

impl<K, V, const N: usize, F, S> RefUnwindSafe for EvictingCacheMap<K, V, N, F, S>

§

impl<K, V, const N: usize, F, S> Send for EvictingCacheMap<K, V, N, F, S>
where F: Send, K: Send, V: Send, S: Send,

§

impl<K, V, const N: usize, F, S> Sync for EvictingCacheMap<K, V, N, F, S>
where F: Sync, K: Sync, V: Sync, S: Sync,

§

impl<K, V, const N: usize, F, S> Unpin for EvictingCacheMap<K, V, N, F, S>
where F: Unpin, S: Unpin,

§

impl<K, V, const N: usize, F, S> UnwindSafe for EvictingCacheMap<K, V, N, F, S>

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.