Skip to main content

Archetype

Struct Archetype 

Source
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

Source

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.

Source

pub fn types(&self) -> &[TypeId]

The component types, sorted.

Source

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.

Source

pub fn entities(&self) -> &[Entity]

The entities, one per row.

Source

pub fn len(&self) -> usize

Number of entities.

Source

pub fn is_empty(&self) -> bool

Whether the archetype holds no entity.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub(crate) fn push_entity(&mut self, entity: Entity, scope: Entity)

Append one entity row that lives in scope.

Source

pub(crate) fn scopes(&self) -> &[Entity]

The scope each row lives in, aligned with Archetype::entities.

Source

pub(crate) fn scope_summary(&self) -> ScopeSummary

Which scopes the rows live in.

Source

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.

Source

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.

Source

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

Source§

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.

Auto Trait Implementations§

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> Component for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more