Skip to main content

ComponentStorage

Trait ComponentStorage 

Source
pub trait ComponentStorage: Send + Sync {
    type Item: Component;

    // Required methods
    fn insert(
        &mut self,
        entity: Entity,
        value: Self::Item,
    ) -> Option<Self::Item>;
    fn remove(&mut self, entity: Entity) -> Option<Self::Item>;
    fn get(&self, entity: Entity) -> Option<&Self::Item>;
    fn get_mut(&mut self, entity: Entity) -> Option<&mut Self::Item>;
    fn contains(&self, entity: Entity) -> bool;
    fn len(&self) -> usize;
    fn is_empty(&self) -> bool;
}
Expand description

Trait for component storage backends.

ComponentStorage defines the interface for storing components indexed by entity. Any type implementing this trait can be used as a storage backend for components in the ECS world.

§Type Parameters

The associated type Item specifies what component type this storage holds. It must implement Component to ensure thread safety requirements are met.

§Thread Safety

Implementations must be Send + Sync to allow the ECS world to be shared across threads. This is enforced by the trait bounds.

§Object Safety

This trait is not object-safe due to the associated type. For type-erased storage, use AnyComponentStorage which provides a runtime interface.

§Example Implementation

use goud_engine::ecs::{Entity, Component, ComponentStorage, SparseSet};

// Define a custom storage (wrapping SparseSet for this example)
struct MyStorage<T: Component> {
    inner: SparseSet<T>,
}

impl<T: Component> ComponentStorage for MyStorage<T> {
    type Item = T;

    fn insert(&mut self, entity: Entity, value: T) -> Option<T> {
        self.inner.insert(entity, value)
    }

    fn remove(&mut self, entity: Entity) -> Option<T> {
        self.inner.remove(entity)
    }

    fn get(&self, entity: Entity) -> Option<&T> {
        self.inner.get(entity)
    }

    fn get_mut(&mut self, entity: Entity) -> Option<&mut T> {
        self.inner.get_mut(entity)
    }

    fn contains(&self, entity: Entity) -> bool {
        self.inner.contains(entity)
    }

    fn len(&self) -> usize {
        self.inner.len()
    }

    fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }
}

Required Associated Types§

Source

type Item: Component

The component type stored in this storage.

Required Methods§

Source

fn insert(&mut self, entity: Entity, value: Self::Item) -> Option<Self::Item>

Inserts a component for the given entity.

If the entity already has a component, the old value is returned. Otherwise, None is returned.

§Arguments
  • entity - The entity to associate with the component
  • value - The component value to store
§Returns

The previous component value if one existed, or None.

§Panics

Implementations may panic if entity is invalid (e.g., placeholder).

Source

fn remove(&mut self, entity: Entity) -> Option<Self::Item>

Removes the component for the given entity.

§Arguments
  • entity - The entity whose component to remove
§Returns

The removed component if one existed, or None.

Source

fn get(&self, entity: Entity) -> Option<&Self::Item>

Returns a reference to the component for the given entity.

§Arguments
  • entity - The entity to look up
§Returns

A reference to the component if the entity has one, or None.

Source

fn get_mut(&mut self, entity: Entity) -> Option<&mut Self::Item>

Returns a mutable reference to the component for the given entity.

§Arguments
  • entity - The entity to look up
§Returns

A mutable reference to the component if the entity has one, or None.

Source

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

Returns true if the entity has a component in this storage.

§Arguments
  • entity - The entity to check
Source

fn len(&self) -> usize

Returns the number of components in this storage.

Source

fn is_empty(&self) -> bool

Returns true if this storage contains no components.

Implementors§