Struct Query

Source
pub struct Query<V: for<'a> View<'a>, F: EntityFilter> {
    pub filter: F,
    /* private fields */
}
Expand description

Queries for entities within a World.

§Examples

Queries can be constructed from any View type, including tuples of Views.

// A query which matches any entity with a `Position` component
let mut query = Read::<Position>::query();

// A query which matches any entity with both a `Position` and a `Velocity` component
let mut query = <(Read<Position>, Read<Velocity>)>::query();

The view determines what data is accessed, and whether it is accessed mutably or not.

// A query which writes `Position`, reads `Velocity` and reads `Model`
// Tags are read-only, and is distinguished from entity data reads with `Tagged<T>`.
let mut query = <(Write<Position>, Read<Velocity>, Tagged<Model>)>::query();

By default, a query will filter its results to include only entities with the data types accessed by the view. However, additional filters can be specified if needed:

#[derive(Copy, Clone, Debug, PartialEq)]
struct Static;

// A query which also requires that entities have the `Static` tag
let mut query = <(Read<Position>, Tagged<Model>)>::query().filter(tag::<Static>());

Filters can be combined with bitwise operators:

#[derive(Copy, Clone, Debug, PartialEq)]
struct Static;

// This query matches entities with positions and a model
// But it also requires that the entity is not static, or has moved (even if static)
let mut query = <(Read<Position>, Tagged<Model>)>::query()
    .filter(!tag::<Static>() | changed::<Position>());

Filters can be iterated through to pull data out of a World:

// A query which writes `Position`, reads `Velocity` and reads `Model`
// Tags are read-only, and is distinguished from entity data reads with `Tagged<T>`.
let mut query = <(Write<Position>, Read<Velocity>, Tagged<Model>)>::query();

for (mut pos, vel, model) in query.iter_mut(&mut world) {
    // `.iter` yields tuples of references to a single entity's data:
    // pos: &mut Position
    // vel: &Velocity
    // model: &Model
}

The lower level iter_chunks_mut function allows access to each underlying chunk of entity data. This allows you to run code for each tag value, or to retrieve a contiguous data slice.

let mut query = <(Write<Position>, Read<Velocity>, Tagged<Model>)>::query();

for chunk in query.iter_chunks_mut(&mut world) {
    let model = chunk.tag::<Model>();
    let positions = chunk.components_mut::<Position>();
    let velocities = chunk.components::<Velocity>();
}

The ChunkView yielded from iter_chunks_mut allows access to all shared data in the chunk (queried for or not), but entity data slices can only be accessed if they were requested in the query’s view. Attempting to access other data types, or attempting to write to components that were only requested via a Read will panic.

Fields§

§filter: F

Implementations§

Source§

impl<V, F> Query<V, F>
where V: for<'a> View<'a>, F: EntityFilter,

Source

pub fn filter<T: EntityFilter>( self, filter: T, ) -> Query<V, <F as BitAnd<T>>::Output>
where F: BitAnd<T>, <F as BitAnd<T>>::Output: EntityFilter,

Adds an additional filter to the query.

Source

pub unsafe fn iter_chunks_unchecked<'a, 'data, T: EntityStore>( &'a self, world: &'data T, ) -> ChunkViewIter<'data, 'a, V, F::ArchetypeFilter, F::ChunksetFilter, F::ChunkFilter>

Gets an iterator which iterates through all chunks that match the query. Does not perform static borrow checking.

§Safety

The normal borrowing restrictions apply for the duration of the iteration:

  • Components borrowed with Read access must not be borrowed mutably elsewhere.
  • Components borrowed with Write access must not be borrowed elsewhere at all.
§Panics

This function may panic if other code is concurrently accessing the same components.

Source

pub fn iter_chunks<'a, 'data, T: EntityStore>( &'a self, world: &'data T, ) -> ChunkViewIter<'data, 'a, V, F::ArchetypeFilter, F::ChunksetFilter, F::ChunkFilter>
where V: ReadOnly,

Gets an iterator which iterates through all chunks that match the query.

Source

pub fn iter_chunks_mut<'a, 'data, T: EntityStore>( &'a self, world: &'data mut T, ) -> ChunkViewIter<'data, 'a, V, F::ArchetypeFilter, F::ChunksetFilter, F::ChunkFilter>

Gets an iterator which iterates through all chunks that match the query.

Source

pub unsafe fn iter_entities_unchecked<'a, 'data, T: EntityStore>( &'a self, world: &'data T, ) -> ChunkEntityIter<'data, V, ChunkViewIter<'data, 'a, V, F::ArchetypeFilter, F::ChunksetFilter, F::ChunkFilter>>

Gets an iterator which iterates through all entity data that matches the query, and also yields the the Entity IDs. Does not perform static borrow checking.

§Safety

The normal borrowing restrictions apply for the duration of the iteration:

  • Components borrowed with Read access must not be borrowed mutably elsewhere.
  • Components borrowed with Write access must not be borrowed elsewhere at all.
§Panics

This function may panic if other code is concurrently accessing the same components.

Source

pub fn iter_entities<'a, 'data, T: EntityStore>( &'a self, world: &'data T, ) -> ChunkEntityIter<'data, V, ChunkViewIter<'data, 'a, V, F::ArchetypeFilter, F::ChunksetFilter, F::ChunkFilter>>
where V: ReadOnly,

