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
impl World
Sourcepub fn spawn(&mut self) -> EntityBuilder<'_>
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.
Sourcepub fn is_alive(&self, entity: Entity) -> bool
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)
Sourcepub fn destroy(&mut self, entity: Entity) -> bool
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.
Sourcepub fn insert<C>(&mut self, entity: Entity, component: C)where
C: Component,
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.
Sourcepub fn remove<C>(&mut self, entity: Entity) -> boolwhere
C: Component,
pub fn remove<C>(&mut self, entity: Entity) -> boolwhere
C: Component,
Removes a component from an entity
Returns true if the component was removed, false if not found.
Sourcepub fn get<C>(&self, entity: Entity) -> Option<&C>where
C: Component,
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
Sourcepub fn get_mut<C>(&mut self, entity: Entity) -> Option<&mut C>where
C: Component,
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
Sourcepub fn has<C>(&self, entity: Entity) -> boolwhere
C: Component,
pub fn has<C>(&self, entity: Entity) -> boolwhere
C: Component,
Checks if an entity has a component of the given type
Sourcepub fn has_tag(&self, entity: Entity, tag: &str) -> bool
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.
Sourcepub fn tag(&mut self, entity: Entity, tag: &str)
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.
Sourcepub fn untag(&mut self, entity: Entity, tag: &str)
pub fn untag(&mut self, entity: Entity, tag: &str)
Removes a tag from an entity by clearing its bit in the bitmask
Returns all tag names associated with an entity
Primarily used for debugging and inspection.
Sourcepub fn select_mut<T>(&mut self) -> QueryMut<'_, T>where
T: for<'w> QueryTupleMut<'w>,
pub fn select_mut<T>(&mut self) -> QueryMut<'_, T>where
T: for<'w> QueryTupleMut<'w>,
Sourcepub fn apply_deferred(&mut self)
pub fn apply_deferred(&mut self)
Executes all queued deferred operations
Called automatically each frame by apply_deferred_system.
Sourcepub fn entity_count(&self) -> usize
pub fn entity_count(&self) -> usize
Returns the number of alive entities
Sourcepub fn dump_all_memory(&self)
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.
Sourcepub fn inspect(&self)
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.