A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.rs/lru/latest/lru/struct.LruCache.html below:

LruCache in lru - Rust

pub struct LruCache<K, V, S = DefaultHasher> {  }
Expand description

An LRU Cache

Source§ Source

Creates a new LRU Cache that holds at most cap items.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
let mut cache: LruCache<isize, &str> = LruCache::new(NonZeroUsize::new(10).unwrap());
Source

Creates a new LRU Cache that never automatically evicts items.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
let mut cache: LruCache<isize, &str> = LruCache::unbounded();
Source§ Source

Creates a new LRU Cache that holds at most cap items and uses the provided hash builder to hash keys.

§Example
use lru::{LruCache, DefaultHasher};
use std::num::NonZeroUsize;

let s = DefaultHasher::default();
let mut cache: LruCache<isize, &str> = LruCache::with_hasher(NonZeroUsize::new(10).unwrap(), s);
Source

Creates a new LRU Cache that never automatically evicts items and uses the provided hash builder to hash keys.

§Example
use lru::{LruCache, DefaultHasher};

let s = DefaultHasher::default();
let mut cache: LruCache<isize, &str> = LruCache::unbounded_with_hasher(s);
Source

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;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

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"));
Source

Pushes a key-value pair into the cache. If an entry with key k already exists in the cache or another cache entry is removed (due to the lru’s capacity), then it returns the old entry’s key-value pair. Otherwise, returns None.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

assert_eq!(None, cache.push(1, "a"));
assert_eq!(None, cache.push(2, "b"));

assert_eq!(Some((2, "b")), cache.push(2, "beta"));

assert_eq!(Some((1, "a")), cache.push(3, "alpha"));

assert_eq!(cache.get(&1), None);
assert_eq!(cache.get(&2), Some(&"beta"));
assert_eq!(cache.get(&3), Some(&"alpha"));
Source

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;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

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"));
Source

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;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

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));
Source

Returns a key-value references pair 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;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

cache.put(String::from("1"), "a");
cache.put(String::from("2"), "b");
cache.put(String::from("2"), "c");
cache.put(String::from("3"), "d");

assert_eq!(cache.get_key_value("1"), None);
assert_eq!(cache.get_key_value("2"), Some((&String::from("2"), &"c")));
assert_eq!(cache.get_key_value("3"), Some((&String::from("3"), &"d")));
Source

Returns a key-value references pair of the key in the cache or None if it is not present in the cache. The reference to the value of the key is mutable. Moves the key to the head of the LRU list if it exists.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

cache.put(1, "a");
cache.put(2, "b");
let (k, v) = cache.get_key_value_mut(&1).unwrap();
assert_eq!(k, &1);
assert_eq!(v, &mut "a");
*v = "aa";
cache.put(3, "c");
assert_eq!(cache.get_key_value(&2), None);
assert_eq!(cache.get_key_value(&1), Some((&1, &"aa")));
assert_eq!(cache.get_key_value(&3), Some((&3, &"c")));
Source

Returns a reference to the value of the key in the cache if it is present in the cache and moves the key to the head of the LRU list. If the key does not exist the provided FnOnce is used to populate the list and a reference is returned.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

cache.put(1, "a");
cache.put(2, "b");
cache.put(2, "c");
cache.put(3, "d");

assert_eq!(cache.get_or_insert(2, ||"a"), &"c");
assert_eq!(cache.get_or_insert(3, ||"a"), &"d");
assert_eq!(cache.get_or_insert(1, ||"a"), &"a");
assert_eq!(cache.get_or_insert(1, ||"b"), &"a");
Source

Returns a reference to the value of the key in the cache if it is present in the cache and moves the key to the head of the LRU list. If the key does not exist the provided FnOnce is used to populate the list and a reference is returned. The value referenced by the key is only cloned (using to_owned()) if it doesn’t exist in the cache.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
use std::rc::Rc;

