World

Struct World 

Source
pub struct World { /* private fields */ }
Expand description

ECS World containing all entities, components and tags

§Tag System

Tags are lightweight string labels stored as bitmasks (u128).

  • Up to 128 unique tags globally
  • O(1) tag filtering in queries
  • Tags are not components (no storage overhead per entity)

§Deferred Operations

Use world.defer() to queue operations that modify the World during iteration. Applied via apply_deferred() at frame end.

§Example

let mut world = World::new();

// Create entity with components and tags
let player = world.spawn()
    .insert(Position { x: 0.0, y: 0.0 })
    .insert(Velocity { x: 1.0, y: 0.0 })
    .tag("player")
    .tag("friendly")
    .id();

// Immutable query
world.select::<(Position, Velocity)>()
    .tagged("player")
    .each(|entity, (pos, vel)| {
        println!("{}: {:?}", entity, pos);
    });

// Mutable query
world.select_mut::<(Position, Velocity)>()
    .not_tagged("frozen")
    .each(|entity, (pos, vel)| {
        pos.x += vel.x;
        pos.y += vel.y;
    });

Implementations§

Source§

impl World

Source

pub fn new() -> World

Creates a new empty World

Source

pub fn spawn(&mut self) -> EntityBuilder<'_>

Starts building a new entity

Returns an EntityBuilder for fluent API chaining. Entity IDs are reused from a free list after destruction.

Source

pub fn is_alive(&self, entity: Entity) -> bool

Checks if an entity is alive and matches the given generation

Returns false if:

  • Entity ID not in alive set
  • Generation mismatch (entity was destroyed and ID reused)
Source

pub fn destroy(&mut self, entity: Entity) -> bool

Destroys an entity and removes all its components and tags

The entity ID is added to a free list for reuse. The generation counter is increased to invalidate old references.

Returns true if the entity was alive, false otherwise.

Source

pub fn insert<C>(&mut self, entity: Entity, component: C)
where C: Component,

Inserts a component into an entity

If the entity already has this component type, it will be replaced. Does nothing if the entity is not alive.

Source

pub fn remove<C>(&mut self, entity: Entity) -> bool
where C: Component,

Removes a component from an entity

Returns true if the component was removed, false if not found.

Source

pub fn get<C>(&self, entity: Entity) -> Option<&C>
where C: Component,

Gets an immutable reference to a component

Returns None if:

  • Entity is not alive
  • Entity doesn’t have this component type
Source

pub fn get_mut<C>(&mut self, entity: Entity) -> Option<&mut C>
where C: Component,

Gets a mutable reference to a component

Returns None if:

  • Entity is not alive
  • Entity doesn’t have this component type
Source

pub fn has<C>(&self, entity: Entity) -> bool
where C: Component,

Checks if an entity has a component of the given type

Source

pub fn has_tag(&self, entity: Entity, tag: &str) -> bool

Checks if an entity has a specific tag

This is O(1) after the tag name lookup.

Source

pub fn tag(&mut self, entity: Entity, tag: &str)

Attaches a tag to an entity

Tags are stored as bitmasks for fast filtering in queries. Does nothing if the entity is not alive.

§Panics

Panics if more than 128 unique tags are created globally.

Source

pub fn untag(&mut self, entity: Entity, tag: &str)

Removes a tag from an entity by clearing its bit in the bitmask

Source

pub fn get_entity_tags(&self, entity_id: u32) -> Vec<String>

Returns all tag names associated with an entity

Primarily used for debugging and inspection.

Source

pub fn select<T>(&self) -> Query<'_, T>
where T: for<'w> QueryTuple<'w>,

Starts an immutable query

§Example
world.select::<(Position, Velocity)>()
    .tagged("player")
    .each(|entity, (pos, vel)| {
        println!("{:?}", pos);
    });
Source

pub fn select_mut<T>(&mut self) -> QueryMut<'_, T>
where T: for<'w> QueryTupleMut<'w>,

Starts a mutable query

§Example
world.select_mut::<(Position, Velocity)>()
    .not_tagged("frozen")
    .each(|entity, (pos, vel)| {
        pos.x += vel.x;
    });
Source

pub fn defer<F>(&mut self, f: F)
where F: FnOnce(&mut World) + Send + Sync + 'static,

Queues an operation to be executed later via apply_deferred()

Useful for spawning/destroying entities during query iteration.

§Example
world.select::<(Health,)>().each(|entity, (health,)| {
    if health.0 <= 0 {
        world.defer(move |w| {
            w.destroy(entity);
        });
    }
});
world.apply_deferred();
Source

pub fn apply_deferred(&mut self)

Executes all queued deferred operations

Called automatically each frame by apply_deferred_system.

Source

pub fn entity_count(&self) -> usize

Returns the number of alive entities

Source

pub fn dump_all_memory(&self)

Prints physical memory layout of all component storages (debug only)

Shows memory addresses and byte offsets to verify:

  • Components are stored contiguously
  • Cache-friendly memory access patterns

Only enabled in debug builds to avoid I/O overhead.

Source

pub fn inspect(&self)

Prints a formatted table of all entities and their components (debug only)

Shows:

  • Entity IDs and generations
  • Attached tags
  • Component values

Useful for debugging game state and verifying entity configurations. Only enabled in debug builds to avoid string formatting overhead.

Trait Implementations§

Source§

impl Default for World

Source§

fn default() -> World

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for World

§

impl !RefUnwindSafe for World

§

impl Send for World

§

impl Sync for World

§

impl Unpin for World

§

impl !UnwindSafe for World

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> Event for T
where T: Any + Send + Sync + 'static,