Skip to main content

Crate bevy_expected_components

Crate bevy_expected_components 

Source
Expand description

§bevy_expected_components

Runtime validation for Bevy component dependencies. Like #[require] but validates instead of auto-inserting.

§Why Use This?

Bevy’s #[require(T)] automatically inserts missing components using Default. This doesn’t work when:

  • Required components need world access to construct
  • You want bugs to surface immediately rather than silently using defaults
  • The required component has no sensible default

#[expects(T)] solves this by panicking if expected components are missing at insert time, making bugs immediately visible during development.

§Performance Warning

This crate adds runtime overhead: every time a component with #[expects(...)] is inserted, the plugin checks that all expected components exist on the entity.

Recommended usage: Enable only in development and test builds.

ⓘ
// Only add the plugin in debug builds
#[cfg(debug_assertions)]
app.add_plugins(ExpectedComponentsPlugin);

§Example

ⓘ
use bevy::prelude::*;
use bevy_expected_components::prelude::*;

#[derive(Component, Default)]
struct Transform;

#[derive(Component, Default)]
struct Velocity;

// PhysicsBody expects Transform and Velocity to exist when it's inserted
#[derive(Component, ExpectComponents)]
#[expects(Transform, Velocity)]
struct PhysicsBody;

fn main() {
    let mut app = App::new();

    // Enable validation (only in debug builds recommended)
    #[cfg(debug_assertions)]
    app.add_plugins(ExpectedComponentsPlugin);

    // This works - all expected components present
    app.world_mut().spawn((PhysicsBody, Transform, Velocity));

    // This panics - Velocity is missing!
    // app.world_mut().spawn((PhysicsBody, Transform));
}

§How It Works

  1. #[derive(ExpectComponents)] generates an ExpectComponents trait implementation
  2. The derive macro registers the type with inventory at compile time
  3. ExpectedComponentsPlugin iterates all registered types and installs on_add hooks
  4. When a component is inserted, the hook validates expected components exist

§Comparison with #[require]

Feature#[require]#[expects]
Missing componentAuto-inserted with DefaultPanics
Requires DefaultYesNo
Runtime costArchetype lookupComponent existence check
Use caseConvenience bundlesBug detection

§Limitations

Validates insertion only, not removal. If you later remove an expected component from an entity, no error occurs. This keeps the implementation simple and covers the main use case: catching mistakes at spawn time.

§Future of This Crate

This crate may become unnecessary when Bevy adds native support for non-defaultable required components. Relevant discussions:

Modules§

prelude
Prelude module for convenient imports.

Structs§

ExpectRegistration
Registration entry for a component with expectations.
ExpectedComponentsPlugin
Plugin that enables runtime validation of component expectations.

Traits§

ExpectComponents
Trait implemented by components that expect other components to be present.

Derive Macros§

ExpectComponents
Derive macro for generating ExpectComponents implementation.