let key1 = Rc::new("1".to_owned());
let key2 = Rc::new("2".to_owned());
let mut cache = LruCache::<Rc<String>, String>::new(NonZeroUsize::new(2).unwrap());
assert_eq!(cache.get_or_insert_ref(&key1, ||"One".to_owned()), "One");
assert_eq!(cache.get_or_insert_ref(&key2, ||"Two".to_owned()), "Two");
assert_eq!(cache.get_or_insert_ref(&key2, ||"Not two".to_owned()), "Two");
assert_eq!(cache.get_or_insert_ref(&key2, ||"Again not two".to_owned()), "Two");
assert_eq!(Rc::strong_count(&key1), 2);
assert_eq!(Rc::strong_count(&key2), 2); 
Source

Returns a reference to the value of the key in the cache if it is present in the cache and moves the key to the head of the LRU list. If the key does not exist the provided FnOnce is used to populate the list and a reference is returned. If FnOnce returns Err, returns the Err.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

cache.put(1, "a");
cache.put(2, "b");
cache.put(2, "c");
cache.put(3, "d");

let f = ||->Result<&str, String> {Err("failed".to_owned())};
let a = ||->Result<&str, String> {Ok("a")};
let b = ||->Result<&str, String> {Ok("b")};
assert_eq!(cache.try_get_or_insert(2, a), Ok(&"c"));
assert_eq!(cache.try_get_or_insert(3, a), Ok(&"d"));
assert_eq!(cache.try_get_or_insert(4, f), Err("failed".to_owned()));
assert_eq!(cache.try_get_or_insert(5, b), Ok(&"b"));
assert_eq!(cache.try_get_or_insert(5, a), Ok(&"b"));
Source

Returns a reference to the value of the key in the cache if it is present in the cache and moves the key to the head of the LRU list. If the key does not exist the provided FnOnce is used to populate the list and a reference is returned. If FnOnce returns Err, returns the Err. The value referenced by the key is only cloned (using to_owned()) if it doesn’t exist in the cache and FnOnce succeeds.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
use std::rc::Rc;

let key1 = Rc::new("1".to_owned());
let key2 = Rc::new("2".to_owned());
let mut cache = LruCache::<Rc<String>, String>::new(NonZeroUsize::new(2).unwrap());
let f = ||->Result<String, ()> {Err(())};
let a = ||->Result<String, ()> {Ok("One".to_owned())};
let b = ||->Result<String, ()> {Ok("Two".to_owned())};
assert_eq!(cache.try_get_or_insert_ref(&key1, a), Ok(&"One".to_owned()));
assert_eq!(cache.try_get_or_insert_ref(&key2, f), Err(()));
assert_eq!(cache.try_get_or_insert_ref(&key2, b), Ok(&"Two".to_owned()));
assert_eq!(cache.try_get_or_insert_ref(&key2, a), Ok(&"Two".to_owned()));
assert_eq!(Rc::strong_count(&key1), 2);
assert_eq!(Rc::strong_count(&key2), 2); 
Source

Returns a mutable reference to the value of the key in the cache if it is present in the cache and moves the key to the head of the LRU list. If the key does not exist the provided FnOnce is used to populate the list and a mutable reference is returned.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

cache.put(1, "a");
cache.put(2, "b");

let v = cache.get_or_insert_mut(2, ||"c");
assert_eq!(v, &"b");
*v = "d";
assert_eq!(cache.get_or_insert_mut(2, ||"e"), &mut "d");
assert_eq!(cache.get_or_insert_mut(3, ||"f"), &mut "f");
assert_eq!(cache.get_or_insert_mut(3, ||"e"), &mut "f");
Source

Returns a mutable reference to the value of the key in the cache if it is present in the cache and moves the key to the head of the LRU list. If the key does not exist the provided FnOnce is used to populate the list and a mutable reference is returned. The value referenced by the key is only cloned (using to_owned()) if it doesn’t exist in the cache.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
use std::rc::Rc;

