From 47381fcf6cc6fb761e3a4f74fba893c6ebdd2198 Mon Sep 17 00:00:00 2001 From: Boxy Date: Thu, 3 Nov 2022 14:39:17 +0000 Subject: [PATCH] Do It --- .../src/core_2d/camera_2d.rs | 2 +- .../src/core_3d/camera_3d.rs | 2 +- crates/bevy_ecs/macros/src/fetch.rs | 33 ++- crates/bevy_ecs/src/query/fetch.rs | 221 +++++++----------- crates/bevy_ecs/src/query/filter.rs | 109 ++++----- crates/bevy_ecs/src/query/iter.rs | 32 +-- crates/bevy_ecs/src/query/state.rs | 36 ++- crates/bevy_ecs/src/system/query.rs | 25 +- crates/bevy_hierarchy/src/query_extension.rs | 22 +- crates/bevy_render/src/extract_component.rs | 4 +- crates/bevy_ui/src/entity.rs | 2 +- examples/shader/shader_instancing.rs | 7 +- 12 files changed, 200 insertions(+), 295 deletions(-) diff --git a/crates/bevy_core_pipeline/src/core_2d/camera_2d.rs b/crates/bevy_core_pipeline/src/core_2d/camera_2d.rs index ca426788529fe..711fda3a57a2a 100644 --- a/crates/bevy_core_pipeline/src/core_2d/camera_2d.rs +++ b/crates/bevy_core_pipeline/src/core_2d/camera_2d.rs @@ -19,7 +19,7 @@ impl ExtractComponent for Camera2d { type Query = &'static Self; type Filter = With; - fn extract_component(item: QueryItem) -> Self { + fn extract_component(item: QueryItem<'_, Self::Query>) -> Self { item.clone() } } diff --git a/crates/bevy_core_pipeline/src/core_3d/camera_3d.rs b/crates/bevy_core_pipeline/src/core_3d/camera_3d.rs index c5309c7c045b3..20a0001457a9f 100644 --- a/crates/bevy_core_pipeline/src/core_3d/camera_3d.rs +++ b/crates/bevy_core_pipeline/src/core_3d/camera_3d.rs @@ -51,7 +51,7 @@ impl ExtractComponent for Camera3d { type Query = &'static Self; type Filter = With; - fn extract_component(item: QueryItem) -> Self { + fn extract_component(item: QueryItem<'_, Self::Query>) -> Self { item.clone() } } diff --git a/crates/bevy_ecs/macros/src/fetch.rs b/crates/bevy_ecs/macros/src/fetch.rs index 7965c8ce2def2..8e4e1cfe790d6 100644 --- a/crates/bevy_ecs/macros/src/fetch.rs +++ b/crates/bevy_ecs/macros/src/fetch.rs @@ -185,7 +185,7 @@ pub fn derive_world_query_impl(ast: DeriveInput) -> TokenStream { #[doc = "`], returned when iterating over query results."] #[automatically_derived] #visibility struct #item_struct_name #user_impl_generics_with_world #user_where_clauses_with_world { - #(#(#field_attrs)* #field_visibilities #field_idents: <#field_types as #path::query::WorldQueryGats<'__w>>::Item,)* + #(#(#field_attrs)* #field_visibilities #field_idents: <#field_types as #path::query::WorldQuery>::Item<'__w>,)* #(#(#ignored_field_attrs)* #ignored_field_visibilities #ignored_field_idents: #ignored_field_types,)* } @@ -195,27 +195,22 @@ pub fn derive_world_query_impl(ast: DeriveInput) -> TokenStream { #[doc = "`], used to define the world data accessed by this query."] #[automatically_derived] #visibility struct #fetch_struct_name #user_impl_generics_with_world #user_where_clauses_with_world { - #(#field_idents: <#field_types as #path::query::WorldQueryGats<'__w>>::Fetch,)* + #(#field_idents: <#field_types as #path::query::WorldQuery>::Fetch<'__w>,)* #(#ignored_field_idents: #ignored_field_types,)* } // SAFETY: `update_component_access` and `update_archetype_component_access` are called on every field - - impl #user_impl_generics_with_world #path::query::WorldQueryGats<'__w> - for #struct_name #user_ty_generics #user_where_clauses { - type Item = #item_struct_name #user_ty_generics_with_world; - type Fetch = #fetch_struct_name #user_ty_generics_with_world; - } - unsafe impl #user_impl_generics #path::query::WorldQuery for #struct_name #user_ty_generics #user_where_clauses { + type Item<'__w> = #item_struct_name #user_ty_generics_with_world; + type Fetch<'__w> = #fetch_struct_name #user_ty_generics_with_world; type ReadOnly = #read_only_struct_name #user_ty_generics; type State = #state_struct_name #user_ty_generics; fn shrink<'__wlong: '__wshort, '__wshort>( - item: <#struct_name #user_ty_generics as #path::query::WorldQueryGats<'__wlong>>::Item - ) -> <#struct_name #user_ty_generics as #path::query::WorldQueryGats<'__wshort>>::Item { + item: <#struct_name #user_ty_generics as #path::query::WorldQuery>::Item<'__wlong> + ) -> <#struct_name #user_ty_generics as #path::query::WorldQuery>::Item<'__wshort> { #item_struct_name { #( #field_idents: <#field_types>::shrink(item.#field_idents), @@ -231,7 +226,7 @@ pub fn derive_world_query_impl(ast: DeriveInput) -> TokenStream { state: &Self::State, _last_change_tick: u32, _change_tick: u32 - ) -> >::Fetch { + ) -> ::Fetch<'__w> { #fetch_struct_name { #(#field_idents: <#field_types>::init_fetch( @@ -246,8 +241,8 @@ pub fn derive_world_query_impl(ast: DeriveInput) -> TokenStream { } unsafe fn clone_fetch<'__w>( - _fetch: &>::Fetch - ) -> >::Fetch { + _fetch: &::Fetch<'__w> + ) -> ::Fetch<'__w> { #fetch_struct_name { #( #field_idents: <#field_types>::clone_fetch(& _fetch. #field_idents), @@ -265,7 +260,7 @@ pub fn derive_world_query_impl(ast: DeriveInput) -> TokenStream { /// SAFETY: we call `set_archetype` for each member that implements `Fetch` #[inline] unsafe fn set_archetype<'__w>( - _fetch: &mut >::Fetch, + _fetch: &mut ::Fetch<'__w>, _state: &Self::State, _archetype: &'__w #path::archetype::Archetype, _table: &'__w #path::storage::Table @@ -276,7 +271,7 @@ pub fn derive_world_query_impl(ast: DeriveInput) -> TokenStream { /// SAFETY: we call `set_table` for each member that implements `Fetch` #[inline] unsafe fn set_table<'__w>( - _fetch: &mut >::Fetch, + _fetch: &mut ::Fetch<'__w>, _state: &Self::State, _table: &'__w #path::storage::Table ) { @@ -286,10 +281,10 @@ pub fn derive_world_query_impl(ast: DeriveInput) -> TokenStream { /// SAFETY: we call `fetch` for each member that implements `Fetch`. #[inline(always)] unsafe fn fetch<'__w>( - _fetch: &mut >::Fetch, + _fetch: &mut ::Fetch<'__w>, _entity: Entity, _table_row: usize - ) -> >::Item { + ) -> ::Item<'__w> { Self::Item { #(#field_idents: <#field_types>::fetch(&mut _fetch.#field_idents, _entity, _table_row),)* #(#ignored_field_idents: Default::default(),)* @@ -299,7 +294,7 @@ pub fn derive_world_query_impl(ast: DeriveInput) -> TokenStream { #[allow(unused_variables)] #[inline(always)] unsafe fn filter_fetch<'__w>( - _fetch: &mut >::Fetch, + _fetch: &mut ::Fetch<'__w>, _entity: Entity, _table_row: usize ) -> bool { diff --git a/crates/bevy_ecs/src/query/fetch.rs b/crates/bevy_ecs/src/query/fetch.rs index b80000de7be0e..d87f2f959a2b2 100644 --- a/crates/bevy_ecs/src/query/fetch.rs +++ b/crates/bevy_ecs/src/query/fetch.rs @@ -283,8 +283,8 @@ use std::{cell::UnsafeCell, marker::PhantomData}; /// /// # Safety /// -/// Component access of `ROQueryFetch` must be a subset of `QueryFetch` -/// and `ROQueryFetch` must match exactly the same archetypes/tables as `QueryFetch` +/// Component access of `Self::ReadOnly` must be a subset of `Self` +/// and `Self::ReadOnly` must match exactly the same archetypes/tables as `Self` /// /// Implementor must ensure that /// [`update_component_access`] and [`update_archetype_component_access`] @@ -296,7 +296,7 @@ use std::{cell::UnsafeCell, marker::PhantomData}; /// [`Added`]: crate::query::Added /// [`fetch`]: Self::fetch /// [`Changed`]: crate::query::Changed -/// [`Fetch`]: crate::query::WorldQueryGats::Fetch +/// [`Fetch`]: crate::query::WorldQuery::Fetch /// [`matches_component_set`]: Self::matches_component_set /// [`Or`]: crate::query::Or /// [`Query`]: crate::system::Query @@ -306,17 +306,23 @@ use std::{cell::UnsafeCell, marker::PhantomData}; /// [`update_component_access`]: Self::update_component_access /// [`With`]: crate::query::With /// [`Without`]: crate::query::Without -pub unsafe trait WorldQuery: for<'w> WorldQueryGats<'w> { +pub unsafe trait WorldQuery { + /// The item returned by this [`WorldQuery`] + type Item<'a>; + + /// Per archetype/table state used by this [`WorldQuery`] to fetch [`Self::Item`](crate::query::WorldQuery::Item) + type Fetch<'a>; + /// The read-only variant of this [`WorldQuery`], which satisfies the [`ReadOnlyWorldQuery`] trait. type ReadOnly: ReadOnlyWorldQuery; - /// State used to construct a [`Self::Fetch`](crate::query::WorldQueryGats::Fetch). This will be cached inside [`QueryState`](crate::query::QueryState), + /// State used to construct a [`Self::Fetch`](crate::query::WorldQuery::Fetch). This will be cached inside [`QueryState`](crate::query::QueryState), /// so it is best to move as much data / computation here as possible to reduce the cost of - /// constructing [`Self::Fetch`](crate::query::WorldQueryGats::Fetch). + /// constructing [`Self::Fetch`](crate::query::WorldQuery::Fetch). type State: Send + Sync + Sized; /// This function manually implements subtyping for the query items. - fn shrink<'wlong: 'wshort, 'wshort>(item: QueryItem<'wlong, Self>) -> QueryItem<'wshort, Self>; + fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort>; /// Creates a new instance of this fetch. /// @@ -329,7 +335,7 @@ pub unsafe trait WorldQuery: for<'w> WorldQueryGats<'w> { state: &Self::State, last_change_tick: u32, change_tick: u32, - ) -> >::Fetch; + ) -> Self::Fetch<'w>; /// While this function can be called for any query, it is always safe to call if `Self: ReadOnlyWorldQuery` holds. /// @@ -337,9 +343,7 @@ pub unsafe trait WorldQuery: for<'w> WorldQueryGats<'w> { /// While calling this method on its own cannot cause UB it is marked `unsafe` as the caller must ensure /// that the returned value is not used in any way that would cause two `QueryItem` for the same /// `archetype_index` or `table_row` to be alive at the same time. - unsafe fn clone_fetch<'w>( - fetch: &>::Fetch, - ) -> >::Fetch; + unsafe fn clone_fetch<'w>(fetch: &Self::Fetch<'w>) -> Self::Fetch<'w>; /// Returns true if (and only if) every table of every archetype matched by this fetch contains /// all of the matched components. This is used to select a more efficient "table iterator" @@ -364,7 +368,7 @@ pub unsafe trait WorldQuery: for<'w> WorldQueryGats<'w> { /// `archetype` and `tables` must be from the [`World`] [`WorldQuery::init_state`] was called on. `state` must /// be the [`Self::State`] this was initialized with. unsafe fn set_archetype<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, state: &Self::State, archetype: &'w Archetype, table: &'w Table, @@ -377,13 +381,9 @@ pub unsafe trait WorldQuery: for<'w> WorldQueryGats<'w> { /// /// `table` must be from the [`World`] [`WorldQuery::init_state`] was called on. `state` must be the /// [`Self::State`] this was initialized with. - unsafe fn set_table<'w>( - fetch: &mut >::Fetch, - state: &Self::State, - table: &'w Table, - ); + unsafe fn set_table<'w>(fetch: &mut Self::Fetch<'w>, state: &Self::State, table: &'w Table); - /// Fetch [`Self::Item`](`WorldQueryGats::Item`) for either the given `entity` in the current [`Table`], + /// Fetch [`Self::Item`](`WorldQuery::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 a `entity` in the current archetype. @@ -393,10 +393,10 @@ pub unsafe trait WorldQuery: for<'w> WorldQueryGats<'w> { /// Must always be called _after_ [`WorldQuery::set_table`] or [`WorldQuery::set_archetype`]. `entity` and /// `table_row` must be in the range of the current table and archetype. unsafe fn fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, entity: Entity, table_row: usize, - ) -> >::Item; + ) -> Self::Item<'w>; /// # Safety /// @@ -404,11 +404,7 @@ pub unsafe trait WorldQuery: for<'w> WorldQueryGats<'w> { /// `table_row` must be in the range of the current table and archetype. #[allow(unused_variables)] #[inline(always)] - unsafe fn filter_fetch( - fetch: &mut >::Fetch, - entity: Entity, - table_row: usize, - ) -> bool { + unsafe fn filter_fetch(fetch: &mut Self::Fetch<'_>, entity: Entity, table_row: usize) -> bool { true } @@ -430,14 +426,6 @@ pub unsafe trait WorldQuery: for<'w> WorldQueryGats<'w> { ) -> bool; } -/// A helper trait for [`WorldQuery`] that works around Rust's lack of Generic Associated Types. -/// -/// **Note**: Consider using the type aliases [`QueryItem`] and [`QueryFetch`] when using `Item` or `Fetch`. -pub trait WorldQueryGats<'world> { - type Item; - type Fetch; -} - /// A world query that is read only. /// /// # Safety @@ -446,9 +434,9 @@ pub trait WorldQueryGats<'world> { pub unsafe trait ReadOnlyWorldQuery: WorldQuery {} /// The `Fetch` of a [`WorldQuery`], which is used to store state for each archetype/table. -pub type QueryFetch<'w, Q> = >::Fetch; +pub type QueryFetch<'w, Q> = ::Fetch<'w>; /// The item type returned when a [`WorldQuery`] is iterated over -pub type QueryItem<'w, Q> = >::Item; +pub type QueryItem<'w, Q> = ::Item<'w>; /// The read-only `Fetch` of a [`WorldQuery`], which is used to store state for each archetype/table. pub type ROQueryFetch<'w, Q> = QueryFetch<'w, ::ReadOnly>; /// The read-only variant of the item type returned when a [`WorldQuery`] is iterated over immutably @@ -456,10 +444,12 @@ pub type ROQueryItem<'w, Q> = QueryItem<'w, ::ReadOnly>; /// SAFETY: no component or archetype access unsafe impl WorldQuery for Entity { + type Fetch<'w> = (); + type Item<'w> = Entity; type ReadOnly = Self; type State = (); - fn shrink<'wlong: 'wshort, 'wshort>(item: QueryItem<'wlong, Self>) -> QueryItem<'wshort, Self> { + fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> { item } @@ -472,17 +462,14 @@ unsafe impl WorldQuery for Entity { _state: &Self::State, _last_change_tick: u32, _change_tick: u32, - ) -> >::Fetch { + ) -> Self::Fetch<'w> { } - unsafe fn clone_fetch<'w>( - _fetch: &>::Fetch, - ) -> >::Fetch { - } + unsafe fn clone_fetch<'w>(_fetch: &Self::Fetch<'w>) -> Self::Fetch<'w> {} #[inline] unsafe fn set_archetype<'w>( - _fetch: &mut >::Fetch, + _fetch: &mut Self::Fetch<'w>, _state: &Self::State, _archetype: &'w Archetype, _table: &Table, @@ -490,19 +477,15 @@ unsafe impl WorldQuery for Entity { } #[inline] - unsafe fn set_table<'w>( - _fetch: &mut >::Fetch, - _state: &Self::State, - _table: &'w Table, - ) { + unsafe fn set_table<'w>(_fetch: &mut Self::Fetch<'w>, _state: &Self::State, _table: &'w Table) { } #[inline(always)] unsafe fn fetch<'w>( - _fetch: &mut >::Fetch, + _fetch: &mut Self::Fetch<'w>, entity: Entity, _table_row: usize, - ) -> >::Item { + ) -> Self::Item<'w> { entity } @@ -525,11 +508,6 @@ unsafe impl WorldQuery for Entity { } } -impl<'w> WorldQueryGats<'w> for Entity { - type Fetch = (); - type Item = Entity; -} - /// SAFETY: access is read only unsafe impl ReadOnlyWorldQuery for Entity {} @@ -541,8 +519,10 @@ pub struct ReadFetch<'w, T> { sparse_set: Option<&'w ComponentSparseSet>, } -/// SAFETY: `ROQueryFetch` is the same as `QueryFetch` +/// SAFETY: `Self` is the same as `Self::ReadOnly` unsafe impl WorldQuery for &T { + type Fetch<'w> = ReadFetch<'w, T>; + type Item<'w> = &'w T; type ReadOnly = Self; type State = ComponentId; @@ -577,9 +557,7 @@ unsafe impl WorldQuery for &T { } } - unsafe fn clone_fetch<'w>( - fetch: &>::Fetch, - ) -> >::Fetch { + unsafe fn clone_fetch<'w>(fetch: &Self::Fetch<'w>) -> Self::Fetch<'w> { ReadFetch { table_components: fetch.table_components, sparse_set: fetch.sparse_set, @@ -615,10 +593,10 @@ unsafe impl WorldQuery for &T { #[inline(always)] unsafe fn fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, entity: Entity, table_row: usize, - ) -> >::Item { + ) -> Self::Item<'w> { match T::Storage::STORAGE_TYPE { StorageType::Table => fetch .table_components @@ -671,11 +649,6 @@ unsafe impl WorldQuery for &T { /// SAFETY: access is read only unsafe impl ReadOnlyWorldQuery for &T {} -impl<'w, T: Component> WorldQueryGats<'w> for &T { - type Fetch = ReadFetch<'w, T>; - type Item = &'w T; -} - #[doc(hidden)] pub struct WriteFetch<'w, T> { // T::Storage = TableStorage @@ -692,6 +665,8 @@ pub struct WriteFetch<'w, T> { /// SAFETY: access of `&T` is a subset of `&mut T` unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T { + type Fetch<'w> = WriteFetch<'w, T>; + type Item<'w> = Mut<'w, T>; type ReadOnly = &'__w T; type State = ComponentId; @@ -728,9 +703,7 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T { } } - unsafe fn clone_fetch<'w>( - fetch: &>::Fetch, - ) -> >::Fetch { + unsafe fn clone_fetch<'w>(fetch: &Self::Fetch<'w>) -> Self::Fetch<'w> { WriteFetch { table_data: fetch.table_data, sparse_set: fetch.sparse_set, @@ -768,10 +741,10 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T { #[inline(always)] unsafe fn fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, entity: Entity, table_row: usize, - ) -> >::Item { + ) -> Self::Item<'w> { match T::Storage::STORAGE_TYPE { StorageType::Table => { let (table_components, table_ticks) = fetch @@ -838,23 +811,20 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T { } } -impl<'w, T: Component> WorldQueryGats<'w> for &mut T { - type Fetch = WriteFetch<'w, T>; - type Item = Mut<'w, T>; -} - #[doc(hidden)] pub struct OptionFetch<'w, T: WorldQuery> { - fetch: >::Fetch, + fetch: T::Fetch<'w>, matches: bool, } // SAFETY: defers to soundness of `T: WorldQuery` impl unsafe impl WorldQuery for Option { + type Fetch<'w> = OptionFetch<'w, T>; + type Item<'w> = Option>; type ReadOnly = Option; type State = T::State; - fn shrink<'wlong: 'wshort, 'wshort>(item: QueryItem<'wlong, Self>) -> QueryItem<'wshort, Self> { + fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> { item.map(T::shrink) } @@ -874,9 +844,7 @@ unsafe impl WorldQuery for Option { } } - unsafe fn clone_fetch<'w>( - fetch: &>::Fetch, - ) -> >::Fetch { + unsafe fn clone_fetch<'w>(fetch: &Self::Fetch<'w>) -> Self::Fetch<'w> { OptionFetch { fetch: T::clone_fetch(&fetch.fetch), matches: fetch.matches, @@ -906,10 +874,10 @@ unsafe impl WorldQuery for Option { #[inline(always)] unsafe fn fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, entity: Entity, table_row: usize, - ) -> >::Item { + ) -> Self::Item<'w> { fetch .matches .then(|| T::fetch(&mut fetch.fetch, entity, table_row)) @@ -950,11 +918,6 @@ unsafe impl WorldQuery for Option { /// SAFETY: [`OptionFetch`] is read only because `T` is read only unsafe impl ReadOnlyWorldQuery for Option {} -impl<'w, T: WorldQuery> WorldQueryGats<'w> for Option { - type Fetch = OptionFetch<'w, T>; - type Item = Option>; -} - /// [`WorldQuery`] that tracks changes and additions for component `T`. /// /// Wraps a [`Component`] to track whether the component changed for the corresponding entities in @@ -1044,10 +1007,12 @@ pub struct ChangeTrackersFetch<'w, T> { // SAFETY: `ROQueryFetch` is the same as `QueryFetch` unsafe impl WorldQuery for ChangeTrackers { + type Fetch<'w> = ChangeTrackersFetch<'w, T>; + type Item<'w> = ChangeTrackers; type ReadOnly = Self; type State = ComponentId; - fn shrink<'wlong: 'wshort, 'wshort>(item: QueryItem<'wlong, Self>) -> QueryItem<'wshort, Self> { + fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> { item } @@ -1081,9 +1046,7 @@ unsafe impl WorldQuery for ChangeTrackers { } } - unsafe fn clone_fetch<'w>( - fetch: &>::Fetch, - ) -> >::Fetch { + unsafe fn clone_fetch<'w>(fetch: &Self::Fetch<'w>) -> Self::Fetch<'w> { ChangeTrackersFetch { table_ticks: fetch.table_ticks, sparse_set: fetch.sparse_set, @@ -1122,10 +1085,10 @@ unsafe impl WorldQuery for ChangeTrackers { #[inline(always)] unsafe fn fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, entity: Entity, table_row: usize, - ) -> >::Item { + ) -> Self::Item<'w> { match T::Storage::STORAGE_TYPE { StorageType::Table => ChangeTrackers { component_ticks: { @@ -1186,28 +1149,18 @@ unsafe impl WorldQuery for ChangeTrackers { /// SAFETY: access is read only unsafe impl ReadOnlyWorldQuery for ChangeTrackers {} -impl<'w, T: Component> WorldQueryGats<'w> for ChangeTrackers { - type Fetch = ChangeTrackersFetch<'w, T>; - type Item = ChangeTrackers; -} - macro_rules! impl_tuple_fetch { ($(($name: ident, $state: ident)),*) => { - #[allow(unused_variables)] - #[allow(non_snake_case)] - impl<'w, $($name: WorldQueryGats<'w>),*> WorldQueryGats<'w> for ($($name,)*) { - type Fetch = ($($name::Fetch,)*); - type Item = ($($name::Item,)*); - } - #[allow(non_snake_case)] #[allow(clippy::unused_unit)] // SAFETY: defers to soundness `$name: WorldQuery` impl unsafe impl<$($name: WorldQuery),*> WorldQuery for ($($name,)*) { + type Fetch<'w> = ($($name::Fetch<'w>,)*); + type Item<'w> = ($($name::Item<'w>,)*); type ReadOnly = ($($name::ReadOnly,)*); type State = ($($name::State,)*); - fn shrink<'wlong: 'wshort, 'wshort>(item: QueryItem<'wlong, Self>) -> QueryItem<'wshort, Self> { + fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> { let ($($name,)*) = item; ($( $name::shrink($name), @@ -1215,14 +1168,14 @@ macro_rules! impl_tuple_fetch { } #[allow(clippy::unused_unit)] - unsafe fn init_fetch<'w>(_world: &'w World, state: &Self::State, _last_change_tick: u32, _change_tick: u32) -> >::Fetch { + unsafe fn init_fetch<'w>(_world: &'w World, state: &Self::State, _last_change_tick: u32, _change_tick: u32) -> Self::Fetch<'w> { let ($($name,)*) = state; ($($name::init_fetch(_world, $name, _last_change_tick, _change_tick),)*) } unsafe fn clone_fetch<'w>( - fetch: &>::Fetch, - ) -> >::Fetch { + fetch: &Self::Fetch<'w>, + ) -> Self::Fetch<'w> { let ($($name,)*) = &fetch; ($($name::clone_fetch($name),)*) } @@ -1233,7 +1186,7 @@ macro_rules! impl_tuple_fetch { #[inline] unsafe fn set_archetype<'w>( - _fetch: &mut >::Fetch, + _fetch: &mut Self::Fetch<'w>, _state: &Self::State, _archetype: &'w Archetype, _table: &'w Table @@ -1244,7 +1197,7 @@ macro_rules! impl_tuple_fetch { } #[inline] - unsafe fn set_table<'w>(_fetch: &mut >::Fetch, _state: &Self::State, _table: &'w Table) { + unsafe fn set_table<'w>(_fetch: &mut Self::Fetch<'w>, _state: &Self::State, _table: &'w Table) { let ($($name,)*) = _fetch; let ($($state,)*) = _state; $($name::set_table($name, $state, _table);)* @@ -1253,17 +1206,17 @@ macro_rules! impl_tuple_fetch { #[inline(always)] #[allow(clippy::unused_unit)] unsafe fn fetch<'w>( - _fetch: &mut >::Fetch, + _fetch: &mut Self::Fetch<'w>, _entity: Entity, _table_row: usize - ) -> >::Item { + ) -> Self::Item<'w> { let ($($name,)*) = _fetch; ($($name::fetch($name, _entity, _table_row),)*) } #[inline(always)] unsafe fn filter_fetch<'w>( - _fetch: &mut >::Fetch, + _fetch: &mut Self::Fetch<'w>, _entity: Entity, _table_row: usize ) -> bool { @@ -1307,21 +1260,16 @@ pub struct AnyOf(PhantomData); macro_rules! impl_anytuple_fetch { ($(($name: ident, $state: ident)),*) => { - #[allow(unused_variables)] - #[allow(non_snake_case)] - impl<'w, $($name: WorldQueryGats<'w>),*> WorldQueryGats<'w> for AnyOf<($($name,)*)> { - type Fetch = ($(($name::Fetch, bool),)*); - type Item = ($(Option<$name::Item>,)*); - } - #[allow(non_snake_case)] #[allow(clippy::unused_unit)] // SAFETY: defers to soundness of `$name: WorldQuery` impl unsafe impl<$($name: WorldQuery),*> WorldQuery for AnyOf<($($name,)*)> { + type Fetch<'w> = ($(($name::Fetch<'w>, bool),)*); + type Item<'w> = ($(Option<$name::Item<'w>>,)*); type ReadOnly = AnyOf<($($name::ReadOnly,)*)>; type State = ($($name::State,)*); - fn shrink<'wlong: 'wshort, 'wshort>(item: QueryItem<'wlong, Self>) -> QueryItem<'wshort, Self> { + fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> { let ($($name,)*) = item; ($( $name.map($name::shrink), @@ -1329,14 +1277,14 @@ macro_rules! impl_anytuple_fetch { } #[allow(clippy::unused_unit)] - unsafe fn init_fetch<'w>(_world: &'w World, state: &Self::State, _last_change_tick: u32, _change_tick: u32) -> >::Fetch { + unsafe fn init_fetch<'w>(_world: &'w World, state: &Self::State, _last_change_tick: u32, _change_tick: u32) -> Self::Fetch<'w> { let ($($name,)*) = state; ($(($name::init_fetch(_world, $name, _last_change_tick, _change_tick), false),)*) } unsafe fn clone_fetch<'w>( - fetch: &>::Fetch, - ) -> >::Fetch { + fetch: &Self::Fetch<'w>, + ) -> Self::Fetch<'w> { let ($($name,)*) = &fetch; ($(($name::clone_fetch(& $name.0), $name.1),)*) } @@ -1347,7 +1295,7 @@ macro_rules! impl_anytuple_fetch { #[inline] unsafe fn set_archetype<'w>( - _fetch: &mut >::Fetch, + _fetch: &mut Self::Fetch<'w>, _state: &Self::State, _archetype: &'w Archetype, _table: &'w Table @@ -1363,7 +1311,7 @@ macro_rules! impl_anytuple_fetch { } #[inline] - unsafe fn set_table<'w>(_fetch: &mut >::Fetch, _state: &Self::State, _table: &'w Table) { + unsafe fn set_table<'w>(_fetch: &mut Self::Fetch<'w>, _state: &Self::State, _table: &'w Table) { let ($($name,)*) = _fetch; let ($($state,)*) = _state; $( @@ -1377,10 +1325,10 @@ macro_rules! impl_anytuple_fetch { #[inline(always)] #[allow(clippy::unused_unit)] unsafe fn fetch<'w>( - _fetch: &mut >::Fetch, + _fetch: &mut Self::Fetch<'w>, _entity: Entity, _table_row: usize - ) -> >::Item { + ) -> Self::Item<'w> { let ($($name,)*) = _fetch; ($( $name.1.then(|| $name::fetch(&mut $name.0, _entity, _table_row)), @@ -1455,6 +1403,8 @@ pub struct NopWorldQuery(PhantomData); /// SAFETY: `Self::ReadOnly` is `Self` unsafe impl WorldQuery for NopWorldQuery { + type Fetch<'w> = (); + type Item<'w> = (); type ReadOnly = Self; type State = Q::State; @@ -1473,10 +1423,7 @@ unsafe impl WorldQuery for NopWorldQuery { ) { } - unsafe fn clone_fetch<'w>( - _fetch: &>::Fetch, - ) -> >::Fetch { - } + unsafe fn clone_fetch<'w>(_fetch: &Self::Fetch<'w>) -> Self::Fetch<'w> {} #[inline(always)] unsafe fn set_archetype( @@ -1492,10 +1439,10 @@ unsafe impl WorldQuery for NopWorldQuery { #[inline(always)] unsafe fn fetch<'w>( - _fetch: &mut >::Fetch, + _fetch: &mut Self::Fetch<'w>, _entity: Entity, _table_row: usize, - ) -> >::Item { + ) -> Self::Item<'w> { } fn update_component_access(_state: &Q::State, _access: &mut FilteredAccess) {} @@ -1519,9 +1466,5 @@ unsafe impl WorldQuery for NopWorldQuery { } } -impl<'a, Q: WorldQuery> WorldQueryGats<'a> for NopWorldQuery { - type Fetch = (); - type Item = (); -} /// SAFETY: `NopFetch` never accesses any data unsafe impl ReadOnlyWorldQuery for NopWorldQuery {} diff --git a/crates/bevy_ecs/src/query/filter.rs b/crates/bevy_ecs/src/query/filter.rs index 1571907f567a6..984aac1178711 100644 --- a/crates/bevy_ecs/src/query/filter.rs +++ b/crates/bevy_ecs/src/query/filter.rs @@ -2,9 +2,7 @@ use crate::{ archetype::{Archetype, ArchetypeComponentId}, component::{Component, ComponentId, ComponentStorage, ComponentTicks, StorageType}, entity::Entity, - query::{ - debug_checked_unreachable, Access, FilteredAccess, QueryFetch, WorldQuery, WorldQueryGats, - }, + query::{debug_checked_unreachable, Access, FilteredAccess, WorldQuery}, storage::{ComponentSparseSet, Table}, world::World, }; @@ -43,20 +41,14 @@ use super::ReadOnlyWorldQuery; /// ``` pub struct With(PhantomData); -impl WorldQueryGats<'_> for With { - type Fetch = (); - type Item = (); -} - -// SAFETY: `ROQueryFetch` is the same as `QueryFetch` +// SAFETY: `Self::ReadOnly` is the same as `Self` unsafe impl WorldQuery for With { + type Fetch<'w> = (); + type Item<'w> = (); type ReadOnly = Self; type State = ComponentId; - fn shrink<'wlong: 'wshort, 'wshort>( - _: >::Item, - ) -> >::Item { - } + fn shrink<'wlong: 'wshort, 'wshort>(_: Self::Item<'wlong>) -> Self::Item<'wshort> {} unsafe fn init_fetch( _world: &World, @@ -66,10 +58,7 @@ unsafe impl WorldQuery for With { ) { } - unsafe fn clone_fetch<'w>( - _fetch: &>::Fetch, - ) -> >::Fetch { - } + unsafe fn clone_fetch<'w>(_fetch: &Self::Fetch<'w>) -> Self::Fetch<'w> {} const IS_DENSE: bool = { match T::Storage::STORAGE_TYPE { @@ -94,10 +83,10 @@ unsafe impl WorldQuery for With { #[inline(always)] unsafe fn fetch<'w>( - _fetch: &mut >::Fetch, + _fetch: &mut Self::Fetch<'w>, _entity: Entity, _table_row: usize, - ) -> >::Item { + ) -> Self::Item<'w> { } #[inline] @@ -154,15 +143,14 @@ unsafe impl ReadOnlyWorldQuery for With {} /// ``` pub struct Without(PhantomData); -// SAFETY: `ROQueryFetch` is the same as `QueryFetch` +// SAFETY: `Self::ReadOnly` is the same as `Self` unsafe impl WorldQuery for Without { + type Fetch<'w> = (); + type Item<'w> = (); type ReadOnly = Self; type State = ComponentId; - fn shrink<'wlong: 'wshort, 'wshort>( - _: >::Item, - ) -> >::Item { - } + fn shrink<'wlong: 'wshort, 'wshort>(_: Self::Item<'wlong>) -> Self::Item<'wshort> {} unsafe fn init_fetch( _world: &World, @@ -172,10 +160,7 @@ unsafe impl WorldQuery for Without { ) { } - unsafe fn clone_fetch<'w>( - _fetch: &>::Fetch, - ) -> >::Fetch { - } + unsafe fn clone_fetch<'w>(_fetch: &Self::Fetch<'w>) -> Self::Fetch<'w> {} const IS_DENSE: bool = { match T::Storage::STORAGE_TYPE { @@ -200,10 +185,10 @@ unsafe impl WorldQuery for Without { #[inline(always)] unsafe fn fetch<'w>( - _fetch: &mut >::Fetch, + _fetch: &mut Self::Fetch<'w>, _entity: Entity, _table_row: usize, - ) -> >::Item { + ) -> Self::Item<'w> { } #[inline] @@ -231,11 +216,6 @@ unsafe impl WorldQuery for Without { } } -impl WorldQueryGats<'_> for Without { - type Fetch = (); - type Item = (); -} - // SAFETY: no component access or archetype component access unsafe impl ReadOnlyWorldQuery for Without {} @@ -273,29 +253,23 @@ pub struct Or(pub T); #[doc(hidden)] pub struct OrFetch<'w, T: WorldQuery> { - fetch: QueryFetch<'w, T>, + fetch: T::Fetch<'w>, matches: bool, } macro_rules! impl_query_filter_tuple { ($(($filter: ident, $state: ident)),*) => { - #[allow(unused_variables)] - #[allow(non_snake_case)] - impl<'w, $($filter: WorldQuery),*> WorldQueryGats<'w> for Or<($($filter,)*)> { - type Fetch = ($(OrFetch<'w, $filter>,)*); - type Item = bool; - } - - #[allow(unused_variables)] #[allow(non_snake_case)] #[allow(clippy::unused_unit)] // SAFETY: defers to soundness of `$filter: WorldQuery` impl unsafe impl<$($filter: WorldQuery),*> WorldQuery for Or<($($filter,)*)> { + type Fetch<'w> = ($(OrFetch<'w, $filter>,)*); + type Item<'w> = bool; type ReadOnly = Or<($($filter::ReadOnly,)*)>; type State = ($($filter::State,)*); - fn shrink<'wlong: 'wshort, 'wshort>(item: super::QueryItem<'wlong, Self>) -> super::QueryItem<'wshort, Self> { + fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> { item } @@ -303,7 +277,7 @@ macro_rules! impl_query_filter_tuple { const IS_ARCHETYPAL: bool = true $(&& $filter::IS_ARCHETYPAL)*; - unsafe fn init_fetch<'w>(world: &'w World, state: &Self::State, last_change_tick: u32, change_tick: u32) -> >::Fetch { + unsafe fn init_fetch<'w>(world: &'w World, state: &Self::State, last_change_tick: u32, change_tick: u32) -> Self::Fetch<'w> { let ($($filter,)*) = state; ($(OrFetch { fetch: $filter::init_fetch(world, $filter, last_change_tick, change_tick), @@ -312,8 +286,8 @@ macro_rules! impl_query_filter_tuple { } unsafe fn clone_fetch<'w>( - fetch: &>::Fetch, - ) -> >::Fetch { + fetch: &Self::Fetch<'w>, + ) -> Self::Fetch<'w> { let ($($filter,)*) = &fetch; ($( OrFetch { @@ -324,7 +298,7 @@ macro_rules! impl_query_filter_tuple { } #[inline] - unsafe fn set_table<'w>(fetch: &mut >::Fetch, state: &Self::State, table: &'w Table) { + unsafe fn set_table<'w>(fetch: &mut Self::Fetch<'w>, state: &Self::State, table: &'w Table) { let ($($filter,)*) = fetch; let ($($state,)*) = state; $( @@ -337,7 +311,7 @@ macro_rules! impl_query_filter_tuple { #[inline] unsafe fn set_archetype<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, state: & Self::State, archetype: &'w Archetype, table: &'w Table @@ -354,17 +328,17 @@ macro_rules! impl_query_filter_tuple { #[inline(always)] unsafe fn fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, _entity: Entity, _table_row: usize - ) -> >::Item { + ) -> Self::Item<'w> { let ($($filter,)*) = fetch; false $(|| ($filter.matches && $filter::filter_fetch(&mut $filter.fetch, _entity, _table_row)))* } #[inline(always)] unsafe fn filter_fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, entity: Entity, table_row: usize ) -> bool { @@ -446,17 +420,19 @@ macro_rules! impl_tick_filter { change_tick: u32, } - // SAFETY: `ROQueryFetch` is the same as `QueryFetch` + // SAFETY: `Self::ReadOnly` is the same as `Self` unsafe impl WorldQuery for $name { + type Fetch<'w> = $fetch_name<'w, T>; + type Item<'w> = bool; type ReadOnly = Self; type State = ComponentId; - fn shrink<'wlong: 'wshort, 'wshort>(item: super::QueryItem<'wlong, Self>) -> super::QueryItem<'wshort, Self> { + fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> { item } - unsafe fn init_fetch<'w>(world: &'w World, &id: &ComponentId, last_change_tick: u32, change_tick: u32) -> >::Fetch { - QueryFetch::<'w, Self> { + unsafe fn init_fetch<'w>(world: &'w World, &id: &ComponentId, last_change_tick: u32, change_tick: u32) -> Self::Fetch<'w> { + Self::Fetch::<'w> { table_ticks: None, sparse_set: (T::Storage::STORAGE_TYPE == StorageType::SparseSet) .then(|| { @@ -472,8 +448,8 @@ macro_rules! impl_tick_filter { } unsafe fn clone_fetch<'w>( - fetch: &>::Fetch, - ) -> >::Fetch { + fetch: &Self::Fetch<'w>, + ) -> Self::Fetch<'w> { $fetch_name { table_ticks: fetch.table_ticks, sparse_set: fetch.sparse_set, @@ -494,7 +470,7 @@ macro_rules! impl_tick_filter { #[inline] unsafe fn set_table<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, &component_id: &ComponentId, table: &'w Table ) { @@ -508,7 +484,7 @@ macro_rules! impl_tick_filter { #[inline] unsafe fn set_archetype<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, component_id: &ComponentId, _archetype: &'w Archetype, table: &'w Table @@ -520,10 +496,10 @@ macro_rules! impl_tick_filter { #[inline(always)] unsafe fn fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, entity: Entity, table_row: usize - ) -> >::Item { + ) -> Self::Item<'w> { match T::Storage::STORAGE_TYPE { StorageType::Table => { $is_detected(&*( @@ -549,7 +525,7 @@ macro_rules! impl_tick_filter { #[inline(always)] unsafe fn filter_fetch<'w>( - fetch: &mut QueryFetch<'w, Self>, + fetch: &mut Self::Fetch<'w>, entity: Entity, table_row: usize ) -> bool { @@ -585,11 +561,6 @@ macro_rules! impl_tick_filter { } } - impl<'w, T: Component> WorldQueryGats<'w> for $name { - type Fetch = $fetch_name<'w, T>; - type Item = bool; - } - /// SAFETY: read-only access unsafe impl ReadOnlyWorldQuery for $name {} }; diff --git a/crates/bevy_ecs/src/query/iter.rs b/crates/bevy_ecs/src/query/iter.rs index 8a693eabb02c4..83c4bc270eadd 100644 --- a/crates/bevy_ecs/src/query/iter.rs +++ b/crates/bevy_ecs/src/query/iter.rs @@ -7,7 +7,7 @@ use crate::{ }; use std::{borrow::Borrow, iter::FusedIterator, marker::PhantomData, mem::MaybeUninit}; -use super::{QueryFetch, QueryItem, ReadOnlyWorldQuery}; +use super::ReadOnlyWorldQuery; /// An [`Iterator`] over query results of a [`Query`](crate::system::Query). /// @@ -42,7 +42,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> QueryIter<'w, 's, Q, F> { } impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Iterator for QueryIter<'w, 's, Q, F> { - type Item = QueryItem<'w, Q>; + type Item = Q::Item<'w>; #[inline(always)] fn next(&mut self) -> Option { @@ -86,8 +86,8 @@ where entities: &'w Entities, tables: &'w Tables, archetypes: &'w Archetypes, - fetch: QueryFetch<'w, Q>, - filter: QueryFetch<'w, F>, + fetch: Q::Fetch<'w>, + filter: F::Fetch<'w>, query_state: &'s QueryState, } @@ -137,7 +137,7 @@ where /// /// It is always safe for shared access. #[inline(always)] - unsafe fn fetch_next_aliased_unchecked(&mut self) -> Option> { + unsafe fn fetch_next_aliased_unchecked(&mut self) -> Option> { for entity in self.entity_iter.by_ref() { let entity = *entity.borrow(); let location = match self.entities.get(entity) { @@ -186,7 +186,7 @@ where /// Get next result from the query #[inline(always)] - pub fn fetch_next(&mut self) -> Option> { + pub fn fetch_next(&mut self) -> Option> { // SAFETY: we are limiting the returned reference to self, // making sure this method cannot be called multiple times without getting rid // of any previously returned unique references first, thus preventing aliasing. @@ -199,7 +199,7 @@ impl<'w, 's, Q: ReadOnlyWorldQuery, F: ReadOnlyWorldQuery, I: Iterator> Iterator where I::Item: Borrow, { - type Item = QueryItem<'w, Q>; + type Item = Q::Item<'w>; #[inline(always)] fn next(&mut self) -> Option { @@ -343,7 +343,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery, const K: usize> /// references to the same component, leading to unique reference aliasing. ///. /// It is always safe for shared access. - unsafe fn fetch_next_aliased_unchecked(&mut self) -> Option<[QueryItem<'w, Q>; K]> { + unsafe fn fetch_next_aliased_unchecked(&mut self) -> Option<[Q::Item<'w>; K]> { if K == 0 { return None; } @@ -368,9 +368,9 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery, const K: usize> } } - let mut values = MaybeUninit::<[QueryItem<'w, Q>; K]>::uninit(); + let mut values = MaybeUninit::<[Q::Item<'w>; K]>::uninit(); - let ptr = values.as_mut_ptr().cast::>(); + let ptr = values.as_mut_ptr().cast::>(); for (offset, cursor) in self.cursors.iter_mut().enumerate() { ptr.add(offset).write(cursor.peek_last().unwrap()); } @@ -380,7 +380,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery, const K: usize> /// Get next combination of queried components #[inline] - pub fn fetch_next(&mut self) -> Option<[QueryItem<'_, Q>; K]> { + pub fn fetch_next(&mut self) -> Option<[Q::Item<'_>; K]> { // SAFETY: we are limiting the returned reference to self, // making sure this method cannot be called multiple times without getting rid // of any previously returned unique references first, thus preventing aliasing. @@ -397,7 +397,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery, const K: usize> impl<'w, 's, Q: ReadOnlyWorldQuery, F: ReadOnlyWorldQuery, const K: usize> Iterator for QueryCombinationIter<'w, 's, Q, F, K> { - type Item = [QueryItem<'w, Q>; K]; + type Item = [Q::Item<'w>; K]; #[inline] fn next(&mut self) -> Option { @@ -468,8 +468,8 @@ struct QueryIterationCursor<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> { archetype_id_iter: std::slice::Iter<'s, ArchetypeId>, table_entities: &'w [Entity], archetype_entities: &'w [ArchetypeEntity], - fetch: QueryFetch<'w, Q>, - filter: QueryFetch<'w, F>, + fetch: Q::Fetch<'w>, + filter: F::Fetch<'w>, // length of the table table or length of the archetype, depending on whether both `Q`'s and `F`'s fetches are dense current_len: usize, // either table row or archetype index, depending on whether both `Q`'s and `F`'s fetches are dense @@ -549,7 +549,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> QueryIterationCursor<'w, 's, /// retrieve item returned from most recent `next` call again. #[inline] - unsafe fn peek_last(&mut self) -> Option> { + unsafe fn peek_last(&mut self) -> Option> { if self.current_index > 0 { let index = self.current_index - 1; if Self::IS_DENSE { @@ -580,7 +580,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> QueryIterationCursor<'w, 's, tables: &'w Tables, archetypes: &'w Archetypes, query_state: &'s QueryState, - ) -> Option> { + ) -> Option> { if Self::IS_DENSE { loop { // we are on the beginning of the query, or finished processing a table, so skip to the next diff --git a/crates/bevy_ecs/src/query/state.rs b/crates/bevy_ecs/src/query/state.rs index 0a26c16ebafc7..7e4e14acde5fa 100644 --- a/crates/bevy_ecs/src/query/state.rs +++ b/crates/bevy_ecs/src/query/state.rs @@ -13,7 +13,7 @@ use bevy_utils::tracing::Instrument; use fixedbitset::FixedBitSet; use std::{borrow::Borrow, fmt, mem::MaybeUninit}; -use super::{NopWorldQuery, QueryItem, QueryManyIter, ROQueryItem, ReadOnlyWorldQuery}; +use super::{NopWorldQuery, QueryManyIter, ROQueryItem, ReadOnlyWorldQuery}; /// Provides scoped access to a [`World`] state according to a given [`WorldQuery`] and query filter. #[repr(C)] @@ -272,7 +272,7 @@ impl QueryState { &mut self, world: &'w mut World, entity: Entity, - ) -> Result, QueryEntityError> { + ) -> Result, QueryEntityError> { self.update_archetypes(world); // SAFETY: query has unique world access unsafe { @@ -328,7 +328,7 @@ impl QueryState { &mut self, world: &'w mut World, entities: [Entity; N], - ) -> Result<[QueryItem<'w, Q>; N], QueryEntityError> { + ) -> Result<[Q::Item<'w>; N], QueryEntityError> { self.update_archetypes(world); // SAFETY: method requires exclusive world access @@ -372,7 +372,7 @@ impl QueryState { &mut self, world: &'w World, entity: Entity, - ) -> Result, QueryEntityError> { + ) -> Result, QueryEntityError> { self.update_archetypes(world); self.get_unchecked_manual( world, @@ -398,7 +398,7 @@ impl QueryState { entity: Entity, last_change_tick: u32, change_tick: u32, - ) -> Result, QueryEntityError> { + ) -> Result, QueryEntityError> { let location = world .entities .get(entity) @@ -472,7 +472,7 @@ impl QueryState { entities: [Entity; N], last_change_tick: u32, change_tick: u32, - ) -> Result<[QueryItem<'w, Q>; N], QueryEntityError> { + ) -> Result<[Q::Item<'w>; N], QueryEntityError> { // Verify that all entities are unique for i in 0..N { for j in 0..i { @@ -786,11 +786,7 @@ impl QueryState { /// Runs `func` on each query result for the given [`World`]. This is faster than the equivalent /// `iter_mut()` method, but cannot be chained like a normal [`Iterator`]. #[inline] - pub fn for_each_mut<'w, FN: FnMut(QueryItem<'w, Q>)>( - &mut self, - world: &'w mut World, - func: FN, - ) { + pub fn for_each_mut<'w, FN: FnMut(Q::Item<'w>)>(&mut self, world: &'w mut World, func: FN) { // SAFETY: query has unique world access unsafe { self.update_archetypes(world); @@ -813,7 +809,7 @@ impl QueryState { /// This does not check for mutable query correctness. To be safe, make sure mutable queries /// have unique access to the components they query. #[inline] - pub unsafe fn for_each_unchecked<'w, FN: FnMut(QueryItem<'w, Q>)>( + pub unsafe fn for_each_unchecked<'w, FN: FnMut(Q::Item<'w>)>( &mut self, world: &'w World, func: FN, @@ -861,7 +857,7 @@ impl QueryState { /// The [`ComputeTaskPool`] is not initialized. If using this from a query that is being /// initialized and run from the ECS scheduler, this should never panic. #[inline] - pub fn par_for_each_mut<'w, FN: Fn(QueryItem<'w, Q>) + Send + Sync + Clone>( + pub fn par_for_each_mut<'w, FN: Fn(Q::Item<'w>) + Send + Sync + Clone>( &mut self, world: &'w mut World, batch_size: usize, @@ -893,7 +889,7 @@ impl QueryState { /// This does not check for mutable query correctness. To be safe, make sure mutable queries /// have unique access to the components they query. #[inline] - pub unsafe fn par_for_each_unchecked<'w, FN: Fn(QueryItem<'w, Q>) + Send + Sync + Clone>( + pub unsafe fn par_for_each_unchecked<'w, FN: Fn(Q::Item<'w>) + Send + Sync + Clone>( &mut self, world: &'w World, batch_size: usize, @@ -919,7 +915,7 @@ impl QueryState { /// have unique access to the components they query. /// This does not validate that `world.id()` matches `self.world_id`. Calling this on a `world` /// with a mismatched [`WorldId`] is unsound. - pub(crate) unsafe fn for_each_unchecked_manual<'w, FN: FnMut(QueryItem<'w, Q>)>( + pub(crate) unsafe fn for_each_unchecked_manual<'w, FN: FnMut(Q::Item<'w>)>( &self, world: &'w World, mut func: FN, @@ -991,7 +987,7 @@ impl QueryState { /// with a mismatched [`WorldId`] is unsound. pub(crate) unsafe fn par_for_each_unchecked_manual< 'w, - FN: Fn(QueryItem<'w, Q>) + Send + Sync + Clone, + FN: Fn(Q::Item<'w>) + Send + Sync + Clone, >( &self, world: &'w World, @@ -1171,7 +1167,7 @@ impl QueryState { /// [`get_single_mut`](Self::get_single_mut) to return a `Result` instead of panicking. #[track_caller] #[inline] - pub fn single_mut<'w>(&mut self, world: &'w mut World) -> QueryItem<'w, Q> { + pub fn single_mut<'w>(&mut self, world: &'w mut World) -> Q::Item<'w> { // SAFETY: query has unique world access self.get_single_mut(world).unwrap() } @@ -1185,7 +1181,7 @@ impl QueryState { pub fn get_single_mut<'w>( &mut self, world: &'w mut World, - ) -> Result, QuerySingleError> { + ) -> Result, QuerySingleError> { self.update_archetypes(world); // SAFETY: query has unique world access @@ -1211,7 +1207,7 @@ impl QueryState { pub unsafe fn get_single_unchecked<'w>( &mut self, world: &'w World, - ) -> Result, QuerySingleError> { + ) -> Result, QuerySingleError> { self.update_archetypes(world); self.get_single_unchecked_manual(world, world.last_change_tick(), world.read_change_tick()) } @@ -1232,7 +1228,7 @@ impl QueryState { world: &'w World, last_change_tick: u32, change_tick: u32, - ) -> Result, QuerySingleError> { + ) -> Result, QuerySingleError> { let mut query = self.iter_unchecked_manual(world, last_change_tick, change_tick); let first = query.next(); let extra = query.next().is_some(); diff --git a/crates/bevy_ecs/src/system/query.rs b/crates/bevy_ecs/src/system/query.rs index 9b5f56bbff19c..8f7e4f70bc303 100644 --- a/crates/bevy_ecs/src/system/query.rs +++ b/crates/bevy_ecs/src/system/query.rs @@ -2,8 +2,8 @@ use crate::{ component::Component, entity::Entity, query::{ - QueryCombinationIter, QueryEntityError, QueryItem, QueryIter, QueryManyIter, - QuerySingleError, QueryState, ROQueryItem, ReadOnlyWorldQuery, WorldQuery, + QueryCombinationIter, QueryEntityError, QueryIter, QueryManyIter, QuerySingleError, + QueryState, ROQueryItem, ReadOnlyWorldQuery, WorldQuery, }, world::{Mut, World}, }; @@ -712,7 +712,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> { /// - [`for_each`](Self::for_each) to operate on read-only query items. /// - [`iter_mut`](Self::iter_mut) for the iterator based alternative. #[inline] - pub fn for_each_mut<'a>(&'a mut self, f: impl FnMut(QueryItem<'a, Q>)) { + pub fn for_each_mut<'a>(&'a mut self, f: impl FnMut(Q::Item<'a>)) { // SAFETY: system runs without conflicts with other systems. same-system queries have runtime // borrow checks when they conflict unsafe { @@ -786,7 +786,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> { pub fn par_for_each_mut<'a>( &'a mut self, batch_size: usize, - f: impl Fn(QueryItem<'a, Q>) + Send + Sync + Clone, + f: impl Fn(Q::Item<'a>) + Send + Sync + Clone, ) { // SAFETY: system runs without conflicts with other systems. same-system queries have runtime // borrow checks when they conflict @@ -945,7 +945,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> { /// /// - [`get`](Self::get) to get a read-only query item. #[inline] - pub fn get_mut(&mut self, entity: Entity) -> Result, QueryEntityError> { + pub fn get_mut(&mut self, entity: Entity) -> Result, QueryEntityError> { // SAFETY: system runs without conflicts with other systems. // same-system queries have runtime borrow checks when they conflict unsafe { @@ -970,7 +970,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> { pub fn get_many_mut( &mut self, entities: [Entity; N], - ) -> Result<[QueryItem<'_, Q>; N], QueryEntityError> { + ) -> Result<[Q::Item<'_>; N], QueryEntityError> { // SAFETY: scheduler ensures safe Query world access unsafe { self.state.get_many_unchecked_manual( @@ -1031,7 +1031,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> { /// - [`get_many_mut`](Self::get_many_mut) for the non panicking version. /// - [`many`](Self::many) to get read-only query items. #[inline] - pub fn many_mut(&mut self, entities: [Entity; N]) -> [QueryItem<'_, Q>; N] { + pub fn many_mut(&mut self, entities: [Entity; N]) -> [Q::Item<'_>; N] { self.get_many_mut(entities).unwrap() } @@ -1048,10 +1048,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> { /// /// - [`get_mut`](Self::get_mut) for the safe version. #[inline] - pub unsafe fn get_unchecked( - &self, - entity: Entity, - ) -> Result, QueryEntityError> { + pub unsafe fn get_unchecked(&self, entity: Entity) -> Result, QueryEntityError> { // SEMI-SAFETY: system runs without conflicts with other systems. // same-system queries have runtime borrow checks when they conflict self.state @@ -1302,7 +1299,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> { /// - [`get_single_mut`](Self::get_single_mut) for the non-panicking version. /// - [`single`](Self::single) to get the read-only query item. #[track_caller] - pub fn single_mut(&mut self) -> QueryItem<'_, Q> { + pub fn single_mut(&mut self) -> Q::Item<'_> { self.get_single_mut().unwrap() } @@ -1332,7 +1329,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> { /// - [`get_single`](Self::get_single) to get the read-only query item. /// - [`single_mut`](Self::single_mut) for the panicking version. #[inline] - pub fn get_single_mut(&mut self) -> Result, QuerySingleError> { + pub fn get_single_mut(&mut self) -> Result, QuerySingleError> { // SAFETY: // the query ensures mutable access to the components it accesses, and the query // is uniquely borrowed @@ -1415,7 +1412,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> IntoIterator for &'w Query<'_ } impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> IntoIterator for &'w mut Query<'_, 's, Q, F> { - type Item = QueryItem<'w, Q>; + type Item = Q::Item<'w>; type IntoIter = QueryIter<'w, 's, Q, F>; fn into_iter(self) -> Self::IntoIter { diff --git a/crates/bevy_hierarchy/src/query_extension.rs b/crates/bevy_hierarchy/src/query_extension.rs index 6282c81992514..6ee6dc26fc2fd 100644 --- a/crates/bevy_hierarchy/src/query_extension.rs +++ b/crates/bevy_hierarchy/src/query_extension.rs @@ -2,7 +2,7 @@ use std::collections::VecDeque; use bevy_ecs::{ entity::Entity, - query::{ReadOnlyWorldQuery, WorldQuery, WorldQueryGats}, + query::{ReadOnlyWorldQuery, WorldQuery}, system::Query, }; @@ -32,7 +32,7 @@ pub trait HierarchyQueryExt<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> { /// ``` fn iter_descendants(&'w self, entity: Entity) -> DescendantIter<'w, 's, Q, F> where - Q::ReadOnly: WorldQueryGats<'w, Item = &'w Children>; + Q::ReadOnly: WorldQuery = &'w Children>; /// Returns an [`Iterator`] of [`Entity`]s over all of `entity`s ancestors. /// @@ -54,7 +54,7 @@ pub trait HierarchyQueryExt<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> { /// ``` fn iter_ancestors(&'w self, entity: Entity) -> AncestorIter<'w, 's, Q, F> where - Q::ReadOnly: WorldQueryGats<'w, Item = &'w Parent>; + Q::ReadOnly: WorldQuery = &'w Parent>; } impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> HierarchyQueryExt<'w, 's, Q, F> @@ -62,14 +62,14 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> HierarchyQueryExt<'w, 's, Q, { fn iter_descendants(&'w self, entity: Entity) -> DescendantIter<'w, 's, Q, F> where - Q::ReadOnly: WorldQueryGats<'w, Item = &'w Children>, + Q::ReadOnly: WorldQuery = &'w Children>, { DescendantIter::new(self, entity) } fn iter_ancestors(&'w self, entity: Entity) -> AncestorIter<'w, 's, Q, F> where - Q::ReadOnly: WorldQueryGats<'w, Item = &'w Parent>, + Q::ReadOnly: WorldQuery = &'w Parent>, { AncestorIter::new(self, entity) } @@ -80,7 +80,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> HierarchyQueryExt<'w, 's, Q, /// Traverses the hierarchy breadth-first. pub struct DescendantIter<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> where - Q::ReadOnly: WorldQueryGats<'w, Item = &'w Children>, + Q::ReadOnly: WorldQuery = &'w Children>, { children_query: &'w Query<'w, 's, Q, F>, vecdeque: VecDeque, @@ -88,7 +88,7 @@ where impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> DescendantIter<'w, 's, Q, F> where - Q::ReadOnly: WorldQueryGats<'w, Item = &'w Children>, + Q::ReadOnly: WorldQuery = &'w Children>, { /// Returns a new [`DescendantIter`]. pub fn new(children_query: &'w Query<'w, 's, Q, F>, entity: Entity) -> Self { @@ -106,7 +106,7 @@ where impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Iterator for DescendantIter<'w, 's, Q, F> where - Q::ReadOnly: WorldQueryGats<'w, Item = &'w Children>, + Q::ReadOnly: WorldQuery = &'w Children>, { type Item = Entity; @@ -124,7 +124,7 @@ where /// An [`Iterator`] of [`Entity`]s over the ancestors of an [`Entity`]. pub struct AncestorIter<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> where - Q::ReadOnly: WorldQueryGats<'w, Item = &'w Parent>, + Q::ReadOnly: WorldQuery = &'w Parent>, { parent_query: &'w Query<'w, 's, Q, F>, next: Option, @@ -132,7 +132,7 @@ where impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> AncestorIter<'w, 's, Q, F> where - Q::ReadOnly: WorldQueryGats<'w, Item = &'w Parent>, + Q::ReadOnly: WorldQuery = &'w Parent>, { /// Returns a new [`AncestorIter`]. pub fn new(parent_query: &'w Query<'w, 's, Q, F>, entity: Entity) -> Self { @@ -145,7 +145,7 @@ where impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Iterator for AncestorIter<'w, 's, Q, F> where - Q::ReadOnly: WorldQueryGats<'w, Item = &'w Parent>, + Q::ReadOnly: WorldQuery = &'w Parent>, { type Item = Entity; diff --git a/crates/bevy_render/src/extract_component.rs b/crates/bevy_render/src/extract_component.rs index da90b0bcb423f..573c3e0cff23f 100644 --- a/crates/bevy_render/src/extract_component.rs +++ b/crates/bevy_render/src/extract_component.rs @@ -38,7 +38,7 @@ pub trait ExtractComponent: Component { /// Filters the entities with additional constraints. type Filter: WorldQuery + ReadOnlyWorldQuery; /// Defines how the component is transferred into the "render world". - fn extract_component(item: QueryItem) -> Self; + fn extract_component(item: QueryItem<'_, Self::Query>) -> Self; } /// This plugin prepares the components of the corresponding type for the GPU @@ -174,7 +174,7 @@ impl ExtractComponent for Handle { type Filter = (); #[inline] - fn extract_component(handle: QueryItem) -> Self { + fn extract_component(handle: QueryItem<'_, Self::Query>) -> Self { handle.clone_weak() } } diff --git a/crates/bevy_ui/src/entity.rs b/crates/bevy_ui/src/entity.rs index 1e3491ad14d27..05ede7d7e0700 100644 --- a/crates/bevy_ui/src/entity.rs +++ b/crates/bevy_ui/src/entity.rs @@ -266,7 +266,7 @@ impl ExtractComponent for UiCameraConfig { type Query = &'static Self; type Filter = With; - fn extract_component(item: QueryItem) -> Self { + fn extract_component(item: QueryItem<'_, Self::Query>) -> Self { item.clone() } } diff --git a/examples/shader/shader_instancing.rs b/examples/shader/shader_instancing.rs index 71e68566b40e5..d7cc9c46755aa 100644 --- a/examples/shader/shader_instancing.rs +++ b/examples/shader/shader_instancing.rs @@ -2,7 +2,10 @@ use bevy::{ core_pipeline::core_3d::Transparent3d, - ecs::system::{lifetimeless::*, SystemParamItem}, + ecs::{ + query::QueryItem, + system::{lifetimeless::*, SystemParamItem}, + }, pbr::{MeshPipeline, MeshPipelineKey, MeshUniform, SetMeshBindGroup, SetMeshViewBindGroup}, prelude::*, render::{ @@ -67,7 +70,7 @@ impl ExtractComponent for InstanceMaterialData { type Query = &'static InstanceMaterialData; type Filter = (); - fn extract_component(item: bevy::ecs::query::QueryItem) -> Self { + fn extract_component(item: QueryItem<'_, Self::Query>) -> Self { InstanceMaterialData(item.0.clone()) } }