pub struct World { /* private fields */ }Expand description
The ECS world, managing entities and their components.
§Fields
entity_allocator: Allocator tracking entity ids and generations.storages: Type-erased component storages keyed by theirTypeId.resource_registry: Type-erased registry for arbitrary resources (including unique ones).
§Safety
- All public APIs enforce Rust’s borrowing rules across component storages.
- Type erasure is limited to internal maps; downcasting is guarded by
TypeIdchecks in accessor methods to prevent type confusion.
Implementations§
Source§impl World
impl World
Sourcepub fn purge(&mut self)
pub fn purge(&mut self)
Purges the world: removes all components and resets the entity allocator. After purge, no entities are alive and the next created entity will start from id 0 with generation 0.
§Effects
- Clears all component storages and resources.
- Resets the entity allocator (no free ids, no generations).
§Notes
- Any previously held
Entityvalues are invalid after purge.
Sourcepub fn entity_count(&self) -> usize
pub fn entity_count(&self) -> usize
Sourcepub fn free_id_count(&self) -> usize
pub fn free_id_count(&self) -> usize
Returns the number of IDs ready to be recycled.
§Returns
usize: Count of free IDs currently available for reuse.
Sourcepub fn create_entity(&mut self) -> Entity
pub fn create_entity(&mut self) -> Entity
Sourcepub fn create_entity_and_add_component<T: Component>(
&mut self,
component: T,
) -> Entity
pub fn create_entity_and_add_component<T: Component>( &mut self, component: T, ) -> Entity
Sourcepub fn destroy_entity(&mut self, entity: Entity)
pub fn destroy_entity(&mut self, entity: Entity)
Destroys an entity, marking it dead and removing all components associated with it.
§Parameters
entity: The entity to destroy. Safe to call multiple times; additional calls are no-ops.
Sourcepub fn add_component<T: Component>(&mut self, entity: Entity, component: T)
pub fn add_component<T: Component>(&mut self, entity: Entity, component: T)
Adds a component of type T to an entity, creating storage if necessary.
§Type Parameters
T: The component type to insert. Must implementComponentand be'static.
§Parameters
entity: The target entity to receive the component.component: The component instance to add (replaces existing component ofTon this entity).
§Effects
- Creates the component storage for
Tif it does not yet exist. - If the entity already has a
T, it is replaced with the new value.
Sourcepub fn entity_has_component<T: Component>(&self, entity: Entity) -> bool
pub fn entity_has_component<T: Component>(&self, entity: Entity) -> bool
Sourcepub fn get_component_mut<T: Component>(
&mut self,
entity: Entity,
) -> Option<&mut T>
pub fn get_component_mut<T: Component>( &mut self, entity: Entity, ) -> Option<&mut T>
Retrieves a mutable reference to a component of type T for an entity.
§Type Parameters
T: The component type to retrieve mutably.
§Parameters
entity: The entity whose component is being requested mutably.
§Returns
Option<&mut T>:Some(&mut T)if the entity currently has aTcomponent;Noneotherwise.
§Notes
- Borrowing rules apply across all storages; this method requires
&mut selfto ensure mutable aliasing rules are respected.
Sourcepub fn get_all_components_with_entities<T: Component>(
&self,
) -> Vec<(Entity, &T)>
pub fn get_all_components_with_entities<T: Component>( &self, ) -> Vec<(Entity, &T)>
Returns all components of type T paired with their owning entities.
§Type Parameters
T: The component type to iterate over.
§Returns
Vec<(Entity, &T)>: A vector of(Entity, &T)pairs for everyTstored.
§Ordering
- Follows the dense storage order of
ComponentStorage<T>, which is generally insertion order preserved via swap-remove when deletions occur.
Sourcepub fn get_all_components_with_entities_mut<T: Component>(
&mut self,
) -> Vec<(Entity, &mut T)>
pub fn get_all_components_with_entities_mut<T: Component>( &mut self, ) -> Vec<(Entity, &mut T)>
Returns all components of type T mutably paired with their owning entities.
§Type Parameters
T: The component type to iterate over mutably.
§Returns
Vec<(Entity, &mut T)>: A vector of(Entity, &mut T)pairs for everyTstored.
§Ordering
- Follows the dense storage order of
ComponentStorage<T>;
§Notes
- Requires
&mut selfto guarantee sound mutable access to components.
Sourcepub fn get_entities_for<T: Component>(&self) -> Vec<Entity>
pub fn get_entities_for<T: Component>(&self) -> Vec<Entity>
Sourcepub fn get_entities_with_3<T1: Component, T2: Component, T3: Component>(
&self,
) -> Vec<Entity>
pub fn get_entities_with_3<T1: Component, T2: Component, T3: Component>( &self, ) -> Vec<Entity>
Sourcepub fn get_entities_with_4<T1: Component, T2: Component, T3: Component, T4: Component>(
&self,
) -> Vec<Entity>
pub fn get_entities_with_4<T1: Component, T2: Component, T3: Component, T4: Component>( &self, ) -> Vec<Entity>
Sourcepub fn get_entities_with_5<T1: Component, T2: Component, T3: Component, T4: Component, T5: Component>(
&self,
) -> Vec<Entity>
pub fn get_entities_with_5<T1: Component, T2: Component, T3: Component, T4: Component, T5: Component>( &self, ) -> Vec<Entity>
Sourcepub fn get_entities_and_components_with_2<T1: Component, T2: Component>(
&self,
) -> Vec<(Entity, &T1, &T2)>
pub fn get_entities_and_components_with_2<T1: Component, T2: Component>( &self, ) -> Vec<(Entity, &T1, &T2)>
Sourcepub fn get_entities_and_components_with_3<T1: Component, T2: Component, T3: Component>(
&self,
) -> Vec<(Entity, &T1, &T2, &T3)>
pub fn get_entities_and_components_with_3<T1: Component, T2: Component, T3: Component>( &self, ) -> Vec<(Entity, &T1, &T2, &T3)>
Returns entities with components T1, T2, and T3 and their references.
§Type Parameters
T1: First component type.T2: Second component type.T3: Third component type.
§Returns
Vec<(Entity, &T1, &T2, &T3)>: Tuples of the entity and three component references.
§Ordering
- The iteration order follows the dense storage of
T1.
Sourcepub fn get_entities_and_components_with_4<T1: Component, T2: Component, T3: Component, T4: Component>(
&self,
) -> Vec<(Entity, &T1, &T2, &T3, &T4)>
pub fn get_entities_and_components_with_4<T1: Component, T2: Component, T3: Component, T4: Component>( &self, ) -> Vec<(Entity, &T1, &T2, &T3, &T4)>
Returns entities with components T1, T2, T3, and T4 and their references.
§Type Parameters
T1: First component type.T2: Second component type.T3: Third component type.T4: Fourth component type.
§Returns
Vec<(Entity, &T1, &T2, &T3, &T4)>: Tuples of the entity and four component references.
§Ordering
- The iteration order follows the dense storage of
T1.
Sourcepub fn get_entities_and_components_with_5<T1: Component, T2: Component, T3: Component, T4: Component, T5: Component>(
&self,
) -> Vec<(Entity, &T1, &T2, &T3, &T4, &T5)>
pub fn get_entities_and_components_with_5<T1: Component, T2: Component, T3: Component, T4: Component, T5: Component>( &self, ) -> Vec<(Entity, &T1, &T2, &T3, &T4, &T5)>
Returns entities with components T1, T2, T3, T4, and T5 and their references.
§Type Parameters
T1: First component type.T2: Second component type.T3: Third component type.T4: Fourth component type.T5: Fifth component type.
§Returns
Vec<(Entity, &T1, &T2, &T3, &T4, &T5)>: Tuples of the entity and five component references.
§Ordering
- The iteration order follows the dense storage of
T1.
Sourcepub fn get_components_with_2<T1: Component, T2: Component>(
&self,
) -> (Vec<&T1>, Vec<&T2>)
pub fn get_components_with_2<T1: Component, T2: Component>( &self, ) -> (Vec<&T1>, Vec<&T2>)
Returns only components for entities that have both components, aligned by entity.
§Type Parameters
T1: First component type.T2: Second component type.
§Returns
(Vec<&T1>, Vec<&T2>): Two vectors with matching indices corresponding to the same entity.
§Ordering
- The ordering follows the dense storage order of
T1filtered by presence inT2.
Sourcepub fn get_components_with_3<T1: Component, T2: Component, T3: Component>(
&self,
) -> (Vec<&T1>, Vec<&T2>, Vec<&T3>)
pub fn get_components_with_3<T1: Component, T2: Component, T3: Component>( &self, ) -> (Vec<&T1>, Vec<&T2>, Vec<&T3>)
Returns only components for entities that have all three components, aligned by entity.
§Type Parameters
T1: First component type.T2: Second component type.T3: Third component type.
§Returns
(Vec<&T1>, Vec<&T2>, Vec<&T3>): Three vectors with matching indices for the same entities.
§Ordering
- The ordering follows the dense storage of
T1filtered by entities also havingT2andT3.
Sourcepub fn get_components_with_4<T1: Component, T2: Component, T3: Component, T4: Component>(
&self,
) -> (Vec<&T1>, Vec<&T2>, Vec<&T3>, Vec<&T4>)
pub fn get_components_with_4<T1: Component, T2: Component, T3: Component, T4: Component>( &self, ) -> (Vec<&T1>, Vec<&T2>, Vec<&T3>, Vec<&T4>)
Returns only components for entities that have all four components, aligned by entity.
§Type Parameters
T1: First component type.T2: Second component type.T3: Third component type.T4: Fourth component type.
§Returns
(Vec<&T1>, Vec<&T2>, Vec<&T3>, Vec<&T4>): Four vectors with matching indices for the same entities.
§Ordering
- The ordering follows the dense storage of
T1filtered by entities also havingT2,T3, andT4.
Sourcepub fn get_components_with_5<T1: Component, T2: Component, T3: Component, T4: Component, T5: Component>(
&self,
) -> (Vec<&T1>, Vec<&T2>, Vec<&T3>, Vec<&T4>, Vec<&T5>)
pub fn get_components_with_5<T1: Component, T2: Component, T3: Component, T4: Component, T5: Component>( &self, ) -> (Vec<&T1>, Vec<&T2>, Vec<&T3>, Vec<&T4>, Vec<&T5>)
Returns only components for entities that have all five components, aligned by entity.
§Type Parameters
T1: First component type.T2: Second component type.T3: Third component type.T4: Fourth component type.T5: Fifth component type.
§Returns
(Vec<&T1>, Vec<&T2>, Vec<&T3>, Vec<&T4>, Vec<&T5>): Five vectors with matching indices for the same entities.
§Ordering
- The ordering follows the dense storage of
T1filtered by entities also havingT2,T3,T4, andT5.
Sourcepub fn get_all_components<T: Component>(&self) -> Vec<&T>
pub fn get_all_components<T: Component>(&self) -> Vec<&T>
Sourcepub fn get_all_components_mut<T: Component>(&mut self) -> Vec<&mut T>
pub fn get_all_components_mut<T: Component>(&mut self) -> Vec<&mut T>
Returns mutable references to all components of type T currently stored in the world.
§Type Parameters
T: The component type to retrieve mutably.
§Returns
Vec<&mut T>: All components of typeTas mutable references. Empty if no storage exists.
§Ordering
- Follows the dense storage order of
ComponentStorage<T>.
§Notes
- Requires
&mut selfto ensure sound mutable access across all storages.
Sourcepub fn get_entity_for<T: Component>(&self, component: &T) -> Option<Entity>
pub fn get_entity_for<T: Component>(&self, component: &T) -> Option<Entity>
Given a reference to a component of type T, returns the owning Entity.
§Type Parameters
T: The component type of the provided reference.
§Parameters
component: A reference to a component previously obtained from the world.
§Returns
Option<Entity>: The entity that owns the component, if found.
§Complexity
- O(n) in the number of
Tcomponents; implemented by pointer-identity scan of the dense storage.
§Notes
- Intended for convenience or debugging; prefer entity-centric access patterns when possible.
Sourcepub fn register_resource<T: 'static>(&mut self, res: T) -> ResourceHandle
pub fn register_resource<T: 'static>(&mut self, res: T) -> ResourceHandle
Registers a resource of any type T into the resource registry.
Returns a ResourceHandle that can be used to retrieve the resource later.
§Type Parameters
T: The type of the resource. Must be'staticso it can be stored in a type-erased container.
§Parameters
res: The resource instance to register.
§Returns
ResourceHandle: A unique handle used to retrieve or mutate the resource later.
Sourcepub fn get_resource<T: 'static>(&self, handle: ResourceHandle) -> Option<&T>
pub fn get_resource<T: 'static>(&self, handle: ResourceHandle) -> Option<&T>
Retrieves an immutable reference to a registered resource of type T
using its ResourceHandle.
§Type Parameters
T: The expected type of the resource. Must match the type used inregister_resource.
§Parameters
handle: The handle returned byregister_resource.
§Returns
Option<&T>:Some(&T)if the handle is valid and the type matches, otherwiseNone.
Sourcepub fn get_resource_mut<T: 'static>(
&mut self,
handle: ResourceHandle,
) -> Option<&mut T>
pub fn get_resource_mut<T: 'static>( &mut self, handle: ResourceHandle, ) -> Option<&mut T>
Retrieves a mutable reference to a registered resource of type T
using its ResourceHandle.
§Type Parameters
T: The expected type of the resource. Must match the type used inregister_resource.
§Parameters
handle: The handle returned byregister_resource.
§Returns
Option<&mut T>:Some(&mut T)if the handle is valid and the type matches, otherwiseNone.
Sourcepub fn remove_resource<T: 'static>(
&mut self,
handle: ResourceHandle,
) -> Option<T>
pub fn remove_resource<T: 'static>( &mut self, handle: ResourceHandle, ) -> Option<T>
Removes a resource of type T from the world’s resource registry.
§Type Parameters
T: The type of the resource to remove.
§Parameters
handle: The handle associated with the resource to be removed.
§Returns
Some(T): If the resource was successfully removed.None: If the handle was invalid or the type mismatched.
Sourcepub fn register_unique<T: 'static>(&mut self, resource: T)
pub fn register_unique<T: 'static>(&mut self, resource: T)
Registers or overwrites the unique resource of type T in the world.
§Parameters
resource: The resource instance to register as unique.
Sourcepub fn get_unique<T: 'static>(&self) -> Option<&T>
pub fn get_unique<T: 'static>(&self) -> Option<&T>
Gets an immutable reference to the unique resource of type T from the world.
§Returns
Option<&T>: Some reference if the unique resource is present, None otherwise.
Sourcepub fn get_unique_mut<T: 'static>(&mut self) -> Option<&mut T>
pub fn get_unique_mut<T: 'static>(&mut self) -> Option<&mut T>
Gets a mutable reference to the unique resource of type T from the world.
§Returns
Option<&mut T>: Some mutable reference if the unique resource is present, None otherwise.
Sourcepub fn remove_unique<T: 'static>(&mut self) -> Option<T>
pub fn remove_unique<T: 'static>(&mut self) -> Option<T>
Removes the unique resource of type T from the world and returns it.
§Returns
Option<T>: The removed resource if it was present, None otherwise.