Events

Derive Macro Events 

Source
#[derive(Events)]
Expand description

Derives the Events trait for an enum.

This macro automatically implements the Events trait along with Copy and Clone. Unlike States, this does not generate a Default implementation.

§Requirements

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

§Generated Implementations

  • Events - Maps variants to/from indices starting at 0
  • Copy and Clone - Enables efficient copying

§Examples

use tinystate::Events;

#[derive(Events)]
enum DoorEvent {
    Push,   // Index 0
    Pull,   // Index 1
    Lock,   // Index 2
    Unlock, // Index 3
}

let event = DoorEvent::Lock;
assert_eq!(event.index(), 2);

let reconstructed = DoorEvent::from_index(2);
assert_eq!(reconstructed.index(), event.index());

§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.