let key1 = Rc::new("1".to_owned());
let key2 = Rc::new("2".to_owned());
let mut cache = LruCache::<Rc<String>, &'static str>::new(NonZeroUsize::new(2).unwrap());
cache.get_or_insert_mut_ref(&key1, ||"One");
let v = cache.get_or_insert_mut_ref(&key2, ||"Two");
*v = "New two";
assert_eq!(cache.get_or_insert_mut_ref(&key2, ||"Two"), &mut "New two");
assert_eq!(Rc::strong_count(&key1), 2);
assert_eq!(Rc::strong_count(&key2), 2); 
Source

Returns a mutable reference to the value of the key in the cache if it is present in the cache and moves the key to the head of the LRU list. If the key does not exist the provided FnOnce is used to populate the list and a mutable reference is returned. If FnOnce returns Err, returns the Err.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

cache.put(1, "a");
cache.put(2, "b");
cache.put(2, "c");

let f = ||->Result<&str, String> {Err("failed".to_owned())};
let a = ||->Result<&str, String> {Ok("a")};
let b = ||->Result<&str, String> {Ok("b")};
if let Ok(v) = cache.try_get_or_insert_mut(2, a) {
    *v = "d";
}
assert_eq!(cache.try_get_or_insert_mut(2, a), Ok(&mut "d"));
assert_eq!(cache.try_get_or_insert_mut(3, f), Err("failed".to_owned()));
assert_eq!(cache.try_get_or_insert_mut(4, b), Ok(&mut "b"));
assert_eq!(cache.try_get_or_insert_mut(4, a), Ok(&mut "b"));
Source

Returns a mutable reference to the value of the key in the cache if it is present in the cache and moves the key to the head of the LRU list. If the key does not exist the provided FnOnce is used to populate the list and a mutable reference is returned. If FnOnce returns Err, returns the Err. The value referenced by the key is only cloned (using to_owned()) if it doesn’t exist in the cache and FnOnce succeeds.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
use std::rc::Rc;

let key1 = Rc::new("1".to_owned());
let key2 = Rc::new("2".to_owned());
let mut cache = LruCache::<Rc<String>, String>::new(NonZeroUsize::new(2).unwrap());
let f = ||->Result<String, ()> {Err(())};
let a = ||->Result<String, ()> {Ok("One".to_owned())};
let b = ||->Result<String, ()> {Ok("Two".to_owned())};
assert_eq!(cache.try_get_or_insert_mut_ref(&key1, a), Ok(&mut "One".to_owned()));
assert_eq!(cache.try_get_or_insert_mut_ref(&key2, f), Err(()));
if let Ok(v) = cache.try_get_or_insert_mut_ref(&key2, b) {
    *v = "New two".to_owned();
}
assert_eq!(cache.try_get_or_insert_mut_ref(&key2, a), Ok(&mut "New two".to_owned()));
assert_eq!(Rc::strong_count(&key1), 2);
assert_eq!(Rc::strong_count(&key2), 2); 
Source

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;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

cache.put(1, "a");
cache.put(2, "b");

assert_eq!(cache.peek(&1), Some(&"a"));
assert_eq!(cache.peek(&2), Some(&"b"));
Source

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;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

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"));
Source

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;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

cache.put(1, "a");
cache.put(2, "b");

assert_eq!(cache.peek_lru(), Some((&1, &"a")));
Source

Returns the value corresponding to the most recently used item or None if the cache is empty. Like peek, peek_mru does not update the LRU list so the item’s position will be unchanged.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

cache.put(1, "a");
cache.put(2, "b");

assert_eq!(cache.peek_mru(), Some((&2, &"b")));
Source

Returns a bool indicating whether the given key is in the cache. Does not update the LRU list.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

cache.put(1, "a");
cache.put(2, "b");
cache.put(3, "c");

