pub struct Archetype {
types: Box<[TypeId]>,
names: Box<[&'static str]>,
columns: Box<[Box<dyn AnyColumn>]>,
entities: Vec<Entity>,
scopes: Vec<Entity>,
scope_counts: Vec<(Entity, u32)>,
}Expand description
One archetype: the entities that share a component set.
Fields§
§types: Box<[TypeId]>§names: Box<[&'static str]>The name of each component type, in the same order as types.
Cached here rather than looked up in the world so that an archetype can describe itself without reaching back into the world it came from.
columns: Box<[Box<dyn AnyColumn>]>§entities: Vec<Entity>§scopes: Vec<Entity>The scope each row lives in, in the same order as entities.
The per-row answer to what a whole-archetype ScopeSummary cannot decide.
It is stored rather than looked up so that a visibility test costs one
read instead of a walk of the scope tree, and it is the only place the
scope of a row is recorded.
scope_counts: Vec<(Entity, u32)>How many rows live in each scope, one entry per distinct scope.
The scope summary is derived from this rather than stored, so it cannot
go stale: an archetype whose rows all move out of one scope stops being
Mixed the moment the last of them leaves.
Implementations§
Source§impl Archetype
impl Archetype
Sourcepub(crate) fn new(
types: Box<[TypeId]>,
names: Box<[&'static str]>,
columns: Box<[Box<dyn AnyColumn>]>,
) -> Self
pub(crate) fn new( types: Box<[TypeId]>, names: Box<[&'static str]>, columns: Box<[Box<dyn AnyColumn>]>, ) -> Self
Build an archetype from its component types, their names and their columns.
types must be sorted and unique, and names and columns must line
up with it. Sorting is what makes one component set one archetype: the
types are the key the pool looks an archetype up by, so two spawns of the
same set have to arrive in the same order.
Sourcepub fn names(&self) -> &[&'static str]
pub fn names(&self) -> &[&'static str]
The names of the component types, in the same order as
Archetype::types.
Each name is what core::any::type_name reported for the type when it
first entered the world, so the two slices answer for the same types in
the same order and an archetype names its own shape without consulting
the world it came from.
Sourcepub(crate) fn column_index(&self, type_id: TypeId) -> Option<usize>
pub(crate) fn column_index(&self, type_id: TypeId) -> Option<usize>
The position of type_id in Archetype::types, if present.
A scan rather than a binary search. An archetype holds the components one
entity was spawned with, which a scene entity keeps to a handful, and
over that range a scan wins: comparing is branch-free and vectorizes once
this is inlined into its caller, while a binary search pays a
mispredicted branch per probe. A scan only loses on an archetype far
wider than a tuple spawn reaches, which a hand-written Bundle can
still build. The sorted order Bundle gives the types is what keeps
archetypes identical across spawn orders; the lookup does not rely on it.
Sourcepub fn has<C: 'static>(&self) -> bool
pub fn has<C: 'static>(&self) -> bool
Whether this archetype carries component C.
Only the component set is consulted, so no cell is borrowed and this answers the same during a borrow that would block reading the component itself.
Sourcepub(crate) fn column<C: 'static>(&self) -> Option<&Column<C>>
pub(crate) fn column<C: 'static>(&self) -> Option<&Column<C>>
The column of component C.
A query resolves this once per archetype instead of doing the lookup per row.
Sourcepub(crate) fn column_mut<C: 'static>(&mut self) -> Option<&mut Column<C>>
pub(crate) fn column_mut<C: 'static>(&mut self) -> Option<&mut Column<C>>
The column of component C, to move a value in or out.
Only reachable from an exclusive borrow of the world, so handing it out cannot race a query that holds one of the rows.
Sourcepub(crate) fn push_erased(&mut self, type_id: TypeId, value: Box<dyn Any>)
pub(crate) fn push_erased(&mut self, type_id: TypeId, value: Box<dyn Any>)
Append an already erased component value.
Every caller supplies a value whose type is one of the archetype’s component types.
Sourcepub(crate) fn push_entity(&mut self, entity: Entity, scope: Entity)
pub(crate) fn push_entity(&mut self, entity: Entity, scope: Entity)
Append one entity row that lives in scope.
Sourcepub(crate) fn scopes(&self) -> &[Entity]
pub(crate) fn scopes(&self) -> &[Entity]
The scope each row lives in, aligned with Archetype::entities.
Sourcepub(crate) fn scope_summary(&self) -> ScopeSummary
pub(crate) fn scope_summary(&self) -> ScopeSummary
Which scopes the rows live in.
Sourcepub(crate) fn move_scope(&mut self, row: usize, from: Entity, to: Entity)
pub(crate) fn move_scope(&mut self, row: usize, from: Entity, to: Entity)
Move row row from from to to.
Called when a row’s entity is linked into another scope: the component set is unchanged, so the row stays where it is and only its scope and the counts change.
Sourcefn count_scope(&mut self, scope: Entity, delta: i32)
fn count_scope(&mut self, scope: Entity, delta: i32)
Add delta to the row count of scope, dropping the entry at zero.
A count that would go below zero means the counts and the rows already disagreed, and the per-scope gates would answer from the disagreement: panicking here is what keeps it from spreading into the tree.
Sourcepub(crate) fn swap_remove(&mut self, row: usize) -> Option<Entity>
pub(crate) fn swap_remove(&mut self, row: usize) -> Option<Entity>
Drop the row at row, moving the last row into the hole.
Returns the entity that was moved into the hole, so its location can be
updated, or None when the row was the last one.
Trait Implementations§
Source§impl Debug for Archetype
impl Debug for Archetype
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Reports the shape and the row count, not the rows.
An archetype can hold tens of thousands of entities, and printing them
would bury the one fact a caller is usually after: which components the
rows carry. The entity list is reachable through
Archetype::entities.