ref-stable-lru 0.2.2

An LRU Cache implementation with compile-time reference stability
Documentation
  • Coverage
  • 0%
    0 out of 15 items documented0 out of 14 items with examples
  • Size
  • Source code size: 46.55 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.88 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 11s Average build duration of successful builds.
  • all releases: 11s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • TennyZhuang

LRU Cache with compile-time reference stability

The main codes are copy from lru-rs, very grateful for the project.

The main motivation for implementing this project is that LRUCache should allow multiple immutable references obtained through get method. Currently, this crate is under the active development stage. The purpose of this crate is to validate the new design pattern and hope to apply it to more collection libraries, preferably including std. As for this library itself, I would prefer its proposed new API to be merged to upstream.

The main idea is separating the value operating permissions from the data structure itself. The blog post elaborates the idea. You can also take a look at uitest, which explains the API design goals.

Example

Below is a simple example shows the main feature of LRUCache.

let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

cache.scope(|mut cache, mut perm| {
    assert_eq!(cache.put("apple", "red", &mut perm), None);
    assert_eq!(cache.put("banana", "yellow", &mut perm), None);
    assert_eq!(cache.put("lemon", "yellow", &mut perm), Some("red"));

    let colors: Vec<_> = ["apple", "banana", "lemon", "watermelon"]
        .iter()
        .map(|k| cache.get(k, &perm))
        .collect();
    assert!(colors[0].is_none());
    assert_opt_eq(colors[1], "yellow");
    assert_opt_eq(colors[2], "yellow");
    assert!(colors[3].is_none());
});