6 releases
new 0.2.1 | Mar 10, 2025 |
---|---|
0.2.0 | Mar 1, 2025 |
0.1.3 | Mar 1, 2025 |
0.1.2 | Feb 27, 2025 |
#636 in Data structures
605 downloads per month
Used in 2 crates
17KB
213 lines
enum-table: enum-table
enum-table
is a lightweight and efficient Rust library designed for mapping enums to values.
It provides a fast and type-safe alternative to using HashMap
for enum keys,
ensuring compile-time safety and performance benefits.
You can create an EnumTable struct at const time, ensuring that values are returned reliably with get, and there is no runtime check.
Features
- Type Safety: Ensures that only valid enum variants are used as keys.
- Compile-Time Checks: Leverages Rust's type system to provide compile-time guarantees.
- Efficiency: Offers constant-time access to values associated with enum variants.
- Custom Derive: Includes a procedural macro to automatically implement the
Enumable
trait for enums.
Usage
Below is a complete example demonstrating how to define an enum, create an EnumTable
,
and access values. This example is designed to be run as a single doctest.
#![cfg(feature = "derive")]
use enum_table::{EnumTable, Enumable};
// Define an enum and derive the Enumable trait
// #[derive(Enumable)]
#[repr(u8)]
enum Test {
// ok with discriminant
A = 100,
B = 1,
C = 34,
}
// Equivalent code not using `#[derive(enum_table::Enumable)]`
impl Enumable for Test {
const VARIANTS: &[Self] = &[Self::A, Self::B, Self::C];
// The COUNT constant is automatically implemented by default when VARIANTS is implemented.
// const COUNT: usize = Self::VARIANTS.len();
}
fn main() {
// Create an EnumTable using the et! macro at compile time.
// This allows for constant-time access to values associated with enum variants.
const TABLE: enum_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",
});
// Access values using the constant table.
// The get method retrieves the value associated with the given enum variant.
const A: &'static str = TABLE.get(&Test::A);
const B: &'static str = TABLE.get(&Test::B);
const C: &'static str = TABLE.get(&Test::C);
assert_eq!(A, "A");
assert_eq!(B, "B");
assert_eq!(C, "C");
// Alternatively, create an EnumTable using the new_with_fn method at runtime.
// This method allows for dynamic initialization of the table.
let mut table = enum_table::EnumTable::<Test, &'static str, { Test::COUNT }>::new_with_fn(|t| match t {
Test::A => "A",
Test::B => "B",
Test::C => "C",
});
// Access values associated with enum variants using the runtime-initialized table.
// The get method guarantees that a value is always returned without runtime checks,
// ensuring safety once the EnumTable struct is created.
assert_eq!(table.get(&Test::A), &"A");
assert_eq!(table.get(&Test::B), &"B");
assert_eq!(table.get(&Test::C), &"C");
}
More Method
more info: doc.rs
License
Licensed under
Dependencies
~96KB