Trait schnellru::Limiter

source ·
pub trait Limiter<K, V> {
    type KeyToInsert<'a>;
    type LinkType: LinkType;

    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§

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.

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§

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.

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.

Called before a node 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.

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 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.

Called after an element is removed from the map.

If applicable the internal cost estimate should be updated to account for the removed node.

Called after the map is cleared.

If applicable the internal cost estimate should be set to zero here.

Called before the map is resized.

Returns whether the map should be allowed to grow.

Returning false here will ensure that no memory will be allocated.

The new_memory_usage specifies how much memory the map will consume after the resize.

Implementors§