Skip to main content

WorldExt

Trait WorldExt 

Source
pub trait WorldExt {
    // Required methods
    fn bind_entity(&self, entity: Entity) -> BoundEntity<'_>;
    fn entity_ptr(&self, entity: Entity) -> EntityPtr;
}
Expand description

Extension trait for World providing ergonomic entity access methods.

This trait adds convenience methods to World that hide the internal unsafe boundary, making entity access more ergonomic.

§Example

use bevy_ecs::prelude::*;
use bevy_entity_ptr::WorldExt;

#[derive(Component)]
struct Health(i32);

fn my_system(world: &World, entity: Entity) {
    // No unsafe needed!
    let ptr = world.entity_ptr(entity);
    if let Some(health) = ptr.get::<Health>() {
        println!("Health: {}", health.0);
    }
}

Required Methods§

Source

fn bind_entity(&self, entity: Entity) -> BoundEntity<'_>

Creates a BoundEntity for scoped access with explicit lifetime.

This is the safest approach with compiler-checked lifetimes. Use when you want explicit lifetime tracking.

Source

fn entity_ptr(&self, entity: Entity) -> EntityPtr

Creates an EntityPtr for ergonomic traversal.

This hides the internal unsafe, providing a clean API for complex entity graph traversal. The EntityPtr is !Send, preventing escape to other threads.

§Safety Invariant

This method is safe to call within Bevy systems where &World is guaranteed to outlive the system scope. Using this method outside of a Bevy system (e.g., in a main() function with a locally-owned World) requires that the caller ensure the World outlives all EntityPtr instances created from it. Dropping the World while EntityPtr instances exist is undefined behavior.

For fully safe code without this invariant, use EntityHandle::bind() and BoundEntity instead.

Implementations on Foreign Types§

Source§

impl WorldExt for World

Source§

fn bind_entity(&self, entity: Entity) -> BoundEntity<'_>

Source§

fn entity_ptr(&self, entity: Entity) -> EntityPtr

Implementors§