pub struct LruCache<K: Eq + Hash, V, S: BuildHasher = RandomState> { /* private fields */ }
Expand description
An LRU cache.
Implementations§
Source§impl<K: Eq + Hash, V, S: BuildHasher> LruCache<K, V, S>
impl<K: Eq + Hash, V, S: BuildHasher> LruCache<K, V, S>
Sourcepub fn with_hasher(capacity: usize, hash_builder: S) -> Self
pub fn with_hasher(capacity: usize, hash_builder: S) -> Self
Creates an empty cache that can hold at most capacity
items with the given hash builder.
Sourcepub fn contains_key<Q>(&mut self, key: &Q) -> bool
pub fn contains_key<Q>(&mut self, key: &Q) -> bool
Checks if the map contains the given key.
§Examples
use lru_cache::LruCache;
let mut cache = LruCache::new(1);
cache.insert(1, "a");
assert_eq!(cache.contains_key(&1), true);
Sourcepub fn insert(&mut self, k: K, v: V) -> Option<V>
pub fn insert(&mut self, k: K, v: V) -> Option<V>
Inserts a key-value pair into the cache. If the key already existed, the old value is returned.
§Examples
use lru_cache::LruCache;
let mut cache = LruCache::new(2);
cache.insert(1, "a");
cache.insert(2, "b");
assert_eq!(cache.get_mut(&1), Some(&mut "a"));
assert_eq!(cache.get_mut(&2), Some(&mut "b"));
Sourcepub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
Returns a mutable reference to the value corresponding to the given key in the cache, if any.
§Examples
use lru_cache::LruCache;
let mut cache = LruCache::new(2);
cache.insert(1, "a");
cache.insert(2, "b");
cache.insert(2, "c");
cache.insert(3, "d");
assert_eq!(cache.get_mut(&1), None);
assert_eq!(cache.get_mut(&2), Some(&mut "c"));
Sourcepub fn remove<Q>(&mut self, k: &Q) -> Option<V>
pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
Removes the given key from the cache and returns its corresponding value.
§Examples
use lru_cache::LruCache;
let mut cache = LruCache::new(2);
cache.insert(2, "a");
assert_eq!(cache.remove(&1), None);
assert_eq!(cache.remove(&2), Some("a"));
assert_eq!(cache.remove(&2), None);
assert_eq!(cache.len(), 0);
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the maximum number of key-value pairs the cache can hold.
§Examples
use lru_cache::LruCache;
let mut cache: LruCache<i32, &str> = LruCache::new(2);
assert_eq!(cache.capacity(), 2);
Sourcepub fn set_capacity(&mut self, capacity: usize)
pub fn set_capacity(&mut self, capacity: usize)
Sets the number of key-value pairs the cache can hold. Removes least-recently-used key-value pairs if necessary.
§Examples
use lru_cache::LruCache;
let mut cache = LruCache::new(2);
cache.insert(1, "a");
cache.insert(2, "b");
cache.insert(3, "c");
assert_eq!(cache.get_mut(&1), None);
assert_eq!(cache.get_mut(&2), Some(&mut "b"));
assert_eq!(cache.get_mut(&3), Some(&mut "c"));
cache.set_capacity(3);
cache.insert(1, "a");
cache.insert(2, "b");
assert_eq!(cache.get_mut(&1), Some(&mut "a"));
assert_eq!(cache.get_mut(&2), Some(&mut "b"));
assert_eq!(cache.get_mut(&3), Some(&mut "c"));
cache.set_capacity(1);
assert_eq!(cache.get_mut(&1), None);
assert_eq!(cache.get_mut(&2), None);
assert_eq!(cache.get_mut(&3), Some(&mut "c"));
Sourcepub fn remove_lru(&mut self) -> Option<(K, V)>
pub fn remove_lru(&mut self) -> Option<(K, V)>
Removes and returns the least recently used key-value pair as a tuple.
§Examples
use lru_cache::LruCache;
let mut cache = LruCache::new(2);
cache.insert(1, "a");
cache.insert(2, "b");
assert_eq!(cache.remove_lru(), Some((1, "a")));
assert_eq!(cache.len(), 1);
Sourcepub fn iter(&self) -> Iter<'_, K, V> ⓘ
pub fn iter(&self) -> Iter<'_, K, V> ⓘ
Returns an iterator over the cache’s key-value pairs in least- to most-recently-used order.
Accessing the cache through the iterator does not affect the cache’s LRU state.
§Examples
use lru_cache::LruCache;
let mut cache = LruCache::new(2);
cache.insert(1, 10);
cache.insert(2, 20);
cache.insert(3, 30);
let kvs: Vec<_> = cache.iter().collect();
assert_eq!(kvs, [(&2, &20), (&3, &30)]);
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
Returns an iterator over the cache’s key-value pairs in least- to most-recently-used order, with mutable references to the values.
Accessing the cache through the iterator does not affect the cache’s LRU state.
§Examples
use lru_cache::LruCache;
let mut cache = LruCache::new(2);
cache.insert(1, 10);
cache.insert(2, 20);
cache.insert(3, 30);
let mut n = 2;
for (k, v) in cache.iter_mut() {
assert_eq!(*k, n);
assert_eq!(*v, n * 10);
*v *= 10;
n += 1;
}
assert_eq!(n, 4);
assert_eq!(cache.get_mut(&2), Some(&mut 200));
assert_eq!(cache.get_mut(&3), Some(&mut 300));
Trait Implementations§
Source§impl<K: Eq + Hash, V, S: BuildHasher> Extend<(K, V)> for LruCache<K, V, S>
impl<K: Eq + Hash, V, S: BuildHasher> Extend<(K, V)> for LruCache<K, V, S>
Source§fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<'a, K: Eq + Hash, V, S: BuildHasher> IntoIterator for &'a LruCache<K, V, S>
impl<'a, K: Eq + Hash, V, S: BuildHasher> IntoIterator for &'a LruCache<K, V, S>
Source§impl<'a, K: Eq + Hash, V, S: BuildHasher> IntoIterator for &'a mut LruCache<K, V, S>
impl<'a, K: Eq + Hash, V, S: BuildHasher> IntoIterator for &'a mut LruCache<K, V, S>
Source§impl<K: Eq + Hash, V, S: BuildHasher> IntoIterator for LruCache<K, V, S>
impl<K: Eq + Hash, V, S: BuildHasher> IntoIterator for LruCache<K, V, S>
Auto Trait Implementations§
impl<K, V, S> Freeze for LruCache<K, V, S>where
S: Freeze,
impl<K, V, S> RefUnwindSafe for LruCache<K, V, S>
impl<K, V, S> Send for LruCache<K, V, S>
impl<K, V, S> Sync for LruCache<K, V, S>
impl<K, V, S> Unpin for LruCache<K, V, S>where
S: Unpin,
impl<K, V, S> UnwindSafe for LruCache<K, V, S>
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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)