bevy_enum_event
General-purpose enum to Bevy event conversion macro.
Overview
bevy_enum_event provides a derive macro that automatically generates Bevy event types from enum variants. For each variant in your enum, it creates a corresponding event struct organized in a snake_case module. Supports unit variants, tuple variants, and named field variants.
Bevy Compatibility
| Bevy | bevy_enum_event |
|---|---|
| 0.17 | - |
| 0.16 | 0.1 |
Features
- Automatic event generation: One macro generates all variant events
- Support for data-carrying variants: Enum variants can contain data (tuple or named fields)
- Snake case module:
PlayerState→player_statemodule - Zero boilerplate: No manual event struct definitions needed
- Type-safe: Each variant gets its own distinct event type
- Generic-friendly: Works with lifetimes, generic parameters, and
whereclauses - Bevy integration: Generated events work seamlessly with Bevy's observer system
- Deref support (optional): Automatic
DerefandDerefMutfor single-field variants, or multi-field variants when you tag one field with#[enum_event(deref)]
Installation
[]
= "0.1"
Quick Start
Unit Variants
use *;
use EnumEvent;
This automatically generates:
Variants with Data
use *;
use EnumEvent;
This generates:
Usage with Bevy Observers
Basic Example
use *;
use EnumEvent;
With Event Data
use *;
use EnumEvent;
Feature: deref (enabled by default)
The deref feature automatically derives Bevy's Deref and DerefMut traits for enum variants with a single field (either tuple or named), providing ergonomic access to the inner value. For multi-field variants, you can opt into the same ergonomic access by adding #[enum_event(deref)] to the field you want to expose (the generated struct receives Bevy's #[deref] attribute). If a multi-field variant is not annotated with #[enum_event(deref)], no deref functionality is generated and fields must be accessed directly by name.
Example
use *;
use EnumEvent;
Disabling the deref Feature
If you prefer not to have Deref and DerefMut automatically implemented, you can disable the default features:
[]
= { = "0.1", = false }
When disabled, you'll need to access fields directly:
Snake Case Conversion
The macro intelligently converts enum names to snake_case module names:
LifeFSM→life_fsmPlayerState→player_stateHTTPServer→http_serverMyHTTPSConnection→my_https_connection
Generics & Lifetimes
All derives mirror the generic parameters, lifetimes, and where clauses from your enum onto the generated
event structs. This makes it straightforward to use EnumEvent with enums such as:
The generated module exposes generic_event::Borrowed<'a, T>, generic_event::Owned<'a, T>, and
generic_event::Done<'a, T> types with identical bounds.
Unit event structs expose ergonomic constructors so you never have to juggle hidden PhantomData
markers by hand. Every unit variant implements Default, and when a phantom marker is required the
derive also emits a new() helper that seeds it for you. Tuple and named variants that require
phantom markers likewise receive new(...) helpers that accept only the original fields.
Use Cases
- State machines (see bevy_fsm)
- Game state transitions
- Entity lifecycle events
- Animation states
- Input modes
- Any enum-based event system
- Network message events
- Input/Output events with associated data
AI Disclaimer
- Refactoring and documentation supported by Claude Code
- Minor editing supported by ChatGPT Codex
- The process and final releases are thoroughly supervised and checked by the author
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.