qlrumap 0.1.0

An LRU HashMap with an optional passive ttl feature.
Documentation
use std::time::Duration;

#[cfg(feature = "fake-clock")]
use sn_fake_clock::FakeClock as Instant;

use qlrumap::LruMap;

#[cfg(test)]
fn delay(secs: u64) {
  #[cfg(not(feature = "sn_fake_clock"))]
  std::thread::sleep(Duration::from_secs(secs));

  // Pretend to wait more than 2s and run cleanup again
  #[cfg(feature = "sn_fake_clock")]
  Instant::advance_time(secs * 1000);
}

#[test]
fn single_entry() {
  let ttl = Duration::from_secs(1);

  let mut map = LruMap::with_ttl(ttl);

  map.insert("hello", "world");
  assert_eq!(map.youngest(), Some((&"hello", &"world")));
  assert_eq!(map.oldest(), Some((&"hello", &"world")));
  delay(1);
  map.insert("moo", "cow");
  assert_eq!(map.youngest(), Some((&"moo", &"cow")));
  assert_eq!(map.oldest(), Some((&"hello", &"world")));
  delay(1);
  map.insert("meow", "cat");
  assert_eq!(map.youngest(), Some((&"meow", &"cat")));
  assert_eq!(map.oldest(), Some((&"hello", &"world")));
  delay(1);

  map.touch("moo");
  assert_eq!(map.youngest(), Some((&"moo", &"cow")));
  assert_eq!(map.oldest(), Some((&"hello", &"world")));
}

// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :