9 releases
new 0.3.2 | May 25, 2025 |
---|---|
0.3.1 | May 24, 2025 |
0.2.2 | Mar 17, 2025 |
0.1.3 | Mar 1, 2025 |
0.1.2 | Feb 27, 2025 |
#698 in Data structures
Used in 2 crates
27KB
446 lines
enum-table: 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
#![cfg(feature = "derive")] // Enabled by default
use enum_table::{EnumTable, Enumable};
// #[derive(Enumable)] // Recommended for automatic implementation Enumable trait
#[repr(u8)] // Optional, but recommended for explicit discriminants
enum Test {
A = 100,
B = 1,
C = 34,
}
// Implementing the Enumable trait manually
// May forget to add all variants. Use derive macro instead. (This is README example)
impl Enumable for Test {
const VARIANTS: &'static [Self] = &[Self::A, Self::B, Self::C];
}
fn main() {
// Compile-time table creation using the et! macro
static TABLE: EnumTable<Test, &'static str, { Test::COUNT }> =
enum_table::et!(Test, &'static str, |t| match t {
Test::A => "A",
Test::B => "B",
Test::C => "C",
});
// Accessing values from the compile-time table
const A: &str = TABLE.get(&Test::A);
assert_eq!(A, "A");
// Runtime table creation
let mut table = EnumTable::<Test, &'static str, { Test::COUNT }>::new_with_fn(
|t| match t {
Test::A => "A",
Test::B => "B",
Test::C => "C",
});
assert_eq!(table.get(&Test::A), &"A");
// 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(&Test::B, "Changed B");
assert_eq!(old_b, "B");
assert_eq!(table.get(&Test::B), &"Changed B");
}
More Method
more info: doc.rs
License
Licensed under
Dependencies
~94KB