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: FImplementations§
Source§impl<V, F> Query<V, F>where
V: for<'a> View<'a>,
F: EntityFilter,
impl<V, F> Query<V, F>where
V: for<'a> View<'a>,
F: EntityFilter,
Sourcepub fn filter<T: EntityFilter>(
self,
filter: T,
) -> Query<V, <F as BitAnd<T>>::Output>
pub fn filter<T: EntityFilter>( self, filter: T, ) -> Query<V, <F as BitAnd<T>>::Output>
Adds an additional filter to the query.
Sourcepub unsafe fn iter_chunks_unchecked<'a, 'data, T: EntityStore>(
&'a self,
world: &'data T,
) -> ChunkViewIter<'data, 'a, V, F::ArchetypeFilter, F::ChunksetFilter, F::ChunkFilter> ⓘ
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
Readaccess must not be borrowed mutably elsewhere. - Components borrowed with
Writeaccess must not be borrowed elsewhere at all.
§Panics
This function may panic if other code is concurrently accessing the same components.
Sourcepub 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,
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.
Sourcepub fn iter_chunks_mut<'a, 'data, T: EntityStore>(
&'a self,
world: &'data mut T,
) -> ChunkViewIter<'data, 'a, V, F::ArchetypeFilter, F::ChunksetFilter, F::ChunkFilter> ⓘ
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.
Sourcepub 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>> ⓘ
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
Readaccess must not be borrowed mutably elsewhere. - Components borrowed with
Writeaccess must not be borrowed elsewhere at all.
§Panics
This function may panic if other code is concurrently accessing the same components.
Sourcepub 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,
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.
Sourcepub 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>> ⓘ
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.
Sourcepub 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>> ⓘ
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
Readaccess must not be borrowed mutably elsewhere. - Components borrowed with
Writeaccess must not be borrowed elsewhere at all.
§Panics
This function may panic if other code is concurrently accessing the same components.
Sourcepub 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,
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.
Sourcepub 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>> ⓘ
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.
Sourcepub unsafe fn for_each_entities_unchecked<'a, 'data, T, W>(
&'a self,
world: &'data W,
f: T,
)
pub unsafe fn for_each_entities_unchecked<'a, 'data, T, W>( &'a self, world: &'data W, f: T, )
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
Readaccess must not be borrowed mutably elsewhere. - Components borrowed with
Writeaccess must not be borrowed elsewhere at all.
§Panics
This function may panic if other code is concurrently accessing the same components.
Sourcepub fn for_each_entities<'a, 'data, T, W>(&'a self, world: &'data W, f: T)
pub fn for_each_entities<'a, 'data, T, W>(&'a self, world: &'data W, f: T)
Iterates through all entity data that matches the query.
Sourcepub fn for_each_entities_mut<'a, 'data, T, W>(
&'a self,
world: &'data mut W,
f: T,
)
pub fn for_each_entities_mut<'a, 'data, T, W>( &'a self, world: &'data mut W, f: T, )
Iterates through all entity data that matches the query.
Sourcepub unsafe fn for_each_unchecked<'a, 'data, T, W>(
&'a self,
world: &'data W,
f: T,
)
pub unsafe fn for_each_unchecked<'a, 'data, T, W>( &'a self, world: &'data W, f: T, )
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
Readaccess must not be borrowed mutably elsewhere. - Components borrowed with
Writeaccess must not be borrowed elsewhere at all.
§Panics
This function may panic if other code is concurrently accessing the same components.
Sourcepub fn for_each<'a, 'data, T, W>(&'a self, world: &'data W, f: T)
pub fn for_each<'a, 'data, T, W>(&'a self, world: &'data W, f: T)
Iterates through all entity data that matches the query.
Sourcepub fn for_each_mut<'a, 'data, T, W>(&'a self, world: &'data mut W, f: T)
pub fn for_each_mut<'a, 'data, T, W>(&'a self, world: &'data mut W, f: T)
Iterates through all entity data that matches the query.
Sourcepub fn components<'a, T: Component, W: EntityStore>(
&self,
world: &'a W,
) -> RefMapSet<'a, Vec<&'a T>>
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.
Sourcepub fn components_mut<'a, T: Component, W: EntityStore>(
&self,
world: &'a mut W,
) -> RefMapMutSet<'a, Vec<&'a mut T>>
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§
Auto Trait Implementations§
impl<V, F> Freeze for Query<V, F>where
F: Freeze,
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>
impl<V, F> UnwindSafe for Query<V, F>where
F: UnwindSafe,
V: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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