#map #duration #no-std #timed-map

no-std timed-map

Lightweight map implementation that supports expiring entries and fully compatible with both std and no_std environments

15 stable releases

new 1.3.1 Apr 14, 2025
1.3.0 Jan 22, 2025
1.1.1 Nov 4, 2024
1.1.0 Oct 26, 2024
0.1.0 Oct 20, 2024

#138 in Data structures

Download history 681/week @ 2024-12-23 185/week @ 2024-12-30 1030/week @ 2025-01-06 1048/week @ 2025-01-13 946/week @ 2025-01-20 670/week @ 2025-01-27 1172/week @ 2025-02-03 998/week @ 2025-02-10 1098/week @ 2025-02-17 710/week @ 2025-02-24 823/week @ 2025-03-03 555/week @ 2025-03-10 743/week @ 2025-03-17 823/week @ 2025-03-24 559/week @ 2025-03-31 617/week @ 2025-04-07

2,818 downloads per month

MIT license

47KB
828 lines

timed-map

Lightweight map implementation that supports expiring entries and fully compatible with both std and no_std environments.

TimedMap allows storing key-value pairs with optional expiration times. Expiration is handled by an implementation of the Clock trait, which abstracts time handling for no_std environments.

When std feature is enabled (which is the default case), Clock trait is handled automatically from the crate internals with std::time::SystemTime.

Basic Usage:

In std environments:

use timed_map::TimedMap;
use std::time::Duration;

let mut map = TimedMap::new();

map.insert_expirable(1, "expirable value", Duration::from_secs(60));
assert_eq!(map.get(&1), Some(&"expirable value"));
assert!(map.get_remaining_duration(&1).is_some());

map.insert_constant(2, "constant value");
assert_eq!(map.get(&2), Some(&"constant value"));
assert!(map.get_remaining_duration(&2).is_none());

In no_std environments:

use core::time::Duration;
use timed_map::{Clock, TimedMap};

struct CustomClock;

impl Clock for CustomClock {
    fn elapsed_seconds_since_creation(&self) -> u64 {
    // Hardware-specific implementation to measure the elapsed time.
        0 // placeholder
    }
}

let clock = CustomClock;
let mut map = TimedMap::new(clock);

map.insert_expirable(1, "expirable value", Duration::from_secs(60));
assert_eq!(map.get(&1), Some(&"expirable value"));
assert!(map.get_remaining_duration(&1).is_some());

map.insert_constant(2, "constant value");
assert_eq!(map.get(&2), Some(&"constant value"));
assert!(map.get_remaining_duration(&2).is_none());

Advanced Usage & Tuning:

Customizing the Internal Map

By default, TimedMap uses BTreeMap to store data, but you can switch to FxHashMap or HashMap.

This is only available on std environments.

use timed_map::{MapKind, TimedMap};

let mut map = TimedMap::new_with_map_kind(MapKind::FxHashMap);

Manual Expiration Control

To have fully control over expired entries, use the *_unchecked functions and drop_expired_entries to handle expiration manually. This can boost performance by running expiration logic only when it's necessary to maximize the performance.

let mut map = TimedMap::new();

map.insert_expirable_unchecked(1, "expirable value", Duration::from_secs(60));
assert_eq!(map.get_unchecked(&1), Some(&"expirable value"));

map.insert_constant_unchecked(2, "constant value");
assert_eq!(map.get_unchecked(&2), Some(&"constant value"));

map.drop_expired_entries();

Setting Expiration Check Frequency

In cases where inserts are frequent, expiration_tick_cap can be set to control how often expired entries are removed. For instance, if there are 100 inserts per second, setting expiration_tick_cap to 100 will trigger the expiration check every 100 inserts which will reduce the expiration logic overhead significantly.

use timed_map::TimedMap;

let mut map = TimedMap::new().expiration_tick_cap(500);

Dependencies

~0–250KB