[][src]Struct legion_core::query::Query

pub struct Query<V: for<'a> View<'a>, F: EntityFilter> {
    pub filter: F,
    // some fields omitted
}

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

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

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
[src]

Adds an additional filter to the query.

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>
[src]

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.

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, 
[src]

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

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>
[src]

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

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>>
[src]

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.

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, 
[src]

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

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>>
[src]

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

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>>
[src]

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.

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, 
[src]

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

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>>
[src]

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

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
[src]

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.

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
[src]

Iterates through all entity data that matches the query.

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
[src]

Iterates through all entity data that matches the query.

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
[src]

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.

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
[src]

Iterates through all entity data that matches the query.

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
[src]

Iterates through all entity data that matches the query.

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

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.

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

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

impl<V: for<'a> View<'a>, F: EntityFilter> Clone for Query<V, F> where
    F: Clone
[src]

Auto Trait Implementations

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

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Component for T where
    T: 'static + Send + Sync
[src]

impl<T> Downcast for T where
    T: Any
[src]

impl<T> DowncastSync for T where
    T: Send + Sync + Any
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.