Gets an iterator which iterates through all entity data that matches the query, and also yields the the Entity IDs.

Source

pub fn iter_entities_mut<'a, 'data, T: EntityStore>( &'a self, world: &'data mut T, ) -> ChunkEntityIter<'data, V, ChunkViewIter<'data, 'a, V, F::ArchetypeFilter, F::ChunksetFilter, F::ChunkFilter>>

Gets an iterator which iterates through all entity data that matches the query, and also yields the the Entity IDs.

Source

pub unsafe fn iter_unchecked<'a, 'data, T: EntityStore>( &'a self, world: &'data T, ) -> ChunkDataIter<'data, V, ChunkViewIter<'data, 'a, V, F::ArchetypeFilter, F::ChunksetFilter, F::ChunkFilter>>

Gets an iterator which iterates through all entity data that matches the query. Does not perform static borrow checking.

§Safety

The normal borrowing restrictions apply for the duration of the iteration:

  • Components borrowed with Read access must not be borrowed mutably elsewhere.
  • Components borrowed with Write access must not be borrowed elsewhere at all.
§Panics

This function may panic if other code is concurrently accessing the same components.

Source

pub fn iter<'a, 'data, T: EntityStore>( &'a self, world: &'data T, ) -> ChunkDataIter<'data, V, ChunkViewIter<'data, 'a, V, F::ArchetypeFilter, F::ChunksetFilter, F::ChunkFilter>>
where V: ReadOnly,

Gets an iterator which iterates through all entity data that matches the query.

Source

pub fn iter_mut<'a, 'data, T: EntityStore>( &'a self, world: &'data mut T, ) -> ChunkDataIter<'data, V, ChunkViewIter<'data, 'a, V, F::ArchetypeFilter, F::ChunksetFilter, F::ChunkFilter>>

Gets an iterator which iterates through all entity data that matches the query.

Source

pub unsafe fn for_each_entities_unchecked<'a, 'data, T, W>( &'a self, world: &'data W, f: T, )
where T: Fn((Entity, <<V as View<'data>>::Iter as Iterator>::Item)), W: EntityStore,

Iterates through all entity data that matches the query. Does not perform static borrow checking.

§Safety

The normal borrowing restrictions apply for the duration of the iteration:

  • Components borrowed with Read access must not be borrowed mutably elsewhere.
  • Components borrowed with Write access must not be borrowed elsewhere at all.
§Panics

This function may panic if other code is concurrently accessing the same components.

Source

pub fn for_each_entities<'a, 'data, T, W>(&'a self, world: &'data W, f: T)
where T: Fn((Entity, <<V as View<'data>>::Iter as Iterator>::Item)), V: ReadOnly, W: EntityStore,

Iterates through all entity data that matches the query.

Source

pub fn for_each_entities_mut<'a, 'data, T, W>( &'a self, world: &'data mut W, f: T, )
where T: Fn((Entity, <<V as View<'data>>::Iter as Iterator>::Item)), W: EntityStore,

Iterates through all entity data that matches the query.

Source

pub unsafe fn for_each_unchecked<'a, 'data, T, W>( &'a self, world: &'data W, f: T, )
where T: Fn(<<V as View<'data>>::Iter as Iterator>::Item), W: EntityStore,

Iterates through all entity data that matches the query. Does not perform static borrow checking.

§Safety

The normal borrowing restrictions apply for the duration of the iteration:

  • Components borrowed with Read access must not be borrowed mutably elsewhere.
  • Components borrowed with Write access must not be borrowed elsewhere at all.
§Panics

This function may panic if other code is concurrently accessing the same components.

Source

pub fn for_each<'a, 'data, T, W>(&'a self, world: &'data W, f: T)
where T: Fn(<<V as View<'data>>::Iter as Iterator>::Item), V: ReadOnly, W: EntityStore,

Iterates through all entity data that matches the query.

Source

pub fn for_each_mut<'a, 'data, T, W>(&'a self, world: &'data mut W, f: T)
where T: Fn(<<V as View<'data>>::Iter as Iterator>::Item), W: EntityStore,

Iterates through all entity data that matches the query.

Source

pub fn components<'a, T: Component, W: EntityStore>( &self, world: &'a W, ) -> RefMapSet<'a, Vec<&'a T>>

Returns a RefMapSet of all components of a given type. This simplifies getting a slice of references to all components of type T that match the filter. This can be useful for passing to other libraries or FFI.

Source

pub fn components_mut<'a, T: Component, W: EntityStore>( &self, world: &'a mut W, ) -> RefMapMutSet<'a, Vec<&'a mut T>>

Returns a RefMapMutSet of all components of a given type. This simplifies getting a slice of mutable refs to all components of type T that match the filter.

Trait Implementations§

Source§

impl<V: for<'a> View<'a>, F> Clone for Query<V, F>
where F: Clone + EntityFilter,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<V, F> Freeze for Query<V, F>
where F: Freeze,

§

impl<V, F> RefUnwindSafe for Query<V, F>

§

impl<V, F> Send for Query<V, F>

§

impl<V, F> Sync for Query<V, F>
where F: Sync,

§

impl<V, F> Unpin for Query<V, F>
where F: Unpin, V: Unpin,

§

impl<V, F> UnwindSafe for Query<V, F>
where F: UnwindSafe, V: UnwindSafe,

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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

impl<T> WithSubscriber for T

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<T> Component for T
where T: Send + Sync + 'static,