pub struct LruMap<K, V, L = ByLength, S = RandomState>where
L: Limiter<K, V>,{ /* private fields */ }
Expand description
An LRU hash map.
Implementations§
source§impl<K, V, L> LruMap<K, V, L, RandomState>
impl<K, V, L> LruMap<K, V, L, RandomState>
source§impl<K, V, S> LruMap<K, V, ByMemoryUsage, S>
impl<K, V, S> LruMap<K, V, ByMemoryUsage, S>
sourcepub const fn with_memory_budget_and_hasher(
memory_budget: usize,
hasher: S,
) -> Self
pub const fn with_memory_budget_and_hasher( memory_budget: usize, hasher: S, ) -> Self
Creates a new empty map with a given memory_budget
and hasher
.
This will configure the limiter so that the map can use at most memory_budget
bytes of memory.
This does not preallocate any memory.
source§impl<K, V> LruMap<K, V, ByMemoryUsage, RandomState>
impl<K, V> LruMap<K, V, ByMemoryUsage, RandomState>
sourcepub fn with_memory_budget(memory_budget: usize) -> Self
pub fn with_memory_budget(memory_budget: usize) -> Self
Creates a new empty map with a given memory_budget
.
This will configure the limiter so that the map can use at most memory_budget
bytes of memory.
This does not preallocate any memory.
source§impl<K, V, L, S> LruMap<K, V, L, S>
impl<K, V, L, S> LruMap<K, V, L, S>
sourcepub const fn with_hasher(limiter: L, hasher: S) -> Self
pub const fn with_hasher(limiter: L, hasher: S) -> Self
Creates a new map with a given limiter
and hasher
.
sourcepub fn reserve_or_panic(&mut self, additional: usize)
pub fn reserve_or_panic(&mut self, additional: usize)
Ensures that at least additional
items can be inserted into the table without
reallocation.
§Panics
Will panic if the total number of elements is too big or the memory couldn’t be allocated.
sourcepub fn guaranteed_capacity(&self) -> usize
pub fn guaranteed_capacity(&self) -> usize
Returns the current guaranteed capacity of the map.
This specifies how many elements the map is guaranteed to be able to hold without allocating any memory. This is a lower bound; it’s possible that the map can hold more elements than this.
This value will change as elements are added and removed. In particular, in might go down when an element is removed from the map.
This is independent of the cap imposed by the limiter.
sourcepub fn memory_usage_for_memory_budget(memory_budget: usize) -> usize
pub fn memory_usage_for_memory_budget(memory_budget: usize) -> usize
Returns an estimated memory usage this map will have with a given memory_budget
.
sourcepub fn memory_usage(&self) -> usize
pub fn memory_usage(&self) -> usize
Returns the current memory usage of the map, in bytes.
sourcepub fn get(
&mut self,
key: &(impl Hash + PartialEq<K> + ?Sized),
) -> Option<&mut V>
pub fn get( &mut self, key: &(impl Hash + PartialEq<K> + ?Sized), ) -> Option<&mut V>
Returns a reference to the value for a given key and promotes that element to be the most recently used.
sourcepub fn get_by_hash(
&mut self,
hash: u64,
is_eq: impl FnMut(&K, &V) -> bool,
) -> Option<&mut V>
pub fn get_by_hash( &mut self, hash: u64, is_eq: impl FnMut(&K, &V) -> bool, ) -> Option<&mut V>
Returns a reference to the value for a given hash
and for which is_eq
returns true
.
Promotes that element to be the most recently used.
sourcepub fn get_or_insert<'a>(
&mut self,
key: impl Into<L::KeyToInsert<'a>> + Hash + PartialEq<K> + ?Sized,
get: impl FnOnce() -> V,
) -> Option<&mut V>
pub fn get_or_insert<'a>( &mut self, key: impl Into<L::KeyToInsert<'a>> + Hash + PartialEq<K> + ?Sized, get: impl FnOnce() -> V, ) -> Option<&mut V>
Tries to get the value for a given key
or insert a new value if there’s no element
in the map which matches that key
.
If present the element will be promoted to be the most recently used.
sourcepub fn get_or_insert_fallible<'a, E>(
&mut self,
key: impl Into<L::KeyToInsert<'a>> + Hash + PartialEq<K> + ?Sized,
get: impl FnOnce() -> Result<V, E>,
) -> Result<Option<&mut V>, E>
pub fn get_or_insert_fallible<'a, E>( &mut self, key: impl Into<L::KeyToInsert<'a>> + Hash + PartialEq<K> + ?Sized, get: impl FnOnce() -> Result<V, E>, ) -> Result<Option<&mut V>, E>
Same as Self::get_or_insert
, just with a fallible callback.
sourcepub fn peek<'a>(
&'a self,
key: &(impl Hash + PartialEq<K> + ?Sized),
) -> Option<&'a V>
pub fn peek<'a>( &'a self, key: &(impl Hash + PartialEq<K> + ?Sized), ) -> Option<&'a V>
Returns a reference to the value for a given key. Does not change the order of the elements.
sourcepub fn peek_mut<'a>(
&'a mut self,
key: &(impl Hash + PartialEq<K> + ?Sized),
) -> Option<&'a mut V>
pub fn peek_mut<'a>( &'a mut self, key: &(impl Hash + PartialEq<K> + ?Sized), ) -> Option<&'a mut V>
Returns a mutable reference to the value for a given key. Does not change the order of the elements.
sourcepub fn peek_by_hash(
&self,
hash: u64,
is_eq: impl FnMut(&K, &V) -> bool,
) -> Option<&V>
pub fn peek_by_hash( &self, hash: u64, is_eq: impl FnMut(&K, &V) -> bool, ) -> Option<&V>
Returns a reference to the value for a given hash
and for which is_eq
returns true
.
Does not change the order of the elements.
sourcepub fn peek_by_hash_mut(
&mut self,
hash: u64,
is_eq: impl FnMut(&K, &V) -> bool,
) -> Option<&mut V>
pub fn peek_by_hash_mut( &mut self, hash: u64, is_eq: impl FnMut(&K, &V) -> bool, ) -> Option<&mut V>
Returns a mutable reference to the value for a given hash
and for which is_eq
returns true
.
Does not change the order of the elements.
sourcepub fn remove(&mut self, key: &(impl Hash + PartialEq<K> + ?Sized)) -> Option<V>
pub fn remove(&mut self, key: &(impl Hash + PartialEq<K> + ?Sized)) -> Option<V>
Removes an element from the map.
sourcepub fn insert<'a>(&mut self, key: L::KeyToInsert<'a>, value: V) -> bool
pub fn insert<'a>(&mut self, key: L::KeyToInsert<'a>, value: V) -> bool
Inserts a new element into the map.
Can fail if the element is rejected by the limiter or if we fail to grow an empty map.
Returns true
if the element was inserted; false
otherwise.
sourcepub fn pop_oldest(&mut self) -> Option<(K, V)>
pub fn pop_oldest(&mut self) -> Option<(K, V)>
Removes the least recently accessed element and returns it.
sourcepub fn pop_newest(&mut self) -> Option<(K, V)>
pub fn pop_newest(&mut self) -> Option<(K, V)>
Removes the most recently accessed element and returns it.
sourcepub fn peek_newest(&self) -> Option<(&K, &V)>
pub fn peek_newest(&self) -> Option<(&K, &V)>
Returns the most recently used (inserted or accessed) element of the map. Does not change the order of the elements.
sourcepub fn peek_oldest(&self) -> Option<(&K, &V)>
pub fn peek_oldest(&self) -> Option<(&K, &V)>
Returns the least recently used (inserted or accessed) element of the map. Does not change the order of the elements.
sourcepub fn iter(&self) -> Iter<'_, K, V, L> ⓘ
pub fn iter(&self) -> Iter<'_, K, V, L> ⓘ
An iterator over all of the elements in the most recently used order.
sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V, L> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V, L> ⓘ
A mutable iterator over all of the elements in the most recently used order.
sourcepub fn drain(&mut self) -> Drain<'_, K, V, L, S> ⓘ
pub fn drain(&mut self) -> Drain<'_, K, V, L, S> ⓘ
Drains the map of all of its elements in the most recently used order.
When the iterator is dropped the map will be automatically cleared.
sourcepub fn limiter_mut(&mut self) -> &mut L
pub fn limiter_mut(&mut self) -> &mut L
Returns a mutable reference to the map’s limiter.
Trait Implementations§
Auto Trait Implementations§
impl<K, V, L, S> Freeze for LruMap<K, V, L, S>
impl<K, V, L, S> RefUnwindSafe for LruMap<K, V, L, S>where
S: RefUnwindSafe,
<L as Limiter<K, V>>::LinkType: RefUnwindSafe,
L: RefUnwindSafe,
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V, L, S> Send for LruMap<K, V, L, S>
impl<K, V, L, S> Sync for LruMap<K, V, L, S>
impl<K, V, L, S> Unpin for LruMap<K, V, L, S>
impl<K, V, L, S> UnwindSafe for LruMap<K, V, L, S>where
S: UnwindSafe,
<L as Limiter<K, V>>::LinkType: UnwindSafe,
L: UnwindSafe,
K: UnwindSafe,
V: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)