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§
Required Methods§
Sourcefn insert(&mut self, entity: Entity, value: Self::Item) -> Option<Self::Item>
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 componentvalue- 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).