assert!(!cache.contains(&1));
assert!(cache.contains(&2));
assert!(cache.contains(&3));
Source

Removes and returns the value corresponding to the key from the cache or None if it does not exist.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

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);
Source

Removes and returns the key and the value corresponding to the key from the cache or None if it does not exist.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

cache.put(1, "a");
cache.put(2, "a");

assert_eq!(cache.pop(&1), Some("a"));
assert_eq!(cache.pop_entry(&2), Some((2, "a")));
assert_eq!(cache.pop(&1), None);
assert_eq!(cache.pop_entry(&2), None);
assert_eq!(cache.len(), 0);
Source

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;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

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);
Source

Removes and returns the key and value corresponding to the most recently used item or None if the cache is empty.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

cache.put(2, "a");
cache.put(3, "b");
cache.put(4, "c");
cache.get(&3);

assert_eq!(cache.pop_mru(), Some((3, "b")));
assert_eq!(cache.pop_mru(), Some((4, "c")));
assert_eq!(cache.pop_mru(), None);
assert_eq!(cache.len(), 0);
Source

Marks the key as the most recently used one. Returns true if the key was promoted because it exists in the cache, false otherwise.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(3).unwrap());

cache.put(1, "a");
cache.put(2, "b");
cache.put(3, "c");
cache.get(&1);
cache.get(&2);

assert!(cache.promote(&3));
assert_eq!(cache.pop_lru(), Some((1, "a")));

assert!(!cache.promote(&4));
Source

Marks the key as the least recently used one. Returns true if the key was demoted because it exists in the cache, false otherwise.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(3).unwrap());

cache.put(1, "a");
cache.put(2, "b");
cache.put(3, "c");
cache.get(&1);
cache.get(&2);

assert!(cache.demote(&2));
assert!(cache.demote(&1));
assert_eq!(cache.pop_lru(), Some((1, "a")));
assert_eq!(cache.pop_lru(), Some((2, "b")));

assert!(!cache.demote(&4));
Source

Returns the number of key-value pairs that are currently in the the cache.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
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);
Source

Returns a bool indicating whether the cache is empty or not.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
assert!(cache.is_empty());

cache.put(1, "a");
assert!(!cache.is_empty());
Source

Returns the maximum number of key-value pairs the cache can hold.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
let mut cache: LruCache<isize, &str> = LruCache::new(NonZeroUsize::new(2).unwrap());
assert_eq!(cache.cap().get(), 2);
Source

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;
use std::num::NonZeroUsize;
let mut cache: LruCache<isize, &str> = LruCache::new(NonZeroUsize::new(2).unwrap());

cache.put(1, "a");
cache.put(2, "b");
cache.resize(NonZeroUsize::new(4).unwrap());
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"));
Source

Clears the contents of the cache.

§Example
use lru::LruCache;
use std::num::NonZeroUsize;
let mut cache: LruCache<isize, &str> = LruCache::new(NonZeroUsize::new(2).unwrap());
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);
Source

An iterator visiting all entries in most-recently used order. The iterator element type is (&K, &V).

§Examples
use lru::LruCache;
use std::num::NonZeroUsize;

let mut cache = LruCache::new(NonZeroUsize::new(3).unwrap());
cache.put("a", 1);
cache.put("b", 2);
cache.put("c", 3);

for (key, val) in cache.iter() {
    println!("key: {} val: {}", key, val);
}
Source

An iterator visiting all entries in most-recently-used order, giving a mutable reference on V. The iterator element type is (&K, &mut V).

§Examples
use lru::LruCache;
use std::num::NonZeroUsize;

struct HddBlock {
    dirty: bool,
    data: [u8; 512]
}

let mut cache = LruCache::new(NonZeroUsize::new(3).unwrap());
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]});

for (block_id, block) in cache.iter_mut() {
    if block.dirty {
        block.dirty = false
    }
}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4