States

Derive Macro States 

Source
#[derive(States)]
Expand description

Derives the States trait for an enum.

This macro automatically implements the States trait along with Default, Copy, and Clone. The first variant of the enum becomes the default state.

§Requirements

  • Can only be derived for enums
  • All variants must be unit variants (no fields)

§Generated Implementations

  • States - Maps variants to/from indices starting at 0
  • Default - Uses the first variant as the default
  • Copy and Clone - Enables efficient copying

§Examples

use tinystate::States;

#[derive(States)]
enum DoorState {
    Closed,  // Index 0, also the default
    Open,    // Index 1
    Locked,  // Index 2
}

let state = DoorState::default();
assert_eq!(state.index(), 0);
assert!(matches!(state, DoorState::Closed));

§Panics

The derive macro will panic at compile time if:

  • Applied to a struct or union (not an enum)
  • Any variant has fields (only unit variants are allowed)

The generated from_index method will panic at runtime if called with an invalid index.