iddqd
Maps where keys are borrowed from values.
This crate consists of several map types, collectively called ID maps:
IdOrdMap: A B-Tree based map where keys are borrowed from values.IdHashMap: A hash map where keys are borrowed from values.BiHashMap: A bijective (1:1) hash map with two keys, borrowed from values.TriHashMap: A trijective (1:1:1) hash map with three keys, borrowed from values.
Usage
- Pick your ID map type.
- Depending on the ID map type, implement
IdOrdItem,IdHashItem,BiHashItem, orTriHashItemfor your value type. - Store values in the ID map type.
Features
This crate was built out a practical need for map types, and addresses issues encountered using Rust’s default map types in practice at Oxide.
- Keys are retrieved from values, not stored separately from them. Separate storage has been a recurring pain point in our codebases: if keys are duplicated within values, it’s proven to be hard to maintain consistency between keys and values. This crate addresses that need.
- Keys may be borrowed from values, which allows for more flexible implementations. (They don’t have to be borrowed, but they can be.)
- There’s no
insertmethod; insertion must be through eitherinsert_overwriteorinsert_unique. You must pick an insertion behavior. - The serde implementations reject duplicate keys.
We’ve also sometimes needed to index a set of data by more than one key, or
perhaps map one key to another. For that purpose, this crate provides
BiHashMap and TriHashMap.
BiHashMaphas two keys, and provides a bijection (1:1 relationship) between the keys.TriHashMaphas three keys, and provides a trijection (1:1:1 relationship) between the keys.
As a consequence of the general API structure, maps can have arbitrary non-key data associated with them as well.
Examples
An example for IdOrdMap:
use ;
// Implement IdOrdItem so the map knows how to get the key from the value.
let mut users = new;
// You must pick an insertion behavior. insert_unique returns an error if
// the key already exists.
users.insert_unique.unwrap;
users.insert_unique.unwrap;
// Lookup by name:
assert_eq!;
assert_eq!;
// Iterate over users:
for user in &users
An example for IdHashMap, showing complex borrowed keys.
use ;
// The key type is a borrowed form of the name and version. It needs to
// implement `Hash + Eq`.
let mut artifacts = new;
// Add artifacts to the map.
artifacts.insert_unique
.unwrap;
artifacts.insert_unique
.unwrap;
// Look up artifacts by name and version.
assert_eq!;
Testing
This crate is validated through a combination of:
- Unit tests
- Property-based tests using a naive map as an oracle
- Chaos tests for several kinds of buggy
EqandOrdimplementations - Miri tests for unsafe code
If you see a gap in testing, new tests are welcome. Thank you!
No-std compatibility
Most of this crate is no-std compatible, though alloc is required.
The IdOrdMap type is not currently no-std compatible due to its use of a
thread-local. This thread-local is just a way to work around a limitation in
std’s BTreeMap API, though. Either a custom B-Tree implementation, or a
platform-specific notion of thread locals, would suffice to make
IdOrdMap no-std compatible.
Optional features
serde: Enables serde support for all ID map types. Not enabled by default.daft: Enablesdaftsupport for all ID map types. Not enabled by default.std: Enables std support. Enabled by default.
Related work
bimapprovides a bijective map, but does not have a way to associate arbitrary values with each pair of keys. However, it does support an ordered map type without the need for std.
Minimum supported Rust version (MSRV)
This crate’s MSRV is Rust 1.81. In general we aim for 6 months of Rust compatibility.
License
This project is available under the terms of either the Apache 2.0 license or the MIT license.
Portions adapted from The Rust Programming Language and used under the MIT and Apache 2.0 licenses. The Rust Programming Language is (c) The Rust Project Contributors.