qlrumap 0.1.0

An LRU HashMap with an optional passive ttl feature.
Documentation
use qlrumap::LruMap;

fn init_test_map() -> LruMap<&'static str, u32> {
  let mut map = LruMap::new();

  map.insert("a", 10); // Oldest
  map.insert("b", 20);
  map.insert("c", 30); // Youngest

  map
}

#[test]
fn immutable() {
  let map = init_test_map();

  let collected: Vec<_> = map.iter().collect();
  assert_eq!(
    collected,
    vec![(&"c", &30), (&"b", &20), (&"a", &10)],
    "iter() did not yield items in youngest-to-oldest order"
  );

  // Test `&map` syntax and ensure the map is not consumed
  let mut count = 0;
  for _ in &map {
    count += 1;
  }
  assert_eq!(count, 3, "Iteration over &map should visit all items");
  assert_eq!(
    map.len(),
    3,
    "Map should not be consumed by immutable iteration"
  );
}

#[test]
fn mutable() {
  let mut map = init_test_map();

  #[expect(clippy::explicit_iter_loop)]
  for (key, val) in map.iter_mut() {
    if *key == "b" {
      *val *= 2; // Modify the value
    }
  }

  // Verify the modification
  let collected_after_mut: Vec<_> = map.iter().collect();
  assert_eq!(
    collected_after_mut,
    vec![(&"c", &30), (&"b", &40), (&"a", &10)],
    "Mutable iterator did not correctly modify the value"
  );
  assert_eq!(
    map.len(),
    3,
    "Map should not be consumed by mutable iteration"
  );
}

#[test]
fn consuming() {
  let map = init_test_map();

  let mut collected_owned = vec![];
  for (k, v) in map {
    // This uses into_iter() implicitly
    collected_owned.push((k, v));
  }

  assert_eq!(
    collected_owned,
    vec![("c", 30), ("b", 20), ("a", 10)],
    "Consuming iterator did not yield items in youngest-to-oldest order"
  );

  // After being consumed, `map` cannot be used anymore.
  // The following line would cause a compile error:
  // assert_eq!(map.len(), 0);
}

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