Open-Source Wikis

/

Bevy

/

Packages

/

bevy_derive

bevyengine/bevy

bevy_derive

Small, general-purpose derive macros that aren't tied to a specific subsystem.

Purpose

Three derives that show up across the workspace:

  • #[derive(Deref)] — implement Deref to the first field of a tuple struct.
  • #[derive(DerefMut)] — implement DerefMut similarly.
  • #[derive(EnumVariantMeta)] — generate a method that returns variant indices/names.

Plus a few smaller helpers used by Bevy's macros internally.

Directory layout

crates/bevy_derive/src/
├── lib.rs                # Re-exports
├── derefs.rs             # Deref / DerefMut
├── enum_variant_meta.rs  # EnumVariantMeta
└── bevy_main.rs          # The #[bevy_main] attribute (Android entry point)

Key abstractions

Macro File Description
Deref crates/bevy_derive/src/derefs.rs Auto-impl Deref for a newtype wrapping a single field.
DerefMut crates/bevy_derive/src/derefs.rs Auto-impl DerefMut similarly.
EnumVariantMeta crates/bevy_derive/src/enum_variant_meta.rs Adds enum_variant_index / enum_variant_name methods.
#[bevy_main] crates/bevy_derive/src/bevy_main.rs Attribute on main to register the Android entry point.

How it's used

#[derive(Deref, DerefMut)]
struct Velocity(Vec3);

let v = Velocity(Vec3::ZERO);
let _len = v.length(); // Vec3::length, accessed through Deref

Most Bevy newtypes use this pattern — RenderLayers, Time<Fixed>, many resource types. It avoids hand-writing Deref/DerefMut impls and keeps the wrapped type accessible.

Integration points

  • Depends on: proc-macro2, quote, syn, bevy_macro_utils.
  • Depended on by: Many crates that wrap a single field. Re-exported through bevy::prelude::*.

Entry points for modification

  • The crate is small; it is unlikely to need many changes. New general-purpose derives can be added here.
  • Compile-fail tests under crates/bevy_derive/compile_fail/ lock in error messages — update them when changing the macros.

See also

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

bevy_derive – Bevy wiki | Factory