pub struct ComponentStorage<T: Component> { /* private fields */ }Expand description
Storage for components of type T. Maintains dense storage and entity-index mappings.
§Fields
components: Dense array of components of typeT.entity_to_index: Map fromEntityto its dense array index.index_to_entity: Reverse map from dense array index to owningEntity.
§Safety
- Storage remains dense by using swap-remove on deletion; index maps are updated accordingly to prevent dangling indices. Component references returned by queries are valid for the borrow duration and follow Rust’s aliasing rules.
Implementations§
Source§impl<T: Component> ComponentStorage<T>
impl<T: Component> ComponentStorage<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new, empty component storage.
§Returns
ComponentStorage<T>: An empty storage ready to hold components of typeT.
Sourcepub fn get_entity_for_component(&self, component: &T) -> Option<Entity>
pub fn get_entity_for_component(&self, component: &T) -> Option<Entity>
Given a reference to a component in this storage, return the owning Entity.
This performs an O(n) scan comparing pointer identity within the dense component array to locate the index, then maps that index back to the entity.
§Parameters
component: A reference to a component stored in this storage.
§Returns
Option<Entity>: The entity that owns the component, if found.
Sourcepub fn insert(&mut self, entity: Entity, component: T)
pub fn insert(&mut self, entity: Entity, component: T)
Inserts or replaces a component for an entity.
§Parameters
entity: The entity that will own the component.component: The component instance to insert (or replace if one already exists).
Sourcepub fn get_all(&self) -> Vec<&T>
pub fn get_all(&self) -> Vec<&T>
Returns all components as a vector of immutable references.
§Returns
Vec<&T>: All components currently stored.
Sourcepub fn get_all_mut(&mut self) -> Vec<&mut T>
pub fn get_all_mut(&mut self) -> Vec<&mut T>
Returns all components as a vector of mutable references.
§Returns
Vec<&mut T>: All components currently stored, mutably borrowed.
Sourcepub fn iter(&self) -> impl Iterator<Item = &T>
pub fn iter(&self) -> impl Iterator<Item = &T>
Returns an iterator over immutable references to components.
§Returns
impl Iterator<Item = &T>: Iterator over all components.
Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>
Returns an iterator over mutable references to components.
§Returns
impl Iterator<Item = &mut T>: Iterator over all components mutably.
Sourcepub fn iter_with_entities(&self) -> impl Iterator<Item = (Entity, &T)>
pub fn iter_with_entities(&self) -> impl Iterator<Item = (Entity, &T)>
Returns an iterator over all entities and immutable references to their components.
§Returns
impl Iterator<Item = (Entity, &T)>: Iterator of(Entity, &T)pairs.
Trait Implementations§
Source§impl<T: Component> ComponentStorageTrait for ComponentStorage<T>
impl<T: Component> ComponentStorageTrait for ComponentStorage<T>
Source§fn remove_entity(&mut self, entity: Entity)
fn remove_entity(&mut self, entity: Entity)
Removes the component for the given entity from this concrete storage.
§Parameters
entity: The entity whose component should be removed.
§Effects
- If a component exists for
entity, it is removed using the storage’s internal removal logic (swap-remove), maintaining dense storage and updating index mappings accordingly.
Source§fn as_any(&self) -> &dyn Any
fn as_any(&self) -> &dyn Any
Returns an immutable, type-erased view of this concrete storage for downcasting.
§Returns
&dyn Any: A reference that can be downcast viaAny::downcast_reftoComponentStorage<T>by callers that know the concreteT.
§Safety
- Callers must only attempt to downcast to the correct concrete type.
The
Worldensures this by checkingTypeIdprior to downcasting.
Source§fn as_any_mut(&mut self) -> &mut dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
Returns a mutable, type-erased view of this concrete storage for downcasting.
§Returns
&mut dyn Any: A mutable reference that can be downcast viaAny::downcast_muttoComponentStorage<T>when the caller knowsT.
§Safety
- Callers must ensure the downcast target type matches the underlying
storage.
WorldAPIs guard this viaTypeIdchecks before downcasting.