Skip to main content

Enumable

Trait Enumable 

Source
pub trait Enumable: Copy + 'static {
    const VARIANTS: &'static [Self];
    const COUNT: usize = _;

    // Provided method
    fn variant_index(&self) -> usize { ... }
}
Expand description

A trait for enumerations that can be used with EnumTable.

This trait requires that the enumeration provides a static array of its variants and a constant representing the count of these variants.

§Safety

The implementations of this trait rely on the memory layout of the enum. It is strongly recommended to use a primitive representation (e.g., #[repr(u8)]) to ensure that the enum has no padding bytes and a stable layout.

Note on Padding: If the enum contains padding bytes (e.g., #[repr(u8, align(2))]), it will cause a compile-time error during constant evaluation, as Rust’s constant evaluator does not allow reading uninitialized memory (padding).

Required Associated Constants§

Source

const VARIANTS: &'static [Self]

Provided Associated Constants§

Source

const COUNT: usize = _

Provided Methods§

Source

fn variant_index(&self) -> usize

Returns the index of this variant in the sorted VARIANTS array.

When derived via #[derive(Enumable)], this is O(1) at runtime (using compile-time-computed constants). The default implementation falls back to O(log N) binary search for manual implementations.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§