Struct World
pub struct World { /* private fields */ }Expand description
Collection of entities and registered sparse component storages.
Register component types before insertion. Iteration order within a storage
changes on removal; use Self::entities for deterministic index order.
Built-in engine worlds are initialized by Scene::default.
Implementations§
§impl World
impl World
pub fn register<T>(&mut self)where
T: Component,
pub fn register<T>(&mut self)where
T: Component,
Creates the storage for T. Must be called (by the host) before T is inserted.
pub fn is_registered<T>(&self) -> boolwhere
T: Component,
pub fn is_registered<T>(&self) -> boolwhere
T: Component,
Whether the host registered this exact component type.
§Panics
Panics if the key belongs to an incompatible component type.
pub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether no living entities remain. Registered storages are not counted.
pub fn spawn(&mut self) -> Entity
pub fn spawn(&mut self) -> Entity
Creates a living entity without components, reusing a vacant slot when possible.
pub fn despawn(&mut self, e: Entity)
pub fn despawn(&mut self, e: Entity)
Removes an entity and all its components. Stale handles are ignored.
This ECS operation does not traverse engine hierarchy relations.
pub fn is_alive(&self, e: Entity) -> bool
pub fn is_alive(&self, e: Entity) -> bool
Checks both slot occupancy and generation. Handles from other worlds are unsupported.
pub fn iter_entities(&self) -> impl Iterator<Item = Entity>
pub fn iter_entities(&self) -> impl Iterator<Item = Entity>
Borrows all living handles in index order without allocating a snapshot.
pub fn clear(&mut self)
pub fn clear(&mut self)
Destroys every entity while preserving registrations and allocated storage. All previously issued handles become stale.
pub fn insert<T>(&mut self, e: Entity, value: T)where
T: Component,
pub fn insert<T>(&mut self, e: Entity, value: T)where
T: Component,
Inserts or replaces a component. Stale entities are ignored.
§Panics
Panics if a living entity is used before Self::register for T,
or the component key conflicts with an incompatible registered type.
pub fn try_insert<T>(
&mut self,
entity: Entity,
value: T,
) -> Result<Option<T>, T>where
T: Component,
pub fn try_insert<T>(
&mut self,
entity: Entity,
value: T,
) -> Result<Option<T>, T>where
T: Component,
Inserts or replaces a component without panicking for stale entities or unregistered types. On success returns the replaced value, if any; on rejection returns ownership of the supplied value unchanged. Conflicting component-key metadata still panics before any erased cast.
pub fn remove<T>(&mut self, e: Entity) -> Option<T>where
T: Component,
pub fn remove<T>(&mut self, e: Entity) -> Option<T>where
T: Component,
Removes and returns a component, or None for a stale entity or absent component.
pub fn get<T>(&self, e: Entity) -> Option<&T>where
T: Component,
pub fn get<T>(&self, e: Entity) -> Option<&T>where
T: Component,
Borrows a component; returns None for stale entities and absent types.
pub fn get_mut<T>(&mut self, e: Entity) -> Option<&mut T>where
T: Component,
pub fn get_mut<T>(&mut self, e: Entity) -> Option<&mut T>where
T: Component,
Mutably borrows a component; returns None for stale entities and absent types.
pub fn get2<A, B>(&self, entity: Entity) -> Option<(&A, &B)>where
A: Component,
B: Component,
pub fn get2<A, B>(&self, entity: Entity) -> Option<(&A, &B)>where
A: Component,
B: Component,
Borrows two components of one living entity. Repeated component types are allowed.
pub fn get2_mut<A, B>(&mut self, entity: Entity) -> Option<(&mut A, &mut B)>where
A: Component,
B: Component,
pub fn get2_mut<A, B>(&mut self, entity: Entity) -> Option<(&mut A, &mut B)>where
A: Component,
B: Component,
Mutably borrows two distinct component storages on one living entity.
Returns None for stale entities, missing components, unregistered types
or identical storage keys (which would otherwise alias mutable references).
§Panics
Panics if component metadata conflicts with a registered storage key.
pub fn for_each2_mut<A, B>(
&mut self,
visit: impl FnMut(Entity, &mut A, &mut B),
) -> usizewhere
A: Component,
B: Component,
pub fn for_each2_mut<A, B>(
&mut self,
visit: impl FnMut(Entity, &mut A, &mut B),
) -> usizewhere
A: Component,
B: Component,
Applies a callback to every entity containing two distinct mutable component types. Returns the processed count; identical storage keys process zero entities. Structural changes are intentionally unavailable inside the callback.
pub fn retain<T>(&mut self, keep: impl FnMut(Entity, &mut T) -> bool) -> usizewhere
T: Component,
pub fn retain<T>(&mut self, keep: impl FnMut(Entity, &mut T) -> bool) -> usizewhere
T: Component,
Removes T values rejected by a mutable predicate, retaining all entities.
Returns the number removed. Dense storage order may change during removals.
pub fn iter<T>(&self) -> impl Iterator<Item = (Entity, &T)>where
T: Component,
pub fn iter<T>(&self) -> impl Iterator<Item = (Entity, &T)>where
T: Component,
Iterates a dense storage without allocation. An unregistered type yields no items.
pub fn iter_mut<T>(&mut self) -> impl Iterator<Item = (Entity, &mut T)>where
T: Component,
pub fn iter_mut<T>(&mut self) -> impl Iterator<Item = (Entity, &mut T)>where
T: Component,
Mutably iterates a dense storage; structural world changes require a snapshot instead.
pub fn entities_with<T>(&self) -> Vec<Entity>where
T: Component,
pub fn entities_with<T>(&self) -> Vec<Entity>where
T: Component,
Entities that have component T (snapshot, safe to mutate the world while looping).
pub fn entities_with2<A, B>(&self) -> Vec<Entity>where
A: Component,
B: Component,
pub fn entities_with2<A, B>(&self) -> Vec<Entity>where
A: Component,
B: Component,
Entities that have both A and B.
pub fn iter2<A, B>(&self) -> impl Iterator<Item = (Entity, &A, &B)>where
A: Component,
B: Component,
pub fn iter2<A, B>(&self) -> impl Iterator<Item = (Entity, &A, &B)>where
A: Component,
B: Component,
Borrows entities containing both component types, in A storage order.
Matching a type with itself is supported because both borrows are shared.
pub fn iter3<A, B, C>(&self) -> impl Iterator<Item = (Entity, &A, &B, &C)>where
A: Component,
B: Component,
C: Component,
pub fn iter3<A, B, C>(&self) -> impl Iterator<Item = (Entity, &A, &B, &C)>where
A: Component,
B: Component,
C: Component,
Borrows entities containing all three component types, in A storage order.
pub fn entities_with3<A, B, C>(&self) -> Vec<Entity>where
A: Component,
B: Component,
C: Component,
pub fn entities_with3<A, B, C>(&self) -> Vec<Entity>where
A: Component,
B: Component,
C: Component,
Snapshot of entities containing all three component types.
pub fn entities_without<A, B>(&self) -> Vec<Entity>where
A: Component,
B: Component,
pub fn entities_without<A, B>(&self) -> Vec<Entity>where
A: Component,
B: Component,
Snapshot of entities with A but without B, in A storage order.
pub fn get_or_insert_with<T>(
&mut self,
e: Entity,
create: impl FnOnce() -> T,
) -> Option<&mut T>where
T: Component,
pub fn get_or_insert_with<T>(
&mut self,
e: Entity,
create: impl FnOnce() -> T,
) -> Option<&mut T>where
T: Component,
Inserts a lazily constructed component only when missing, then borrows it.
Returns None without invoking create for stale entities.
§Panics
Panics if T is unregistered on a living entity.
pub fn clear_components<T>(&mut self)where
T: Component,
pub fn clear_components<T>(&mut self)where
T: Component,
Removes every T, preserving entities and the registered storage.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for World
impl !Send for World
impl !Sync for World
impl !UnwindSafe for World
impl Freeze for World
impl Unpin for World
impl UnsafeUnpin for World
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.