pub struct NestedQuery<D, F = ()>(/* private fields */)
where
D: QueryData + 'static,
F: QueryFilter + 'static;Expand description
A helper type for accessing a Query within a QueryData.
This is intended to be used inside other implementations of QueryData,
either for manual implementations or #[derive(QueryData)].
It is not normally useful to query directly,
since it’s equivalent to adding another Query parameter to a system.
Note that this requires the inner query to be a ReadOnlyQueryData
to prevent mutable aliasing.
fn system(mut query: Query<NestedQuery<&A>>) {
// This works, because it performs read-only iteration
for a in &query {
let a: Query<&A> = a;
}
}fn system(mut query: Query<NestedQuery<&mut A>>) {
// This fails, because it would allow mutable aliasing of `&mut A`
for a in &mut query {
let a: Query<&mut A> = a;
}
}§Example
The simplest way to use a NestedQuery is with a #[derive(QueryData)] struct.
The Query will be available on the generated Item struct,
and we can use the query in methods on that struct.
// We want to create a relational query data
// that lets us query components on an entity's parent,
// like this:
let root = world.spawn(Data(3)).id();
let child = world.spawn(ChildOf(root)).id();
let mut query = world.query::<Parent<&Data>>();
let &Data(data) = query.query(&mut world).get(child).unwrap().data().unwrap();
assert_eq!(data, 3);
// We derive a query data struct that contains the relation plus a `NestedQuery`
#[derive(QueryData)]
struct Parent<D: ReadOnlyQueryData + 'static, F: QueryFilter + 'static = ()> {
// This will query `ChildOf` on the entity itself,
// so we can find the parent entity
parent: &'static ChildOf,
// This will provide a `Query` that we can use to
// query data on the parent entity
nested_query: NestedQuery<D, F>,
}
// And add a method on the generated item struct to invoke the nested query.
impl<'w, 's, D: ReadOnlyQueryData + 'static, F: QueryFilter + 'static> ParentItem<'w, 's, D, F> {
fn data(&self) -> Option<D::Item<'w, 's>> {
// We need to use `_inner` methods to return the full `'w` lifetime.
self.nested_query.get_inner(self.parent.parent()).ok()
}
}In order to make a query that returns the inner query data directly,
instead of through an intermediate Item struct,
you can implement QueryData manually by delegating to NestedQuery.
// We want to create a relational query data
// that lets us query components on an entity's parent,
// like this:
let root = world.spawn(Data(3)).id();
let child = world.spawn(ChildOf(root)).id();
let mut query = world.query::<Parent<&Data>>();
let &Data(data) = query.query(&mut world).get(child).unwrap();
assert_eq!(data, 3);
// This is the relational query data.
// This will never actually be constructed,
// and is only used as a `QueryData` type.
pub struct Parent<D: ReadOnlyQueryData, F: QueryFilter = ()>(D, F);
// A type alias to delegate the `QueryData` impls to.
// We need to refer to this type a lot, so the alias will help.
// This could also be a `#[derive(QueryData)]` type.
type ParentInner<D, F> = (
// This will query `ChildOf` on the entity itself,
// so we can find the parent entity
&'static ChildOf,
// This will provide a `Query` that we can use to
// query data on the parent entity
NestedQuery<D, F>,
);
unsafe impl<D: ReadOnlyQueryData + 'static, F: QueryFilter + 'static> QueryData for Parent<D, F> {
// Set `Item` to what we need for this relational query.
// Here we use the output of `D`.
type Item<'w, 's> = D::Item<'w, 's>;
unsafe fn fetch<'w, 's>(state: &'s Self::State, fetch: &mut Self::Fetch<'w>, entity: Entity, table_row: TableRow) -> Option<Self::Item<'w, 's>> {
// In `fetch`, first delegate to the type alias to get the parts:
let (&ChildOf(parent), nested_query) =
<ParentInner<D, F> as QueryData>::fetch(state, fetch, entity, table_row)?;
// Then use the `NestedQuery` to get the data we need.
// We need to use `_inner` methods to return the full `'w` lifetime.
nested_query.get_inner(parent).ok()
}
fn shrink<'wlong: 'wshort, 'wshort, 's>(item: Self::Item<'wlong, 's>) -> Self::Item<'wshort, 's> {
D::shrink(item)
}
// Set `ReadOnly` to `Self`,
// as `NestedQuery` does not yet support mutable queries.
type ReadOnly = Self;
// Delegate everything else on `QueryData` and `WorldQuery` to the type alias.
// This is sound for `unsafe` items because they delegate to the
// sound implementations on the type alias.
const IS_READ_ONLY: bool = <ParentInner<D, F> as QueryData>::IS_READ_ONLY;
const IS_ARCHETYPAL: bool = <ParentInner<D, F> as QueryData>::IS_ARCHETYPAL;
fn iter_access(state: &Self::State) -> impl Iterator<Item = EcsAccessType<'_>> {
<ParentInner<D, F> as QueryData>::iter_access(state)
}
}
unsafe impl<D: ReadOnlyQueryData + 'static, F: QueryFilter + 'static> WorldQuery for Parent<D, F> {
type Fetch<'w> = <ParentInner<D, F> as WorldQuery>::Fetch<'w>;
type State = <ParentInner<D, F> as WorldQuery>::State;
fn shrink_fetch<'wlong: 'wshort, 'wshort>(fetch: Self::Fetch<'wlong>) -> Self::Fetch<'wshort> {
<ParentInner<D, F> as WorldQuery>::shrink_fetch(fetch)
}
unsafe fn init_fetch<'w, 's>(world: UnsafeWorldCell<'w>, state: &'s Self::State, last_run: Tick, this_run: Tick) -> Self::Fetch<'w> {
<ParentInner<D, F> as WorldQuery>::init_fetch(world, state, last_run, this_run)
}
const IS_DENSE: bool = <ParentInner<D, F> as WorldQuery>::IS_DENSE;
unsafe fn set_archetype<'w, 's>(fetch: &mut Self::Fetch<'w>, state: &'s Self::State, archetype: &'w Archetype, table: &'w Table) {
<ParentInner<D, F> as WorldQuery>::set_archetype(fetch, state, archetype, table)
}
unsafe fn set_table<'w, 's>(fetch: &mut Self::Fetch<'w>, state: &'s Self::State, table: &'w Table) {
<ParentInner<D, F> as WorldQuery>::set_table(fetch, state, table)
}
fn update_component_access(state: &Self::State, access: &mut FilteredAccess) {
<ParentInner<D, F> as WorldQuery>::update_component_access(state, access)
}
fn init_state(world: &mut World) -> Self::State {
<ParentInner<D, F> as WorldQuery>::init_state(world)
}
fn get_state(components: &Components) -> Option<Self::State> {
<ParentInner<D, F> as WorldQuery>::get_state(components)
}
fn matches_component_set(state: &Self::State, set_contains_id: &impl Fn(ComponentId) -> bool) -> bool {
<ParentInner<D, F> as WorldQuery>::matches_component_set(state, set_contains_id)
}
}
// Also impl `ReadOnlyQueryData`, `IterQueryData`, and `ReleaseStateQueryData`
// These are safe because they delegate to the type alias, which is also read-only.
// Do *not* impl `ArchetypeQueryData`, because `fetch` sometimes returns `None`,
// and do *not* impl `SingleEntityQueryData`, because `NestedQuery` accesses other entities.
unsafe impl<D: ReadOnlyQueryData + 'static, F: QueryFilter + 'static> ReadOnlyQueryData for Parent<D, F> {}
unsafe impl<D: ReadOnlyQueryData + 'static, F: QueryFilter + 'static> IterQueryData for Parent<D, F> {}
impl<D: ReadOnlyQueryData + ReleaseStateQueryData + 'static, F: QueryFilter + 'static>
ReleaseStateQueryData for Parent<D, F>
{
fn release_state<'w>(item: Self::Item<'w, '_>) -> Self::Item<'w, 'static> {
D::release_state(item)
}
}Trait Implementations§
Source§impl<D, F> QueryData for NestedQuery<D, F>where
D: ReadOnlyQueryData + 'static,
F: QueryFilter + 'static,
impl<D, F> QueryData for NestedQuery<D, F>where
D: ReadOnlyQueryData + 'static,
F: QueryFilter + 'static,
Source§const IS_READ_ONLY: bool = D::IS_READ_ONLY
const IS_READ_ONLY: bool = D::IS_READ_ONLY
Source§const IS_ARCHETYPAL: bool = true
const IS_ARCHETYPAL: bool = true
Source§type ReadOnly = NestedQuery<D, F>
type ReadOnly = NestedQuery<D, F>
QueryData, which satisfies the ReadOnlyQueryData trait.Source§type Item<'w, 's> = Query<'w, 's, D, F>
type Item<'w, 's> = Query<'w, 's, D, F>
WorldQuery
This will be the data retrieved by the query,
and is visible to the end user when calling e.g. Query<Self>::get.Source§fn shrink<'wlong, 'wshort, 's>(
item: <NestedQuery<D, F> as QueryData>::Item<'wlong, 's>,
) -> <NestedQuery<D, F> as QueryData>::Item<'wshort, 's>where
'wlong: 'wshort,
fn shrink<'wlong, 'wshort, 's>(
item: <NestedQuery<D, F> as QueryData>::Item<'wlong, 's>,
) -> <NestedQuery<D, F> as QueryData>::Item<'wshort, 's>where
'wlong: 'wshort,
Source§unsafe fn fetch<'w, 's>(
state: &'s <NestedQuery<D, F> as WorldQuery>::State,
fetch: &mut <NestedQuery<D, F> as WorldQuery>::Fetch<'w>,
_entity: Entity,
_table_row: TableRow,
) -> Option<<NestedQuery<D, F> as QueryData>::Item<'w, 's>>
unsafe fn fetch<'w, 's>( state: &'s <NestedQuery<D, F> as WorldQuery>::State, fetch: &mut <NestedQuery<D, F> as WorldQuery>::Fetch<'w>, _entity: Entity, _table_row: TableRow, ) -> Option<<NestedQuery<D, F> as QueryData>::Item<'w, 's>>
Self::Item for either the given entity in the current Table,
or for the given entity in the current Archetype. This must always be called after
WorldQuery::set_table with a table_row in the range of the current Table or after
WorldQuery::set_archetype with an entity in the current archetype.
Accesses components registered in WorldQuery::update_component_access. Read moreSource§fn iter_access(
_state: &<NestedQuery<D, F> as WorldQuery>::State,
) -> impl Iterator<Item = EcsAccessType<'_>>
fn iter_access( _state: &<NestedQuery<D, F> as WorldQuery>::State, ) -> impl Iterator<Item = EcsAccessType<'_>>
QueryData::fetch. Access conflicts are usually
checked in WorldQuery::update_component_access, but in certain cases this method can be useful to implement
a way of checking for access conflicts in a non-allocating way.Source§fn provide_extra_access(
_state: &mut Self::State,
_access: &mut Access,
_available_access: &Access,
)
fn provide_extra_access( _state: &mut Self::State, _access: &mut Access, _available_access: &Access, )
update_component_access.
Implementations may add additional access that is a subset of available_access
and does not conflict with anything in access,
and must update access to include that access. Read moreSource§impl<D, F> WorldQuery for NestedQuery<D, F>where
D: ReadOnlyQueryData + 'static,
F: QueryFilter + 'static,
impl<D, F> WorldQuery for NestedQuery<D, F>where
D: ReadOnlyQueryData + 'static,
F: QueryFilter + 'static,
Source§const IS_DENSE: bool = true
const IS_DENSE: bool = true
Source§type Fetch<'w> = NestedQueryFetch<'w>
type Fetch<'w> = NestedQueryFetch<'w>
WorldQuery to compute Self::Item for each entity.Source§type State = QueryState<D, F>
type State = QueryState<D, F>
Self::Fetch. This will be cached inside QueryState,
so it is best to move as much data / computation here as possible to reduce the cost of
constructing Self::Fetch.Source§fn shrink_fetch<'wlong, 'wshort>(
fetch: <NestedQuery<D, F> as WorldQuery>::Fetch<'wlong>,
) -> <NestedQuery<D, F> as WorldQuery>::Fetch<'wshort>where
'wlong: 'wshort,
fn shrink_fetch<'wlong, 'wshort>(
fetch: <NestedQuery<D, F> as WorldQuery>::Fetch<'wlong>,
) -> <NestedQuery<D, F> as WorldQuery>::Fetch<'wshort>where
'wlong: 'wshort,
Source§unsafe fn init_fetch<'w, 's>(
world: UnsafeWorldCell<'w>,
_state: &'s <NestedQuery<D, F> as WorldQuery>::State,
last_run: Tick,
this_run: Tick,
) -> <NestedQuery<D, F> as WorldQuery>::Fetch<'w>
unsafe fn init_fetch<'w, 's>( world: UnsafeWorldCell<'w>, _state: &'s <NestedQuery<D, F> as WorldQuery>::State, last_run: Tick, this_run: Tick, ) -> <NestedQuery<D, F> as WorldQuery>::Fetch<'w>
Self::Fetch,
by combining data from the World with the cached Self::State.
Readonly accesses resources registered in WorldQuery::update_component_access. Read moreSource§unsafe fn set_archetype<'w>(
_fetch: &mut <NestedQuery<D, F> as WorldQuery>::Fetch<'w>,
_state: &<NestedQuery<D, F> as WorldQuery>::State,
_archetype: &'w Archetype,
_table: &'w Table,
)
unsafe fn set_archetype<'w>( _fetch: &mut <NestedQuery<D, F> as WorldQuery>::Fetch<'w>, _state: &<NestedQuery<D, F> as WorldQuery>::State, _archetype: &'w Archetype, _table: &'w Table, )
Archetype. This will always be called on
archetypes that match this WorldQuery. Read moreSource§unsafe fn set_table<'w>(
_fetch: &mut <NestedQuery<D, F> as WorldQuery>::Fetch<'w>,
_state: &<NestedQuery<D, F> as WorldQuery>::State,
_table: &'w Table,
)
unsafe fn set_table<'w>( _fetch: &mut <NestedQuery<D, F> as WorldQuery>::Fetch<'w>, _state: &<NestedQuery<D, F> as WorldQuery>::State, _table: &'w Table, )
Table. This will always be called on tables
that match this WorldQuery. Read moreSource§fn update_component_access(
_state: &<NestedQuery<D, F> as WorldQuery>::State,
_access: &mut FilteredAccess,
)
fn update_component_access( _state: &<NestedQuery<D, F> as WorldQuery>::State, _access: &mut FilteredAccess, )
Source§fn init_nested_access(
state: &<NestedQuery<D, F> as WorldQuery>::State,
system_name: Option<&str>,
component_access_set: &mut FilteredAccessSet,
world: UnsafeWorldCell<'_>,
)
fn init_nested_access( state: &<NestedQuery<D, F> as WorldQuery>::State, system_name: Option<&str>, component_access_set: &mut FilteredAccessSet, world: UnsafeWorldCell<'_>, )
WorldQuery. Read moreSource§fn init_state(world: &mut World) -> <NestedQuery<D, F> as WorldQuery>::State
fn init_state(world: &mut World) -> <NestedQuery<D, F> as WorldQuery>::State
State for this WorldQuery type.Source§fn get_state(
_components: &Components,
) -> Option<<NestedQuery<D, F> as WorldQuery>::State>
fn get_state( _components: &Components, ) -> Option<<NestedQuery<D, F> as WorldQuery>::State>
Source§fn matches_component_set(
_state: &<NestedQuery<D, F> as WorldQuery>::State,
_set_contains_id: &impl Fn(ComponentId) -> bool,
) -> bool
fn matches_component_set( _state: &<NestedQuery<D, F> as WorldQuery>::State, _set_contains_id: &impl Fn(ComponentId) -> bool, ) -> bool
Source§fn update_archetypes(
state: &mut <NestedQuery<D, F> as WorldQuery>::State,
world: UnsafeWorldCell<'_>,
)
fn update_archetypes( state: &mut <NestedQuery<D, F> as WorldQuery>::State, world: UnsafeWorldCell<'_>, )
impl<D, F> ArchetypeQueryData for NestedQuery<D, F>where
D: ReadOnlyQueryData,
F: QueryFilter,
impl<D, F> IterQueryData for NestedQuery<D, F>where
D: ReadOnlyQueryData,
F: QueryFilter,
impl<D, F> ReadOnlyQueryData for NestedQuery<D, F>where
D: ReadOnlyQueryData,
F: QueryFilter,
Auto Trait Implementations§
impl<D, F> Freeze for NestedQuery<D, F>
impl<D, F = ()> !RefUnwindSafe for NestedQuery<D, F>
impl<D, F> Send for NestedQuery<D, F>
impl<D, F> Sync for NestedQuery<D, F>
impl<D, F> Unpin for NestedQuery<D, F>
impl<D, F> UnsafeUnpin for NestedQuery<D, F>
impl<D, F = ()> !UnwindSafe for NestedQuery<D, F>
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> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§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.Source§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.Source§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.Source§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> DowncastSend for T
impl<T> DowncastSend for T
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