Struct ComponentStorage

Source
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 type T.
  • entity_to_index: Map from Entity to its dense array index.
  • index_to_entity: Reverse map from dense array index to owning Entity.

§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>

Source

pub fn new() -> Self

Creates a new, empty component storage.

§Returns
  • ComponentStorage<T>: An empty storage ready to hold components of type T.
Source

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.
Source

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).
Source

pub fn remove(&mut self, entity: Entity) -> Option<T>

Removes the component for an entity, returning it if present. Uses swap-remove to keep storage dense and updates mappings accordingly.

§Parameters
  • entity: The entity whose component should be removed.
§Returns
  • Option<T>: The removed component if it existed.
Source

pub fn get(&self, entity: Entity) -> Option<&T>

Gets an immutable reference to a component for an entity, if it exists.

§Parameters
  • entity: The entity whose component to retrieve.
§Returns
  • Option<&T>: A reference to the component if present.
Source

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

Gets a mutable reference to a component for an entity, if it exists.

§Parameters
  • entity: The entity whose component to retrieve mutably.
§Returns
  • Option<&mut T>: A mutable reference to the component if present.
Source

pub fn get_all(&self) -> Vec<&T>

Returns all components as a vector of immutable references.

§Returns
  • Vec<&T>: All components currently stored.
Source

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.
Source

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.
Source

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.
Source

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.
Source

pub fn iter_with_entities_mut( &mut self, ) -> impl Iterator<Item = (Entity, &mut T)>

Returns an iterator over all entities and mutable references to their components.

§Returns
  • impl Iterator<Item = (Entity, &mut T)>: Iterator of (Entity, &mut T) pairs.
Source

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

Returns true if this storage contains a component for the specified entity.

§Parameters
  • entity: The entity to check.
§Returns
  • bool: true if a component for entity exists in this storage.

Trait Implementations§

Source§

impl<T: Component> ComponentStorageTrait for ComponentStorage<T>

Source§

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

Returns an immutable, type-erased view of this concrete storage for downcasting.

§Returns
  • &dyn Any: A reference that can be downcast via Any::downcast_ref to ComponentStorage<T> by callers that know the concrete T.
§Safety
  • Callers must only attempt to downcast to the correct concrete type. The World ensures this by checking TypeId prior to downcasting.
Source§

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 via Any::downcast_mut to ComponentStorage<T> when the caller knows T.
§Safety
  • Callers must ensure the downcast target type matches the underlying storage. World APIs guard this via TypeId checks before downcasting.

Auto Trait Implementations§

§

impl<T> Freeze for ComponentStorage<T>

§

impl<T> RefUnwindSafe for ComponentStorage<T>
where T: RefUnwindSafe,

§

impl<T> Send for ComponentStorage<T>
where T: Send,

§

impl<T> Sync for ComponentStorage<T>
where T: Sync,

§

impl<T> Unpin for ComponentStorage<T>
where T: Unpin,

§

impl<T> UnwindSafe for ComponentStorage<T>
where T: UnwindSafe,

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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.