Expand description
§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.
§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
Enumabletrait 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 new_with_fn method
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
assert_eq!(table.get(&Test::A), &"A");
assert_eq!(table.get(&Test::B), &"B");
assert_eq!(table.get(&Test::C), &"C");
// Alternatively, create an EnumTable using the et! macro
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
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
Modules§
Macros§
- et
- A macro to create an
EnumTablefor a given enumeration and value type.
Structs§
- Enum
Table - A table that associates each variant of an enumeration with a value.
Traits§
- Enumable
- A trait for enumerations that can be used with
EnumTable.