#lru-cache #key-value #linked-hash-map #order #linked-list #hash-key #container

no-std hashlink

HashMap-like containers that hold their key-value pairs in a user controllable order

17 releases (9 breaking)

new 0.10.0 Dec 9, 2024
0.9.1 May 12, 2024
0.9.0 Jan 9, 2024
0.8.4 Aug 28, 2023
0.3.0 Jul 12, 2019

#19 in Data structures

Download history 692376/week @ 2024-08-23 656711/week @ 2024-08-30 713561/week @ 2024-09-06 644744/week @ 2024-09-13 723780/week @ 2024-09-20 687673/week @ 2024-09-27 742776/week @ 2024-10-04 723186/week @ 2024-10-11 790547/week @ 2024-10-18 854994/week @ 2024-10-25 796611/week @ 2024-11-01 831619/week @ 2024-11-08 885313/week @ 2024-11-15 750082/week @ 2024-11-22 768383/week @ 2024-11-29 711316/week @ 2024-12-06

3,278,481 downloads per month
Used in 2,834 crates (66 directly)

MIT/Apache

99KB
3K SLoC

hashlink -- HashMap-like containers that hold their key-value pairs in a user controllable order

Build Status Latest Version API Documentation

This crate is a fork of linked-hash-map that builds on top of hashbrown to implement more up to date versions of LinkedHashMap LinkedHashSet, and LruCache.

One important API change is that when a LinkedHashMap is used as a LRU cache, it allows you to easily retrieve an entry and move it to the back OR produce a new entry at the back without needlessly repeating key hashing and lookups:

let mut lru_cache = LinkedHashMap::new();
let key = "key".to_owned();
// Try to find my expensive to construct and hash key
let _cached_val = match lru_cache.raw_entry_mut().from_key(&key) {
    RawEntryMut::Occupied(mut occupied) => {
        // Cache hit, move entry to the back.
        occupied.to_back();
        occupied.into_mut()
    }
    RawEntryMut::Vacant(vacant) => {
        // Insert expensive to construct key and expensive to compute value,
        // automatically inserted at the back.
        vacant.insert(key.clone(), 42).1
    }
};

Or, a simpler way to do the same thing:

let mut lru_cache = LinkedHashMap::new();
let key = "key".to_owned();
let _cached_val = lru_cache
    .raw_entry_mut()
    .from_key(&key)
    .or_insert_with(|| (key.clone(), 42));

This crate contains a decent amount of unsafe code from handling its internal linked list, and the unsafe code has diverged quite a lot from the original linked-hash-map implementation. It currently passes tests under miri and sanitizers, but it should probably still receive more review and testing, and check for test code coverage.

Credit

There is a huge amount of code in this crate that is copied verbatim from linked-hash-map and hashbrown, especially tests, associated types like iterators, and things like Debug impls.

License

This library is licensed the same as linked-hash-map and hashbrown, it is licensed under either of:

at your option.

Dependencies

~0.5–0.8MB
~13K SLoC