generational-lru

Crates.iogenerational-lru
lib.rsgenerational-lru
version0.1.2
sourcesrc
created_at2022-06-26 18:44:30.506279
updated_at2022-06-27 06:34:03.783165
descriptionA generational arena based LRU Cache implementation in 100% safe rust.
homepage
repositoryhttps://github.com/arindas/lrucache
max_upload_size
id613657
size38,127
Arindam Das (arindas)

documentation

README

generational-lru

ci codecov rustdoc Crates.io

Crate providing a 100% safe, generational arena based LRU cache implementation.

use generational_lru::lrucache::{LRUCache, CacheError};

let capacity = 5;

let mut lru_cache = LRUCache::<i32, i32>::with_capacity(capacity);
assert_eq!(lru_cache.query(&0), Err(CacheError::CacheMiss));

for ele in 0..capacity {
    let x = ele as i32;
    assert!(lru_cache.insert(x, x).is_ok());
}

for ele in 0..capacity {
    let x = ele as i32;
    assert_eq!(lru_cache.query(&x), Ok(&x));
}

let x = capacity as i32;
assert!(lru_cache.insert(x, x).is_ok());

assert_eq!(lru_cache.query(&x), Ok(&x));

assert_eq!(lru_cache.query(&0), Err(CacheError::CacheMiss));

let x = capacity as i32 / 2;
assert_eq!(lru_cache.remove(&x), Ok(x));

assert_eq!(lru_cache.query(&x), Err(CacheError::CacheMiss));
assert_eq!(lru_cache.remove(&x), Err(CacheError::CacheMiss));

// zero capacity LRUCache is unusable
let mut lru_cache = LRUCache::<i32, i32>::with_capacity(0);

assert!(matches!(
    lru_cache.insert(0, 0),
    Err(CacheError::CacheBroken(_))
));

Refer to API documentation for more details.

Usage

This is a library crate. You may include it in your Cargo.toml as follows:

[dependencies]
generational-lru = "0.1"

License

This repository is licensed under the MIT License. See LICENSE for the full license text.

Commit count: 29

cargo fmt