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 b94b59a0b65254..3a1d20e929314e 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 ce91c2d2e04a8a..265f0d2649270b 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 6d0e3d509428ca..a25e7855d3ef9d 100644 --- a/crates/bevy_ecs/macros/src/fetch.rs +++ b/crates/bevy_ecs/macros/src/fetch.rs @@ -182,34 +182,29 @@ pub fn derive_world_query_impl(ast: DeriveInput) -> TokenStream { #derive_macro_call #[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,)* } #[derive(Clone)] #[doc(hidden)] #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), @@ -225,7 +220,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,7 +241,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, _tables: &'__w #path::storage::Tables @@ -257,7 +252,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 ) { @@ -267,9 +262,9 @@ pub fn derive_world_query_impl(ast: DeriveInput) -> TokenStream { /// SAFETY: we call `table_fetch` for each member that implements `Fetch`. #[inline] unsafe fn table_fetch<'__w>( - _fetch: &mut >::Fetch, + _fetch: &mut ::Fetch<'__w>, _table_row: usize - ) -> >::Item { + ) -> ::Item<'__w> { Self::Item { #(#field_idents: <#field_types>::table_fetch(&mut _fetch.#field_idents, _table_row),)* #(#ignored_field_idents: Default::default(),)* @@ -279,9 +274,9 @@ pub fn derive_world_query_impl(ast: DeriveInput) -> TokenStream { /// SAFETY: we call `archetype_fetch` for each member that implements `Fetch`. #[inline] unsafe fn archetype_fetch<'__w>( - _fetch: &mut >::Fetch, + _fetch: &mut ::Fetch<'__w>, _archetype_index: usize - ) -> >::Item { + ) -> ::Item<'__w> { Self::Item { #(#field_idents: <#field_types>::archetype_fetch(&mut _fetch.#field_idents, _archetype_index),)* #(#ignored_field_idents: Default::default(),)* @@ -290,13 +285,13 @@ pub fn derive_world_query_impl(ast: DeriveInput) -> TokenStream { #[allow(unused_variables)] #[inline] - unsafe fn table_filter_fetch<'__w>(_fetch: &mut >::Fetch, _table_row: usize) -> bool { + unsafe fn table_filter_fetch<'__w>(_fetch: &mut ::Fetch<'__w>, _table_row: usize) -> bool { true #(&& <#field_types>::table_filter_fetch(&mut _fetch.#field_idents, _table_row))* } #[allow(unused_variables)] #[inline] - unsafe fn archetype_filter_fetch<'__w>(_fetch: &mut >::Fetch, _archetype_index: usize) -> bool { + unsafe fn archetype_filter_fetch<'__w>(_fetch: &mut ::Fetch<'__w>, _archetype_index: usize) -> bool { true #(&& <#field_types>::archetype_filter_fetch(&mut _fetch.#field_idents, _archetype_index))* } diff --git a/crates/bevy_ecs/src/query/fetch.rs b/crates/bevy_ecs/src/query/fetch.rs index a4670944807f87..bf74ba6c5ec910 100644 --- a/crates/bevy_ecs/src/query/fetch.rs +++ b/crates/bevy_ecs/src/query/fetch.rs @@ -277,8 +277,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`] @@ -291,7 +291,7 @@ use std::{cell::UnsafeCell, marker::PhantomData}; /// [`Added`]: crate::query::Added /// [`archetype_fetch`]: Self::archetype_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 @@ -302,17 +302,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. /// @@ -325,7 +331,7 @@ pub unsafe trait WorldQuery: for<'w> WorldQueryGats<'w> { state: &Self::State, last_change_tick: u32, change_tick: u32, - ) -> >::Fetch; + ) -> 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" @@ -349,7 +355,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, tables: &'w Tables, @@ -362,13 +368,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 the given `archetype_index` in the current [`Archetype`]. This must + /// Fetch [`Self::Item`](`WorldQuery::Item`) for the given `archetype_index` in the current [`Archetype`]. This must /// always be called after [`WorldQuery::set_archetype`] with an `archetype_index` in the range of /// the current [`Archetype`] /// @@ -376,21 +378,19 @@ pub unsafe trait WorldQuery: for<'w> WorldQueryGats<'w> { /// Must always be called _after_ [`WorldQuery::set_archetype`]. `archetype_index` must be in the range /// of the current archetype unsafe fn archetype_fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, archetype_index: usize, - ) -> >::Item; + ) -> QueryItem<'w, Self>; - /// Fetch [`Self::Item`](`WorldQueryGats::Item`) for the given `table_row` in the current [`Table`]. This must always be + /// Fetch [`Self::Item`](`WorldQuery::Item`) for the given `table_row` in the current [`Table`]. This must always be /// called after [`WorldQuery::set_table`] with a `table_row` in the range of the current [`Table`] /// /// # Safety /// /// Must always be called _after_ [`WorldQuery::set_table`]. `table_row` must be in the range of the /// current table - unsafe fn table_fetch<'w>( - fetch: &mut >::Fetch, - table_row: usize, - ) -> >::Item; + unsafe fn table_fetch<'w>(fetch: &mut Self::Fetch<'w>, table_row: usize) + -> QueryItem<'w, Self>; /// # Safety /// @@ -398,10 +398,7 @@ pub unsafe trait WorldQuery: for<'w> WorldQueryGats<'w> { /// of the current archetype. #[allow(unused_variables)] #[inline] - unsafe fn archetype_filter_fetch( - fetch: &mut >::Fetch, - archetype_index: usize, - ) -> bool { + unsafe fn archetype_filter_fetch(fetch: &mut Self::Fetch<'_>, archetype_index: usize) -> bool { true } @@ -411,10 +408,7 @@ pub unsafe trait WorldQuery: for<'w> WorldQueryGats<'w> { /// current table. #[allow(unused_variables)] #[inline] - unsafe fn table_filter_fetch( - fetch: &mut >::Fetch, - table_row: usize, - ) -> bool { + unsafe fn table_filter_fetch(fetch: &mut Self::Fetch<'_>, table_row: usize) -> bool { true } @@ -436,14 +430,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 @@ -451,14 +437,10 @@ pub trait WorldQueryGats<'world> { /// This must only be implemented for read-only [`WorldQuery`]'s. 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; /// The item type returned when a [`WorldQuery`] is iterated over -pub type QueryItem<'w, Q> = >::Item; -/// 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>; +pub type QueryItem<'w, Q> = ::Item<'w>; /// The read-only variant of the item type returned when a [`WorldQuery`] is iterated over immutably -pub type ROQueryItem<'w, Q> = QueryItem<'w, ::ReadOnly>; +pub type ROQueryItem<'w, Q> = <::ReadOnly as WorldQuery>::Item<'w>; #[doc(hidden)] #[derive(Clone)] @@ -468,10 +450,12 @@ pub struct EntityFetch<'w> { /// SAFETY: no component or archetype access unsafe impl WorldQuery for Entity { + type Fetch<'w> = EntityFetch<'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 } @@ -505,7 +489,7 @@ unsafe impl WorldQuery for Entity { #[inline] unsafe fn table_fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, table_row: usize, ) -> QueryItem<'w, Self> { let entities = fetch @@ -516,9 +500,9 @@ unsafe impl WorldQuery for Entity { #[inline] unsafe fn archetype_fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, archetype_index: usize, - ) -> >::Item { + ) -> QueryItem<'w, Self> { let entities = fetch .entities .unwrap_or_else(|| debug_checked_unreachable()); @@ -544,11 +528,6 @@ unsafe impl WorldQuery for Entity { } } -impl<'w> WorldQueryGats<'w> for Entity { - type Fetch = EntityFetch<'w>; - type Item = Entity; -} - /// SAFETY: access is read only unsafe impl ReadOnlyWorldQuery for Entity {} @@ -562,8 +541,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; @@ -621,9 +602,9 @@ unsafe impl WorldQuery for &T { #[inline] unsafe fn archetype_fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, archetype_index: usize, - ) -> >::Item { + ) -> QueryItem<'w, Self> { match T::Storage::STORAGE_TYPE { StorageType::Table => { let (entity_table_rows, table_components) = fetch @@ -649,9 +630,9 @@ unsafe impl WorldQuery for &T { #[inline] unsafe fn table_fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, table_row: usize, - ) -> >::Item { + ) -> QueryItem<'w, Self> { let components = fetch .table_components .unwrap_or_else(|| debug_checked_unreachable()); @@ -706,11 +687,6 @@ impl Clone for ReadFetch<'_, 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 @@ -727,6 +703,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; @@ -794,9 +772,9 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T { #[inline] unsafe fn archetype_fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, archetype_index: usize, - ) -> >::Item { + ) -> QueryItem<'w, Self> { match T::Storage::STORAGE_TYPE { StorageType::Table => { let (entity_table_rows, (table_components, table_ticks)) = fetch @@ -836,9 +814,9 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T { #[inline] unsafe fn table_fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, table_row: usize, - ) -> >::Item { + ) -> QueryItem<'w, Self> { let (table_components, table_ticks) = fetch .table_components .zip(fetch.table_ticks) @@ -901,19 +879,14 @@ impl Clone for WriteFetch<'_, 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, } impl<'w, T: WorldQuery> Clone for OptionFetch<'w, T> where - >::Fetch: Clone, + T::Fetch<'w>: Clone, { fn clone(&self) -> Self { Self { @@ -925,10 +898,12 @@ where // 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) } @@ -971,9 +946,9 @@ unsafe impl WorldQuery for Option { #[inline] unsafe fn archetype_fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, archetype_index: usize, - ) -> >::Item { + ) -> QueryItem<'w, Self> { if fetch.matches { Some(T::archetype_fetch(&mut fetch.fetch, archetype_index)) } else { @@ -983,9 +958,9 @@ unsafe impl WorldQuery for Option { #[inline] unsafe fn table_fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, table_row: usize, - ) -> >::Item { + ) -> QueryItem<'w, Self> { if fetch.matches { Some(T::table_fetch(&mut fetch.fetch, table_row)) } else { @@ -1028,11 +1003,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 @@ -1125,12 +1095,14 @@ impl Clone for ChangeTrackersFetch<'_, T> { } } -// SAFETY: `ROQueryFetch` is the same as `QueryFetch` +// SAFETY: `Self::ReadOnly` is the same as `Self` 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 } @@ -1189,9 +1161,9 @@ unsafe impl WorldQuery for ChangeTrackers { #[inline] unsafe fn archetype_fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, archetype_index: usize, - ) -> >::Item { + ) -> QueryItem<'w, Self> { match T::Storage::STORAGE_TYPE { StorageType::Table => { let entity_table_rows = fetch @@ -1233,9 +1205,9 @@ unsafe impl WorldQuery for ChangeTrackers { #[inline] unsafe fn table_fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, table_row: usize, - ) -> >::Item { + ) -> QueryItem<'w, Self> { ChangeTrackers { component_ticks: { let table_ticks = fetch @@ -1283,28 +1255,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), @@ -1312,7 +1274,7 @@ 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),)*) } @@ -1322,14 +1284,14 @@ macro_rules! impl_tuple_fetch { const IS_ARCHETYPAL: bool = true $(&& $name::IS_ARCHETYPAL)*; #[inline] - unsafe fn set_archetype<'w>(_fetch: &mut >::Fetch, _state: &Self::State, _archetype: &'w Archetype, _tables: &'w Tables) { + unsafe fn set_archetype<'w>(_fetch: &mut Self::Fetch<'w>, _state: &Self::State, _archetype: &'w Archetype, _tables: &'w Tables) { let ($($name,)*) = _fetch; let ($($state,)*) = _state; $($name::set_archetype($name, $state, _archetype, _tables);)* } #[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);)* @@ -1337,28 +1299,28 @@ macro_rules! impl_tuple_fetch { #[inline] #[allow(clippy::unused_unit)] - unsafe fn table_fetch<'w>(_fetch: &mut >::Fetch, _table_row: usize) -> QueryItem<'w, Self> { + unsafe fn table_fetch<'w>(_fetch: &mut Self::Fetch<'w>, _table_row: usize) -> QueryItem<'w, Self> { let ($($name,)*) = _fetch; ($($name::table_fetch($name, _table_row),)*) } #[inline] #[allow(clippy::unused_unit)] - unsafe fn archetype_fetch<'w>(_fetch: &mut >::Fetch, _archetype_index: usize) -> QueryItem<'w, Self> { + unsafe fn archetype_fetch<'w>(_fetch: &mut Self::Fetch<'w>, _archetype_index: usize) -> QueryItem<'w, Self> { let ($($name,)*) = _fetch; ($($name::archetype_fetch($name, _archetype_index),)*) } #[allow(unused_variables)] #[inline] - unsafe fn table_filter_fetch(_fetch: &mut QueryFetch<'_, Self>, table_row: usize) -> bool { + unsafe fn table_filter_fetch(_fetch: &mut Self::Fetch<'_>, table_row: usize) -> bool { let ($($name,)*) = _fetch; true $(&& $name::table_filter_fetch($name, table_row))* } #[allow(unused_variables)] #[inline] - unsafe fn archetype_filter_fetch(_fetch: &mut QueryFetch<'_, Self>, archetype_index: usize) -> bool { + unsafe fn archetype_filter_fetch(_fetch: &mut Self::Fetch<'_>, archetype_index: usize) -> bool { let ($($name,)*) = _fetch; true $(&& $name::archetype_filter_fetch($name, archetype_index))* } @@ -1400,21 +1362,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), @@ -1422,7 +1379,7 @@ 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),)*) } @@ -1432,7 +1389,7 @@ macro_rules! impl_anytuple_fetch { const IS_ARCHETYPAL: bool = true $(&& $name::IS_ARCHETYPAL)*; #[inline] - unsafe fn set_archetype<'w>(_fetch: &mut >::Fetch, _state: &Self::State, _archetype: &'w Archetype, _tables: &'w Tables) { + unsafe fn set_archetype<'w>(_fetch: &mut Self::Fetch<'w>, _state: &Self::State, _archetype: &'w Archetype, _tables: &'w Tables) { let ($($name,)*) = _fetch; let ($($state,)*) = _state; $( @@ -1444,7 +1401,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; $( @@ -1457,7 +1414,7 @@ macro_rules! impl_anytuple_fetch { #[inline] #[allow(clippy::unused_unit)] - unsafe fn table_fetch<'w>(_fetch: &mut >::Fetch, _table_row: usize) -> QueryItem<'w, Self> { + unsafe fn table_fetch<'w>(_fetch: &mut Self::Fetch<'w>, _table_row: usize) -> QueryItem<'w, Self> { let ($($name,)*) = _fetch; ($( $name.1.then(|| $name::table_fetch(&mut $name.0, _table_row)), @@ -1466,7 +1423,7 @@ macro_rules! impl_anytuple_fetch { #[inline] #[allow(clippy::unused_unit)] - unsafe fn archetype_fetch<'w>(_fetch: &mut >::Fetch, _archetype_index: usize) -> QueryItem<'w, Self> { + unsafe fn archetype_fetch<'w>(_fetch: &mut Self::Fetch<'w>, _archetype_index: usize) -> QueryItem<'w, Self> { let ($($name,)*) = _fetch; ($( $name.1.then(|| $name::archetype_fetch(&mut $name.0, _archetype_index)), @@ -1541,6 +1498,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; @@ -1573,17 +1532,13 @@ unsafe impl WorldQuery for NopWorldQuery { #[inline(always)] unsafe fn archetype_fetch<'w>( - _fetch: &mut >::Fetch, + _fetch: &mut Self::Fetch<'w>, _archetype_index: usize, - ) -> >::Item { + ) -> QueryItem<'w, Self> { } #[inline(always)] - unsafe fn table_fetch<'w>( - _fetch: &mut (), - _table_row: usize, - ) -> >::Item { - } + unsafe fn table_fetch<'w>(_fetch: &mut (), _table_row: usize) -> QueryItem<'w, Self> {} fn update_component_access(_state: &Q::State, _access: &mut FilteredAccess) {} @@ -1606,9 +1561,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 960c6d4bcd7f53..be2978cb97ca20 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, Tables}, world::World, }; @@ -12,7 +10,7 @@ use bevy_ecs_macros::all_tuples; use bevy_ptr::{ThinSlicePtr, UnsafeCellDeref}; use std::{cell::UnsafeCell, marker::PhantomData}; -use super::ReadOnlyWorldQuery; +use super::{QueryItem, ReadOnlyWorldQuery}; /// Filter that selects entities with a component `T`. /// @@ -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, @@ -89,16 +81,16 @@ unsafe impl WorldQuery for With { #[inline] unsafe fn archetype_fetch<'w>( - _fetch: &mut >::Fetch, + _fetch: &mut Self::Fetch<'w>, _archetype_index: usize, - ) -> >::Item { + ) -> QueryItem<'w, Self> { } #[inline] unsafe fn table_fetch<'w>( - _fetch: &mut >::Fetch, + _fetch: &mut Self::Fetch<'w>, _table_row: usize, - ) -> >::Item { + ) -> QueryItem<'w, Self> { } #[inline] @@ -155,15 +147,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, @@ -196,16 +187,16 @@ unsafe impl WorldQuery for Without { #[inline] unsafe fn archetype_fetch<'w>( - _fetch: &mut >::Fetch, + _fetch: &mut Self::Fetch<'w>, _archetype_index: usize, - ) -> >::Item { + ) -> QueryItem<'w, Self> { } #[inline] unsafe fn table_fetch<'w>( - _fetch: &mut >::Fetch, + _fetch: &mut Self::Fetch<'w>, _table_row: usize, - ) -> >::Item { + ) -> QueryItem<'w, Self> { } #[inline] @@ -233,11 +224,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 {} @@ -276,13 +262,13 @@ pub struct Or(pub T); #[doc(hidden)] pub struct OrFetch<'w, T: WorldQuery> { - fetch: QueryFetch<'w, T>, + fetch: T::Fetch<'w>, matches: bool, } -impl<'w, T: WorldQuery> Copy for OrFetch<'w, T> where QueryFetch<'w, T>: Copy {} +impl<'w, T: WorldQuery> Copy for OrFetch<'w, T> where T::Fetch<'w>: Copy {} impl<'w, T: WorldQuery> Clone for OrFetch<'w, T> where - QueryFetch<'w, T>: Clone, + T::Fetch<'w>: Clone, { fn clone(&self) -> Self { Self { @@ -294,23 +280,17 @@ where 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 } @@ -318,7 +298,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), @@ -327,7 +307,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; $( @@ -339,7 +319,7 @@ macro_rules! impl_query_filter_tuple { } #[inline] - unsafe fn set_archetype<'w>(fetch: &mut >::Fetch, state: &Self::State, archetype: &'w Archetype, tables: &'w Tables) { + unsafe fn set_archetype<'w>(fetch: &mut Self::Fetch<'w>, state: &Self::State, archetype: &'w Archetype, tables: &'w Tables) { let ($($filter,)*) = fetch; let ($($state,)*) = state; $( @@ -351,24 +331,24 @@ macro_rules! impl_query_filter_tuple { } #[inline] - unsafe fn table_fetch<'w>(fetch: &mut >::Fetch, table_row: usize) -> >::Item { + unsafe fn table_fetch<'w>(fetch: &mut Self::Fetch<'w>, table_row: usize) -> QueryItem<'w, Self> { let ($($filter,)*) = fetch; false $(|| ($filter.matches && $filter::table_filter_fetch(&mut $filter.fetch, table_row)))* } #[inline] - unsafe fn archetype_fetch<'w>(fetch: &mut >::Fetch, archetype_index: usize) -> >::Item { + unsafe fn archetype_fetch<'w>(fetch: &mut Self::Fetch<'w>, archetype_index: usize) -> QueryItem<'w, Self> { let ($($filter,)*) = fetch; false $(|| ($filter.matches && $filter::archetype_filter_fetch(&mut $filter.fetch, archetype_index)))* } #[inline] - unsafe fn table_filter_fetch(fetch: &mut QueryFetch<'_, Self>, table_row: usize) -> bool { + unsafe fn table_filter_fetch(fetch: &mut Self::Fetch<'_>, table_row: usize) -> bool { Self::table_fetch(fetch, table_row) } #[inline] - unsafe fn archetype_filter_fetch(fetch: &mut QueryFetch<'_, Self>, archetype_index: usize) -> bool { + unsafe fn archetype_filter_fetch(fetch: &mut Self::Fetch<'_>, archetype_index: usize) -> bool { Self::archetype_fetch(fetch, archetype_index) } @@ -449,17 +429,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, entities: None, entity_table_rows: None, @@ -480,11 +462,11 @@ macro_rules! impl_tick_filter { const IS_ARCHETYPAL: bool = false; - unsafe fn set_table<'w>(fetch: &mut >::Fetch, &id: &ComponentId, table: &'w Table) { + unsafe fn set_table<'w>(fetch: &mut Self::Fetch<'w>, &id: &ComponentId, table: &'w Table) { fetch.table_ticks = Some(table.get_column(id).unwrap().get_ticks_slice().into()); } - unsafe fn set_archetype<'w>(fetch: &mut >::Fetch, &id: &ComponentId, archetype: &'w Archetype, tables: &'w Tables) { + unsafe fn set_archetype<'w>(fetch: &mut Self::Fetch<'w>, &id: &ComponentId, archetype: &'w Archetype, tables: &'w Tables) { match T::Storage::STORAGE_TYPE { StorageType::Table => { fetch.entity_table_rows = Some(archetype.entity_table_rows().into()); @@ -495,11 +477,11 @@ macro_rules! impl_tick_filter { } } - unsafe fn table_fetch<'w>(fetch: &mut >::Fetch, table_row: usize) -> >::Item { + unsafe fn table_fetch<'w>(fetch: &mut Self::Fetch<'w>, table_row: usize) -> QueryItem<'w, Self> { $is_detected(&*(fetch.table_ticks.unwrap_or_else(|| debug_checked_unreachable()).get(table_row)).deref(), fetch.last_change_tick, fetch.change_tick) } - unsafe fn archetype_fetch<'w>(fetch: &mut >::Fetch, archetype_index: usize) -> >::Item { + unsafe fn archetype_fetch<'w>(fetch: &mut Self::Fetch<'w>, archetype_index: usize) -> QueryItem<'w, Self> { match T::Storage::STORAGE_TYPE { StorageType::Table => { let table_row = *fetch.entity_table_rows.unwrap_or_else(|| debug_checked_unreachable()).get(archetype_index); @@ -520,12 +502,12 @@ macro_rules! impl_tick_filter { } #[inline] - unsafe fn table_filter_fetch(fetch: &mut QueryFetch<'_, Self>, table_row: usize) -> bool { + unsafe fn table_filter_fetch(fetch: &mut Self::Fetch<'_>, table_row: usize) -> bool { Self::table_fetch(fetch, table_row) } #[inline] - unsafe fn archetype_filter_fetch(fetch: &mut QueryFetch<'_, Self>, archetype_index: usize) -> bool { + unsafe fn archetype_filter_fetch(fetch: &mut Self::Fetch<'_>, archetype_index: usize) -> bool { Self::archetype_fetch(fetch, archetype_index) } @@ -558,11 +540,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 374c0f7d6f1ba0..05b38ef1112c17 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::{QueryItem, ReadOnlyWorldQuery}; /// An [`Iterator`] over query results of a [`Query`](crate::system::Query). /// @@ -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, } @@ -182,7 +182,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. @@ -331,8 +331,8 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery, const K: usize> /// It is always safe for shared access. unsafe fn fetch_next_aliased_unchecked(&mut self) -> Option<[QueryItem<'w, Q>; K]> where - QueryFetch<'w, Q>: Clone, - QueryFetch<'w, F>: Clone, + Q::Fetch<'w>: Clone, + F::Fetch<'w>: Clone, { if K == 0 { return None; @@ -370,10 +370,10 @@ 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]> where - for<'a> QueryFetch<'a, Q>: Clone, - for<'a> QueryFetch<'a, F>: Clone, + for<'a> Q::Fetch<'a>: Clone, + for<'a> F::Fetch<'a>: Clone, { // SAFETY: we are limiting the returned reference to self, // making sure this method cannot be called multiple times without getting rid @@ -391,8 +391,8 @@ 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> where - QueryFetch<'w, Q>: Clone, - QueryFetch<'w, F>: Clone, + Q::Fetch<'w>: Clone, + F::Fetch<'w>: Clone, { type Item = [QueryItem<'w, Q>; K]; @@ -458,16 +458,16 @@ where impl<'w, 's, Q: ReadOnlyWorldQuery, F: ReadOnlyWorldQuery, const K: usize> FusedIterator for QueryCombinationIter<'w, 's, Q, F, K> where - QueryFetch<'w, Q>: Clone, - QueryFetch<'w, F>: Clone, + Q::Fetch<'w>: Clone, + F::Fetch<'w>: Clone, { } struct QueryIterationCursor<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> { table_id_iter: std::slice::Iter<'s, TableId>, archetype_id_iter: std::slice::Iter<'s, ArchetypeId>, - 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 @@ -477,8 +477,8 @@ struct QueryIterationCursor<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> { impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Clone for QueryIterationCursor<'w, 's, Q, F> where - QueryFetch<'w, Q>: Clone, - QueryFetch<'w, F>: Clone, + Q::Fetch<'w>: Clone, + F::Fetch<'w>: Clone, { fn clone(&self) -> Self { Self { diff --git a/crates/bevy_ecs/src/query/mod.rs b/crates/bevy_ecs/src/query/mod.rs index 4adfbd2e369ee8..9619f858204c41 100644 --- a/crates/bevy_ecs/src/query/mod.rs +++ b/crates/bevy_ecs/src/query/mod.rs @@ -21,7 +21,7 @@ pub(crate) unsafe fn debug_checked_unreachable() -> ! { mod tests { use super::{ReadOnlyWorldQuery, WorldQuery}; use crate::prelude::{AnyOf, Entity, Or, QueryState, With, Without}; - use crate::query::{ArchetypeFilter, QueryCombinationIter, QueryFetch}; + use crate::query::{ArchetypeFilter, QueryCombinationIter}; use crate::system::{IntoSystem, Query, System, SystemState}; use crate::{self as bevy_ecs, component::Component, world::World}; use std::any::type_name; @@ -88,8 +88,8 @@ mod tests { Q: ReadOnlyWorldQuery, F: ReadOnlyWorldQuery, F::ReadOnly: ArchetypeFilter, - for<'w> QueryFetch<'w, Q::ReadOnly>: Clone, - for<'w> QueryFetch<'w, F::ReadOnly>: Clone, + for<'w> ::Fetch<'w>: Clone, + for<'w> ::Fetch<'w>: Clone, { let mut query = world.query_filtered::(); let iter = query.iter(world); @@ -166,8 +166,8 @@ mod tests { Q: WorldQuery, F: ReadOnlyWorldQuery, F::ReadOnly: ArchetypeFilter, - for<'w> QueryFetch<'w, Q::ReadOnly>: Clone, - for<'w> QueryFetch<'w, F::ReadOnly>: Clone, + for<'w> ::Fetch<'w>: Clone, + for<'w> ::Fetch<'w>: Clone, { let mut query = world.query_filtered::(); let iter = query.iter_combinations::(world); @@ -179,8 +179,8 @@ mod tests { Q: WorldQuery, F: ReadOnlyWorldQuery, F::ReadOnly: ArchetypeFilter, - for<'w> QueryFetch<'w, Q::ReadOnly>: Clone, - for<'w> QueryFetch<'w, F::ReadOnly>: Clone, + for<'w> ::Fetch<'w>: Clone, + for<'w> ::Fetch<'w>: Clone, { let mut query = world.query_filtered::(); let iter = query.iter(world); diff --git a/crates/bevy_ecs/src/system/query.rs b/crates/bevy_ecs/src/system/query.rs index fcd4039a7983d2..8581ad286fdbdf 100644 --- a/crates/bevy_ecs/src/system/query.rs +++ b/crates/bevy_ecs/src/system/query.rs @@ -704,7 +704,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 { @@ -778,7 +778,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 @@ -937,7 +937,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 { @@ -962,7 +962,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( @@ -1023,7 +1023,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() } @@ -1040,10 +1040,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 @@ -1289,7 +1286,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() } @@ -1319,7 +1316,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 diff --git a/crates/bevy_render/src/extract_component.rs b/crates/bevy_render/src/extract_component.rs index 38a9b597fb7624..182dee83c284d3 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 abb73fae70ec23..81d7608080d2c3 100644 --- a/crates/bevy_ui/src/entity.rs +++ b/crates/bevy_ui/src/entity.rs @@ -255,7 +255,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 71e68566b40e5c..848f9e821401ca 100644 --- a/examples/shader/shader_instancing.rs +++ b/examples/shader/shader_instancing.rs @@ -67,7 +67,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()) } }