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 b94b59a0b6525..3a1d20e929314 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 ce91c2d2e04a8..265f0d2649270 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 6d0e3d509428c..a25e7855d3ef9 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 a4670944807f8..3f875ceeba2f4 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,18 @@ 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; + ) -> Self::Item<'w>; - /// 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) -> Self::Item<'w>; /// # Safety /// @@ -398,10 +397,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 +407,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 +429,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 +436,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 +449,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 } @@ -504,10 +487,7 @@ unsafe impl WorldQuery for Entity { } #[inline] - 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) -> Self::Item<'w> { let entities = fetch .entities .unwrap_or_else(|| debug_checked_unreachable()); @@ -516,9 +496,9 @@ unsafe impl WorldQuery for Entity { #[inline] unsafe fn archetype_fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, archetype_index: usize, - ) -> >::Item { + ) -> Self::Item<'w> { let entities = fetch .entities .unwrap_or_else(|| debug_checked_unreachable()); @@ -544,11 +524,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 +537,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 +598,9 @@ unsafe impl WorldQuery for &T { #[inline] unsafe fn archetype_fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, archetype_index: usize, - ) -> >::Item { + ) -> Self::Item<'w> { match T::Storage::STORAGE_TYPE { StorageType::Table => { let (entity_table_rows, table_components) = fetch @@ -648,10 +625,7 @@ unsafe impl WorldQuery for &T { } #[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) -> Self::Item<'w> { let components = fetch .table_components .unwrap_or_else(|| debug_checked_unreachable()); @@ -706,11 +680,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 +696,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 +765,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 { + ) -> Self::Item<'w> { match T::Storage::STORAGE_TYPE { StorageType::Table => { let (entity_table_rows, (table_components, table_ticks)) = fetch @@ -835,10 +806,7 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T { } #[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) -> Self::Item<'w> { let (table_components, table_ticks) = fetch .table_components .zip(fetch.table_ticks) @@ -901,19 +869,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 +888,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 +936,9 @@ unsafe impl WorldQuery for Option { #[inline] unsafe fn archetype_fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, archetype_index: usize, - ) -> >::Item { + ) -> Self::Item<'w> { if fetch.matches { Some(T::archetype_fetch(&mut fetch.fetch, archetype_index)) } else { @@ -982,10 +947,7 @@ unsafe impl WorldQuery for Option { } #[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) -> Self::Item<'w> { if fetch.matches { Some(T::table_fetch(&mut fetch.fetch, table_row)) } else { @@ -1028,11 +990,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 +1082,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 +1148,9 @@ unsafe impl WorldQuery for ChangeTrackers { #[inline] unsafe fn archetype_fetch<'w>( - fetch: &mut >::Fetch, + fetch: &mut Self::Fetch<'w>, archetype_index: usize, - ) -> >::Item { + ) -> Self::Item<'w> { match T::Storage::STORAGE_TYPE { StorageType::Table => { let entity_table_rows = fetch @@ -1232,10 +1191,7 @@ unsafe impl WorldQuery for ChangeTrackers { } #[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) -> Self::Item<'w> { ChangeTrackers { component_ticks: { let table_ticks = fetch @@ -1283,28 +1239,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 +1258,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 +1268,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 +1283,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) -> Self::Item<'w> { 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) -> Self::Item<'w> { 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 +1346,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 +1363,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 +1373,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 +1385,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 +1398,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) -> Self::Item<'w> { let ($($name,)*) = _fetch; ($( $name.1.then(|| $name::table_fetch(&mut $name.0, _table_row)), @@ -1466,7 +1407,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) -> Self::Item<'w> { let ($($name,)*) = _fetch; ($( $name.1.then(|| $name::archetype_fetch(&mut $name.0, _archetype_index)), @@ -1541,6 +1482,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 +1516,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 { + ) -> Self::Item<'w> { } #[inline(always)] - unsafe fn table_fetch<'w>( - _fetch: &mut (), - _table_row: usize, - ) -> >::Item { - } + unsafe fn table_fetch<'w>(_fetch: &mut (), _table_row: usize) -> Self::Item<'w> {} fn update_component_access(_state: &Q::State, _access: &mut FilteredAccess) {} @@ -1606,9 +1545,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 960c6d4bcd7f5..ad64c86c55e3d 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, }; @@ -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,17 +81,13 @@ unsafe impl WorldQuery for With { #[inline] unsafe fn archetype_fetch<'w>( - _fetch: &mut >::Fetch, + _fetch: &mut Self::Fetch<'w>, _archetype_index: usize, - ) -> >::Item { + ) -> Self::Item<'w> { } #[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) -> Self::Item<'w> {} #[inline] fn update_component_access(&id: &ComponentId, access: &mut FilteredAccess) { @@ -155,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, @@ -196,17 +183,13 @@ unsafe impl WorldQuery for Without { #[inline] unsafe fn archetype_fetch<'w>( - _fetch: &mut >::Fetch, + _fetch: &mut Self::Fetch<'w>, _archetype_index: usize, - ) -> >::Item { + ) -> Self::Item<'w> { } #[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) -> Self::Item<'w> {} #[inline] fn update_component_access(&id: &ComponentId, access: &mut FilteredAccess) { @@ -233,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 {} @@ -276,13 +254,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 +272,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 +290,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 +299,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 +311,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 +323,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) -> Self::Item<'w> { 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) -> Self::Item<'w> { 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 +421,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 +454,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 +469,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) -> Self::Item<'w> { $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) -> Self::Item<'w> { 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 +494,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 +532,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 374c0f7d6f1ba..9db660070b862 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 location = match self.entities.get(*entity.borrow()) { Some(location) => location, @@ -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. @@ -195,7 +195,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 { @@ -329,10 +329,10 @@ 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]> where - QueryFetch<'w, Q>: Clone, - QueryFetch<'w, F>: Clone, + Q::Fetch<'w>: Clone, + F::Fetch<'w>: Clone, { if K == 0 { return None; @@ -358,9 +358,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()); } @@ -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,10 +391,10 @@ 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]; + type Item = [Q::Item<'w>; K]; #[inline] fn next(&mut self) -> Option { @@ -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 { @@ -540,7 +540,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 { if Self::IS_DENSE { Some(Q::table_fetch(&mut self.fetch, self.current_index - 1)) @@ -564,7 +564,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/mod.rs b/crates/bevy_ecs/src/query/mod.rs index 4adfbd2e369ee..9619f858204c4 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/query/state.rs b/crates/bevy_ecs/src/query/state.rs index 0f77b03a3b3f9..611621ddfc385 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}; -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) @@ -482,7 +482,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 { @@ -779,11 +779,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); @@ -806,7 +802,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, @@ -854,7 +850,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, @@ -886,7 +882,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, @@ -912,7 +908,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, @@ -973,7 +969,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, @@ -1141,7 +1137,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() } @@ -1155,7 +1151,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 @@ -1181,7 +1177,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()) } @@ -1202,7 +1198,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 fcd4039a7983d..1eb62a8fa0335 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}, }; @@ -196,7 +196,7 @@ use std::{any::TypeId, borrow::Borrow, fmt::Debug}; /// |[`single`]\([`_mut`][`single_mut`]),
[`get_single`]\([`_mut`][`get_single_mut`])|Returns the query item while verifying that there aren't others.| /// /// There are two methods for each type of query operation: immutable and mutable (ending with `_mut`). -/// When using immutable methods, the query items returned are of type [`ROQueryItem`], a read-only version of the query item. +/// When using immutable methods, the query items returned are of type `ROQueryItem`, a read-only version of the query item. /// In this circumstance, every mutable reference in the query fetch type parameter is substituted by a shared reference. /// /// # Performance @@ -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 @@ -1402,7 +1399,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_render/src/extract_component.rs b/crates/bevy_render/src/extract_component.rs index 38a9b597fb762..182dee83c284d 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 abb73fae70ec2..81d7608080d2c 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 71e68566b40e5..848f9e821401c 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()) } }