enum-table
enum-table is a lightweight and efficient Rust library for mapping enums to values.
It provides a fast, type-safe, and allocation-free alternative to using HashMap
for enum keys,
with compile-time safety and constant-time access.
Features
- Type Safety: Only valid enum variants can be used as keys.
- Compile-Time Checks: Leverages Rust's type system for compile-time guarantees.
- Efficiency: Constant-time access, no heap allocation.
- Custom Derive: Procedural macro to automatically implement the
Enumable
trait for enums. - Const Support: Tables can be constructed at compile time.
Usage
// Enabled by default
use ;
// #[derive(Enumable)] // Recommended for automatic implementation Enumable trait
// Optional, but recommended for explicit discriminants
// Implementing the Enumable trait manually
// May forget to add all variants. Use derive macro instead. (This is README example)
> =
et!;
// Accessing values from the compile-time table
const A: &str = TABLE.get;
assert_eq!;
// Runtime table creation
let mut table = COUNT }> new_with_fn;
assert_eq!;
// This call does not panic and is not wrapped in Result or Option
// always return old value, because all enum variants are initialized
let old_b = table.set;
assert_eq!;
assert_eq!;
}
More Method
more info: doc.rs
License
Licensed under