lute
lute is a Rust library for immutable maps and sets built from fixed collections of up to 65535 (i.e. 2¹⁶ − 1) entries. It is designed with small sizes and use cases like lookup tables in mind. It is usable in a no_std environment by default.
Expected construction time is O(n), where $n$ is the number of entries, and worst-case query time is O(1).
See BENCHMARKS.md for performance comparisons to the phf crate.
Usage
use Map;
let planets = from;
assert_eq!;
assert_eq!;
use Set;
let primes = from;
assert!;
assert!;
Compile-time generation with macros
The map! and set! macros (enabled by the macros feature flag) build maps and sets at compile time. The result is an expression that can be used for a static or const. See the documentation of each macro for more details.
use ;
static PLANETS: = map! ;
assert_eq!;
assert_eq!;
use ;
static PRIMES: = set! ;
assert!;
assert!;
Compile-time generation with a build script
For entries that are not supported by the map! or set! macros, you can also construct maps and sets in a build script with the codegen feature flag enabled. Here is an example with Map:
[]
= { = "0.1.1", = false }
[]
= { = "0.1.1", = ["codegen"] }
In build.rs, build the map and write it to a file in OUT_DIR:
use Map;
use var_os;
use write;
use Path;
Then include the generated file anywhere in your code:
include!;
assert_eq!;
assert_eq!;
Reproducibility and portability
Embedded maps and sets are not necessarily stable across breaking versions and should be regenerated.
Keys must hash identically on the machine that builds the map and the target that runs it. Platform properties that can affect this include:
- Pointer width. Keys whose hash uses
usizeorisizeeither directly or via a length prefix (e.g. arrays, slices, byte strings, C strings) hash differently across targets of different pointer width. - Endianness. Arrays and slices of multibyte integers (e.g.
[u16; N],&[u32]) hash their raw native-endian bytes, so they hash differently across targets of different endianness.
Keys must also have consistent Hash and Eq: equal keys must hash equally and two keys that are distinct under Eq must not hash identically under every seed.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.