pub trait Limiter<K, V> {
type KeyToInsert<'a>;
type LinkType: LinkType;
// Required methods
fn is_over_the_limit(&self, length: usize) -> bool;
fn on_insert(
&mut self,
length: usize,
key: Self::KeyToInsert<'_>,
value: V,
) -> Option<(K, V)>;
fn on_replace(
&mut self,
length: usize,
old_key: &mut K,
new_key: Self::KeyToInsert<'_>,
old_value: &mut V,
new_value: &mut V,
) -> bool;
fn on_removed(&mut self, key: &mut K, value: &mut V);
fn on_cleared(&mut self);
fn on_grow(&mut self, new_memory_usage: usize) -> bool;
}
Expand description
A trait used to customize the behavior and the limits of the LRU map.
Required Associated Types§
sourcetype KeyToInsert<'a>
type KeyToInsert<'a>
The type of the key to be inserted into the map.
This is the type which is passed into insert
.
This usually should be the same as K
, but can be set to a different type
if creating an instance of K
is not zero-cost and we want to achieve
zero-cost replacement of existing values through insert
.
sourcetype LinkType: LinkType
type LinkType: LinkType
The type used to hold the links between the nodes inside of the LRU map.
In practice this type determines the maximum number of elements that can be in the map, regardless of any other limits imposed on it.
The exact limit is implementation defined.
The narrower the type the less memory each element in the map will use, and the map should also get slightly faster as long as this limit will not be hit.
Can be one of: usize
, u32
, u16
, u8
.
You probably want to use either usize
or u32
here.
Required Methods§
sourcefn is_over_the_limit(&self, length: usize) -> bool
fn is_over_the_limit(&self, length: usize) -> bool
Checks whether any of the elements must be popped.
This is called before an element is inserted, and after it was inserted.
The length
specifies how many elements will be in the map or how many are currently
in the map, depending on whether this was called before or after an insert.
sourcefn on_insert(
&mut self,
length: usize,
key: Self::KeyToInsert<'_>,
value: V,
) -> Option<(K, V)>
fn on_insert( &mut self, length: usize, key: Self::KeyToInsert<'_>, value: V, ) -> Option<(K, V)>
Called before a node is inserted into the map.
Should return None
if it would be impossible to insert a given element into an empty map.
Otherwise should always return Some
and, if applicable, update its internal cost estimate.
Changing the key here in a way that hashes or compares differently is a logic error.
The length
specifies how many elements are currently in the map.
The key
and value
is the key and value of the element we’re about to insert.
sourcefn on_replace(
&mut self,
length: usize,
old_key: &mut K,
new_key: Self::KeyToInsert<'_>,
old_value: &mut V,
new_value: &mut V,
) -> bool
fn on_replace( &mut self, length: usize, old_key: &mut K, new_key: Self::KeyToInsert<'_>, old_value: &mut V, new_value: &mut V, ) -> bool
Called before a value is replaced inside of the map.
Should return false
if it would be impossible to insert a given element into an empty map.
Otherwise should always return true
and, if applicable, update its internal cost estimate.
Returning false
will remove the element from the map.
The length
specifies how many elements are currently in the map.
The old_key
and old_value
is the key and value that are already inside of the map.
The new_key
and new_value
is the key and value of the element we’re about to insert.
Changing the old_key
here in a way that hashes or compares differently is a logic error.
This is only called when the keys are equal! The old_key
will be kept in the map while
the new_key
is moved into this callback, so you’re free to do with it as you see fit.
sourcefn on_removed(&mut self, key: &mut K, value: &mut V)
fn on_removed(&mut self, key: &mut K, value: &mut V)
Called after an element is removed from the map.
If applicable the internal cost estimate should be updated to account for the removed node.
sourcefn on_cleared(&mut self)
fn on_cleared(&mut self)
Called after the map is cleared.
If applicable the internal cost estimate should be set to zero here.