pub trait ComponentBundle {
// Required methods
fn type_ids() -> Vec<TypeId>;
fn create_columns() -> HashMap<TypeId, Box<dyn AnyVec>>;
fn update_metadata(
metadata: &mut EntityMetadata,
location: PageIndex,
registry: &ComponentRegistry,
);
unsafe fn add_to_page(self, page: &mut ComponentPage);
}
Expand description
A trait for any collection of components that can be spawned together as a single unit.
This is a key part of the ECS’s public API. It is typically implemented on tuples
of components, like (Position, Velocity)
. It provides the logic for identifying
its component types and safely writing its data into a ComponentPage
.
Required Methods§
Sourcefn type_ids() -> Vec<TypeId>
fn type_ids() -> Vec<TypeId>
Returns the sorted list of TypeId
s for the components in this bundle.
This provides a canonical “signature” for the bundle, which is used to find
a matching ComponentPage
in the World
. Sorting is crucial to ensure that
tuples with the same components but in a different order (e.g., (A, B) vs (B, A))
are treated as identical.
Sourcefn create_columns() -> HashMap<TypeId, Box<dyn AnyVec>>
fn create_columns() -> HashMap<TypeId, Box<dyn AnyVec>>
Creates the set of empty, type-erased Vec<T>
columns required to store
this bundle’s components.
This is called by the World
when a new ComponentPage
needs to be
created for a specific bundle layout.
Sourcefn update_metadata(
metadata: &mut EntityMetadata,
location: PageIndex,
registry: &ComponentRegistry,
)
fn update_metadata( metadata: &mut EntityMetadata, location: PageIndex, registry: &ComponentRegistry, )
Updates the appropriate fields in an EntityMetadata
struct to point
to the location of this bundle’s data.
This method is called by World::spawn
to link an entity to its newly
created component data.
Sourceunsafe fn add_to_page(self, page: &mut ComponentPage)
unsafe fn add_to_page(self, page: &mut ComponentPage)
Adds the components from this bundle into the specified ComponentPage
.
§Safety
This function is unsafe because it relies on the caller to guarantee that
the ComponentPage
is the correct one for this bundle’s exact component layout.
It performs unsafe downcasting to write to the type-erased Vec<T>
s.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.