[−][src]Struct lru::LruCache
An LRU Cache
Methods
impl<K: Hash + Eq, V> LruCache<K, V>
[src]
pub fn new(cap: usize) -> LruCache<K, V>
[src]
Creates a new LRU Cache that holds at most cap
items.
Example
use lru::LruCache; let mut cache: LruCache<isize, &str> = LruCache::new(10);
pub fn unbounded() -> LruCache<K, V>
[src]
Creates a new LRU Cache that never automatically evicts items.
Example
use lru::LruCache; let mut cache: LruCache<isize, &str> = LruCache::unbounded();
impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S>
[src]
pub fn with_hasher(cap: usize, hash_builder: S) -> LruCache<K, V, S>
[src]
Creates a new LRU Cache that holds at most cap
items and
uses the providedash builder to hash keys.
Example
use lru::{LruCache, DefaultHasher}; let s = DefaultHasher::default(); let mut cache: LruCache<isize, &str> = LruCache::with_hasher(10, s);
pub fn put(&mut self, k: K, v: V) -> Option<V>
[src]
Puts a key-value pair into cache. If the key already exists in the cache, then it updates
the key's value and returns the old value. Otherwise, None
is returned.
Example
use lru::LruCache; let mut cache = LruCache::new(2); assert_eq!(None, cache.put(1, "a")); assert_eq!(None, cache.put(2, "b")); assert_eq!(Some("b"), cache.put(2, "beta")); assert_eq!(cache.get(&1), Some(&"a")); assert_eq!(cache.get(&2), Some(&"beta"));
pub fn get<'a, Q: ?Sized>(&'a mut self, k: &Q) -> Option<&'a V> where
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq,
[src]
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq,
Returns a reference to the value of the key in the cache or None
if it is not
present in the cache. Moves the key to the head of the LRU list if it exists.
Example
use lru::LruCache; let mut cache = LruCache::new(2); cache.put(1, "a"); cache.put(2, "b"); cache.put(2, "c"); cache.put(3, "d"); assert_eq!(cache.get(&1), None); assert_eq!(cache.get(&2), Some(&"c")); assert_eq!(cache.get(&3), Some(&"d"));
pub fn get_mut<'a, Q: ?Sized>(&'a mut self, k: &Q) -> Option<&'a mut V> where
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq,
[src]
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq,
Returns a mutable reference to the value of the key in the cache or None
if it
is not present in the cache. Moves the key to the head of the LRU list if it exists.
Example
use lru::LruCache; let mut cache = LruCache::new(2); cache.put("apple", 8); cache.put("banana", 4); cache.put("banana", 6); cache.put("pear", 2); assert_eq!(cache.get_mut(&"apple"), None); assert_eq!(cache.get_mut(&"banana"), Some(&mut 6)); assert_eq!(cache.get_mut(&"pear"), Some(&mut 2));
pub fn peek<'a, Q: ?Sized>(&'a self, k: &Q) -> Option<&'a V> where
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq,
[src]
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq,
Returns a reference to the value corresponding to the key in the cache or None
if it is
not present in the cache. Unlike get
, peek
does not update the LRU list so the key's
position will be unchanged.
Example
use lru::LruCache; let mut cache = LruCache::new(2); cache.put(1, "a"); cache.put(2, "b"); assert_eq!(cache.peek(&1), Some(&"a")); assert_eq!(cache.peek(&2), Some(&"b"));
pub fn peek_mut<'a, Q: ?Sized>(&'a mut self, k: &Q) -> Option<&'a mut V> where
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq,
[src]
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq,
Returns a mutable reference to the value corresponding to the key in the cache or None
if it is not present in the cache. Unlike get_mut
, peek_mut
does not update the LRU
list so the key's position will be unchanged.
Example
use lru::LruCache; let mut cache = LruCache::new(2); cache.put(1, "a"); cache.put(2, "b"); assert_eq!(cache.peek_mut(&1), Some(&mut "a")); assert_eq!(cache.peek_mut(&2), Some(&mut "b"));
pub fn peek_lru<'a>(&'a self) -> Option<(&'a K, &'a V)>
[src]
Returns the value corresponding to the least recently used item or None
if the
cache is empty. Like peek
, peek_lru
does not update the LRU list so the item's
position will be unchanged.
Example
use lru::LruCache; let mut cache = LruCache::new(2); cache.put(1, "a"); cache.put(2, "b"); assert_eq!(cache.peek_lru(), Some((&1, &"a")));
pub fn contains<Q: ?Sized>(&self, k: &Q) -> bool where
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq,
[src]
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq,
Returns a bool indicating whether the given key is in the cache. Does not update the LRU list.
Example
use lru::LruCache; let mut cache = LruCache::new(2); cache.put(1, "a"); cache.put(2, "b"); cache.put(3, "c"); assert!(!cache.contains(&1)); assert!(cache.contains(&2)); assert!(cache.contains(&3));
pub fn pop<Q: ?Sized>(&mut self, k: &Q) -> Option<V> where
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq,
[src]
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq,
Removes and returns the value corresponding to the key from the cache or
None
if it does not exist.
Example
use lru::LruCache; let mut cache = LruCache::new(2); cache.put(2, "a"); assert_eq!(cache.pop(&1), None); assert_eq!(cache.pop(&2), Some("a")); assert_eq!(cache.pop(&2), None); assert_eq!(cache.len(), 0);
pub fn pop_lru(&mut self) -> Option<(K, V)>
[src]
Removes and returns the key and value corresponding to the least recently
used item or None
if the cache is empty.
Example
use lru::LruCache; let mut cache = LruCache::new(2); cache.put(2, "a"); cache.put(3, "b"); cache.put(4, "c"); cache.get(&3); assert_eq!(cache.pop_lru(), Some((4, "c"))); assert_eq!(cache.pop_lru(), Some((3, "b"))); assert_eq!(cache.pop_lru(), None); assert_eq!(cache.len(), 0);
pub fn len(&self) -> usize
[src]
Returns the number of key-value pairs that are currently in the the cache.
Example
use lru::LruCache; let mut cache = LruCache::new(2); assert_eq!(cache.len(), 0); cache.put(1, "a"); assert_eq!(cache.len(), 1); cache.put(2, "b"); assert_eq!(cache.len(), 2); cache.put(3, "c"); assert_eq!(cache.len(), 2);
pub fn is_empty(&self) -> bool
[src]
Returns a bool indicating whether the cache is empty or not.
Example
use lru::LruCache; let mut cache = LruCache::new(2); assert!(cache.is_empty()); cache.put(1, "a"); assert!(!cache.is_empty());
pub fn cap(&self) -> usize
[src]
Returns the maximum number of key-value pairs the cache can hold.
Example
use lru::LruCache; let mut cache: LruCache<isize, &str> = LruCache::new(2); assert_eq!(cache.cap(), 2);
pub fn resize(&mut self, cap: usize)
[src]
Resizes the cache. If the new capacity is smaller than the size of the current cache any entries past the new capacity are discarded.
Example
use lru::LruCache; let mut cache: LruCache<isize, &str> = LruCache::new(2); cache.put(1, "a"); cache.put(2, "b"); cache.resize(4); cache.put(3, "c"); cache.put(4, "d"); assert_eq!(cache.len(), 4); assert_eq!(cache.get(&1), Some(&"a")); assert_eq!(cache.get(&2), Some(&"b")); assert_eq!(cache.get(&3), Some(&"c")); assert_eq!(cache.get(&4), Some(&"d"));
pub fn clear(&mut self)
[src]
Clears the contents of the cache.
Example
use lru::LruCache; let mut cache: LruCache<isize, &str> = LruCache::new(2); assert_eq!(cache.len(), 0); cache.put(1, "a"); assert_eq!(cache.len(), 1); cache.put(2, "b"); assert_eq!(cache.len(), 2); cache.clear(); assert_eq!(cache.len(), 0);
ⓘImportant traits for Iter<'a, K, V>pub fn iter<'a>(&'a self) -> Iter<'a, K, V>
[src]
An iterator visiting all entries in order. The iterator element type is (&'a K, &'a V)
.
Examples
use lru::LruCache; let mut cache = LruCache::new(3); cache.put("a", 1); cache.put("b", 2); cache.put("c", 3); for (key, val) in cache.iter() { println!("key: {} val: {}", key, val); }
ⓘImportant traits for IterMut<'a, K, V>pub fn iter_mut<'a>(&'a mut self) -> IterMut<'a, K, V>
[src]
An iterator visiting all entries in order, giving a mutable reference on V.
The iterator element type is (&'a K, &'a mut V)
.
Examples
use lru::LruCache; struct HddBlock { dirty: bool, data: [u8; 512] } let mut cache = LruCache::new(3); cache.put(0, HddBlock { dirty: false, data: [0x00; 512]}); cache.put(1, HddBlock { dirty: true, data: [0x55; 512]}); cache.put(2, HddBlock { dirty: true, data: [0x77; 512]}); // write dirty blocks to disk. for (block_id, block) in cache.iter_mut() { if block.dirty { // write block to disk block.dirty = false } }
Trait Implementations
impl<K: Hash + Eq, V> Debug for LruCache<K, V>
[src]
impl<K, V, S> Drop for LruCache<K, V, S>
[src]
impl<K: Send, V: Send> Send for LruCache<K, V>
[src]
impl<K: Sync, V: Sync> Sync for LruCache<K, V>
[src]
impl<'a, K: Hash + Eq, V, S: BuildHasher> IntoIterator for &'a LruCache<K, V, S>
[src]
type Item = (&'a K, &'a V)
The type of the elements being iterated over.
type IntoIter = Iter<'a, K, V>
Which kind of iterator are we turning this into?
ⓘImportant traits for Iter<'a, K, V>fn into_iter(self) -> Iter<'a, K, V>
[src]
impl<'a, K: Hash + Eq, V, S: BuildHasher> IntoIterator for &'a mut LruCache<K, V, S>
[src]
Auto Trait Implementations
impl<K, V, S> Unpin for LruCache<K, V, S> where
S: Unpin,
S: Unpin,
impl<K, V, S = ABuildHasher> !Send for LruCache<K, V, S>
impl<K, V, S = ABuildHasher> !Sync for LruCache<K, V, S>
impl<K, V, S> UnwindSafe for LruCache<K, V, S> where
K: RefUnwindSafe + UnwindSafe,
S: UnwindSafe,
V: RefUnwindSafe + UnwindSafe,
K: RefUnwindSafe + UnwindSafe,
S: UnwindSafe,
V: RefUnwindSafe + UnwindSafe,
impl<K, V, S> RefUnwindSafe for LruCache<K, V, S> where
K: RefUnwindSafe,
S: RefUnwindSafe,
V: RefUnwindSafe,
K: RefUnwindSafe,
S: RefUnwindSafe,
V: RefUnwindSafe,
Blanket Implementations
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = !
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,