Skip to main content

Scope

Struct Scope 

Source
pub struct Scope<'w> {
    pub(crate) world: &'w World,
    pub(crate) scope: Entity,
}
Expand description

A read-only scope: one entity and everything below it.

Fields§

§world: &'w World§scope: Entity

Implementations§

Source§

impl<'w> Scope<'w>

Source

pub(crate) fn new(world: &'w World, scope: Entity) -> Self

Source

pub fn entity(&self) -> Entity

The entity this scope is rooted at.

Source

pub fn contains(&self, entity: Entity) -> bool

Whether entity is visible from this scope.

This is the addressability test: the two roots answer true so that their components can be read and written like any other entity’s, even though counting and querying omit them (see Scope::len).

Source

pub fn location(&self, entity: Entity) -> Result<Location, EntityAccessError>

Where a visible entity’s components live.

Answers EntityAccessError::NotAlive for a handle that names no live entity, and EntityAccessError::NotVisible for one that lives outside this scope.

Source

pub fn query<Q: Query>(&self) -> QueryBorrow<'w, Q>

Iterate the visible entities that match Q.

The query is a type, so which components to hand out and which archetypes to visit are written in the turbofish. See crate::query for the items a query is made of.

let mut world = World::new();
world.root_scope_mut().spawn(bundle![1u32]);

for (entity, value) in world.root_scope().query::<(Entity, &u32)>().iter() {
    println!("{entity:?} carries {value}");
}

The answer is a holder: it claims the components the query reaches before anything is handed out, and releases them when it is dropped. A claim that overlaps one already held is reported here, naming where the other was taken, rather than at the row it would have reached.

Source

pub fn query_one<Q: Query>( &self, entity: Entity, ) -> Result<QueryOne<'w, Q>, EntityAccessError>

What Q yields for entity, when it is visible here and its archetype matches.

This is the one-entity form of Scope::query: the row the entity is stored at is looked up directly instead of the archetypes being walked. A lookup that finds nothing says why: the entity is not alive, out of this scope’s reach, or its row does not satisfy the query. The last one covers two cases a caller answers the same way, so it is one error: the archetype does not match Q, or the row sits outside what this scope reaches. A shared row is reachable from every scope, the same by handle as it is through Scope::query.

A lookup by handle does not filter the scaffolding: the root and the shared scope answer like any other entity, which is what lets the resources they carry be read. Iterating and counting go through a scope and leave the pair out; see Scope::len.

let mut world = World::new();
let entity = world.root_scope_mut().spawn(bundle![1u32]);

assert_eq!(*world.root_scope().query_one::<&u32>(entity).unwrap().get(), 1);
assert!(world.root_scope().query_one::<Has<u32>>(entity).unwrap().get());
assert!(!world.root_scope().query_one::<Has<bool>>(entity).unwrap().get());
assert!(world.root_scope().query_one::<&bool>(entity).is_err());

The answer is a holder rather than the item itself: it claims the components the query reaches on that row while it is built, so the claim begins before the item exists and lasts as long as the holder.

Source

pub fn archetype(&self, id: u32) -> Result<&'w Archetype, ArchetypeAccessError>

The archetype with this id, when this scope sees every row of it.

A partially visible archetype is turned down: handing out its entity list would report rows the scope cannot otherwise reach. So is an empty one, which has no row to see. A caller that only wants the shape of an archetype uses Scope::archetype_infos, whose length is the visible one; a caller that has an entity and wants the archetype its row lives in uses Scope::archetype_of, which asks about the row rather than about the whole archetype.

Source

pub fn archetype_of( &self, entity: Entity, ) -> Result<&'w Archetype, EntityAccessError>

The archetype an entity’s row lives in, when the entity is visible here.

This is the counterpart of Scope::location: it answers for one visible row, so it also answers for an archetype Scope::archetype turns down. Two scopes whose entities share a component set share the archetype and each sees only its own rows of it; asking about one row is well defined, while handing out the archetype as a whole is not.

The archetype is the storage, not a view of it: Archetype::entities lists the rows of every scope, the ones this scope cannot see included. Scope::entity_info and Scope::entity_infos are what filter by visibility.

Source

pub fn archetypes(&self) -> impl Iterator<Item = &'w Archetype>

Every archetype this scope sees in full, in id order.

Empty archetypes are skipped: they hold no row, so there is nothing to iterate. The one archetype holding the tree’s scaffolding is included, since its rows do exist; Scope::len is what leaves them out of the count.

Source

pub fn scope(&self, scope: Entity) -> Result<Scope<'w>, EntityAccessError>

The scope of a visible scope.

A scope is an entity like any other, so this is turned down the same way Scope::location is.

Source

pub fn parent_scope(&self, entity: Entity) -> Result<Entity, EntityAccessError>

The parent of a visible entity.

Source

pub fn children(&self, entity: Entity) -> Result<Vec<Entity>, EntityAccessError>

The direct children of a visible entity.

Answers EntityAccessError::NotAlive or EntityAccessError::NotVisible rather than an empty list when the entity is not there to ask about.

Source

pub fn len(&self) -> usize

The number of visible entities, the two roots excluded.

An empty world is empty; the root and the shared scope are scaffolding and are reported by WorldInfo::root and WorldInfo::shared instead of being counted.

Source

pub fn is_empty(&self) -> bool

Whether nothing is visible.

Source

pub fn info(&self) -> WorldInfo

The scope’s shape: visible counts, the component inventory, and the two roots.

Source

pub fn archetype_infos(&self) -> Vec<ArchetypeInfo>

Every archetype with at least one visible row, in id order.

Source

pub fn entity_name( &self, entity: Entity, ) -> Result<&'static str, EntityAccessError>

What a visible entity is called, read from the row that stores it.

A handle rebuilt from bare bits carries no name of its own; the row does. See World::entity_name.

Source

pub fn entity_info( &self, entity: Entity, ) -> Result<EntityInfo, EntityAccessError>

A visible entity’s location and component names.

The handle reported, and the name beside it, both come from the row that stores the entity, so a handle that arrived as bare bits is answered with the label its spawn site gave it.

Source

pub fn entity_infos(&self, limit: usize) -> Vec<EntityInfo>

At most limit visible entities’ information, in storage order.

Source

pub fn queue(self) -> Commands<'w>

A queue of structural changes, all of them in this scope.

Takes the scope by value, so the queue outlives it: the scope is a copy of a reference, and a queue that borrowed the temporary would be unusable in the position it is most wanted.

Source

fn archetype_visible(&self, archetype: &Archetype) -> bool

Whether an archetype has any row this scope can see.

Source

fn fully_visible(&self, archetype: &Archetype) -> bool

Whether every row of archetype is visible from this scope.

Source

fn row_visible(&self, archetype: &Archetype, row: usize) -> bool

Whether row row of archetype is visible.

Source

fn visible_len(&self, index: u32, archetype: &Archetype) -> usize

The number of rows of archetype this scope can see, its own content only: the scaffolding is never counted.

Trait Implementations§

Source§

impl<'w> Clone for Scope<'w>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'w> Copy for Scope<'w>

Source§

impl Debug for Scope<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'w> !RefUnwindSafe for Scope<'w>

§

impl<'w> !Send for Scope<'w>

§

impl<'w> !Sync for Scope<'w>

§

impl<'w> !UnwindSafe for Scope<'w>

§

impl<'w> Forget for Scope<'w>

§

impl<'w> Freeze for Scope<'w>

§

impl<'w> Unpin for Scope<'w>

§

impl<'w> UnsafeUnpin for Scope<'w>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Component for T
where T: 'static,

Source§

impl<T> DynClone for T
where T: Clone,

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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