diff --git a/crates/bevy_ecs/src/query/fetch.rs b/crates/bevy_ecs/src/query/fetch.rs index aaace631aa..97bdda0462 100644 --- a/crates/bevy_ecs/src/query/fetch.rs +++ b/crates/bevy_ecs/src/query/fetch.rs @@ -41,11 +41,11 @@ use std::{ /// /// [`Or`]: crate::query::Or pub trait WorldQuery { - type Fetch: for<'a> Fetch<'a, State = Self::State>; + type Fetch: for<'world, 'state> Fetch<'world, 'state, State = Self::State>; type State: FetchState; } -pub trait Fetch<'w>: Sized { +pub trait Fetch<'world, 'state>: Sized { type Item; type State: FetchState; @@ -173,7 +173,7 @@ unsafe impl FetchState for EntityState { } } -impl<'w> Fetch<'w> for EntityFetch { +impl<'w, 's> Fetch<'w, 's> for EntityFetch { type Item = Entity; type State = EntityState; @@ -296,7 +296,7 @@ impl Clone for ReadFetch { /// SAFETY: access is read only unsafe impl ReadOnlyFetch for ReadFetch {} -impl<'w, T: Component> Fetch<'w> for ReadFetch { +impl<'w, 's, T: Component> Fetch<'w, 's> for ReadFetch { type Item = &'w T; type State = ReadState; @@ -459,7 +459,7 @@ unsafe impl FetchState for WriteState { } } -impl<'w, T: Component> Fetch<'w> for WriteFetch { +impl<'w, 's, T: Component> Fetch<'w, 's> for WriteFetch { type Item = Mut<'w, T>; type State = WriteState; @@ -619,7 +619,7 @@ unsafe impl FetchState for OptionState { } } -impl<'w, T: Fetch<'w>> Fetch<'w> for OptionFetch { +impl<'w, 's, T: Fetch<'w, 's>> Fetch<'w, 's> for OptionFetch { type Item = Option; type State = OptionState; @@ -810,7 +810,7 @@ pub struct ChangeTrackersFetch { /// SAFETY: access is read only unsafe impl ReadOnlyFetch for ChangeTrackersFetch {} -impl<'w, T: Component> Fetch<'w> for ChangeTrackersFetch { +impl<'w, 's, T: Component> Fetch<'w, 's> for ChangeTrackersFetch { type Item = ChangeTrackers; type State = ChangeTrackersState; @@ -913,7 +913,7 @@ impl<'w, T: Component> Fetch<'w> for ChangeTrackersFetch { macro_rules! impl_tuple_fetch { ($(($name: ident, $state: ident)),*) => { #[allow(non_snake_case)] - impl<'a, $($name: Fetch<'a>),*> Fetch<'a> for ($($name,)*) { + impl<'w, 's, $($name: Fetch<'w, 's>),*> Fetch<'w, 's> for ($($name,)*) { type Item = ($($name::Item,)*); type State = ($($name::State,)*); diff --git a/crates/bevy_ecs/src/query/filter.rs b/crates/bevy_ecs/src/query/filter.rs index cd3d2c9c80..9a46035c62 100644 --- a/crates/bevy_ecs/src/query/filter.rs +++ b/crates/bevy_ecs/src/query/filter.rs @@ -12,7 +12,7 @@ use std::{cell::UnsafeCell, marker::PhantomData, ptr}; /// Extension trait for [`Fetch`] containing methods used by query filters. /// This trait exists to allow "short circuit" behaviors for relevant query filter fetches. -pub trait FilterFetch: for<'a> Fetch<'a> { +pub trait FilterFetch: for<'w, 's> Fetch<'w, 's> { /// # Safety /// /// Must always be called _after_ [`Fetch::set_archetype`]. `archetype_index` must be in the range @@ -28,7 +28,7 @@ pub trait FilterFetch: for<'a> Fetch<'a> { impl FilterFetch for T where - T: for<'a> Fetch<'a, Item = bool>, + T: for<'w, 's> Fetch<'w, 's, Item = bool>, { #[inline] unsafe fn archetype_filter_fetch(&mut self, archetype_index: usize) -> bool { @@ -119,7 +119,7 @@ unsafe impl FetchState for WithState { } } -impl<'a, T: Component> Fetch<'a> for WithFetch { +impl<'w, 's, T: Component> Fetch<'w, 's> for WithFetch { type Item = bool; type State = WithState; @@ -238,7 +238,7 @@ unsafe impl FetchState for WithoutState { } } -impl<'a, T: Component> Fetch<'a> for WithoutFetch { +impl<'w, 's, T: Component> Fetch<'w, 's> for WithoutFetch { type Item = bool; type State = WithoutState; @@ -338,7 +338,7 @@ unsafe impl FetchState for WithBundleState { } } -impl<'a, T: Bundle> Fetch<'a> for WithBundleFetch { +impl<'w, 's, T: Bundle> Fetch<'w, 's> for WithBundleFetch { type Item = bool; type State = WithBundleState; @@ -446,8 +446,8 @@ macro_rules! impl_query_filter_tuple { #[allow(unused_variables)] #[allow(non_snake_case)] - impl<'a, $($filter: FilterFetch),*> Fetch<'a> for Or<($(OrFetch<$filter>,)*)> { - type State = Or<($(<$filter as Fetch<'a>>::State,)*)>; + impl<'w, 's, $($filter: FilterFetch),*> Fetch<'w, 's> for Or<($(OrFetch<$filter>,)*)> { + type State = Or<($(<$filter as Fetch<'w, 's>>::State,)*)>; type Item = bool; unsafe fn init(world: &World, state: &Self::State, last_change_tick: u32, change_tick: u32) -> Self { @@ -612,7 +612,7 @@ macro_rules! impl_tick_filter { } } - impl<'a, T: Component> Fetch<'a> for $fetch_name { + impl<'w, 's, T: Component> Fetch<'w, 's> for $fetch_name { type State = $state_name; type Item = bool; diff --git a/crates/bevy_ecs/src/query/iter.rs b/crates/bevy_ecs/src/query/iter.rs index 04e04ae4bf..7585e15087 100644 --- a/crates/bevy_ecs/src/query/iter.rs +++ b/crates/bevy_ecs/src/query/iter.rs @@ -131,7 +131,7 @@ impl<'w, 's, Q: WorldQuery, F: WorldQuery> Iterator for QueryIter<'w, 's, Q, F> where F::Fetch: FilterFetch, { - type Item = >::Item; + type Item = >::Item; // NOTE: If you are changing query iteration code, remember to update the following places, where relevant: // QueryIter, QueryIterationCursor, QueryState::for_each_unchecked_manual, QueryState::par_for_each_unchecked_manual @@ -279,7 +279,7 @@ where /// It is always safe for shared access. unsafe fn fetch_next_aliased_unchecked<'a>( &mut self, - ) -> Option<[>::Item; K]> + ) -> Option<[>::Item; K]> where Q::Fetch: Clone, F::Fetch: Clone, @@ -309,7 +309,7 @@ where } // TODO: use MaybeUninit::uninit_array if it stabilizes - let mut values: [MaybeUninit<>::Item>; K] = + let mut values: [MaybeUninit<>::Item>; K] = MaybeUninit::uninit().assume_init(); for (value, cursor) in values.iter_mut().zip(&mut self.cursors) { @@ -317,15 +317,15 @@ where } // TODO: use MaybeUninit::array_assume_init if it stabilizes - let values: [>::Item; K] = - (&values as *const _ as *const [>::Item; K]).read(); + let values: [>::Item; K] = + (&values as *const _ as *const [>::Item; K]).read(); Some(values) } /// Get next combination of queried components #[inline] - pub fn fetch_next(&mut self) -> Option<[>::Item; K]> + pub fn fetch_next(&mut self) -> Option<[>::Item; K]> where Q::Fetch: Clone, F::Fetch: Clone, @@ -346,7 +346,7 @@ where Q::Fetch: Clone + ReadOnlyFetch, F::Fetch: Clone + FilterFetch + ReadOnlyFetch, { - type Item = [>::Item; K]; + type Item = [>::Item; K]; #[inline] fn next(&mut self) -> Option { @@ -476,7 +476,7 @@ where /// retrieve item returned from most recent `next` call again. #[inline] - unsafe fn peek_last<'w>(&mut self) -> Option<>::Item> { + unsafe fn peek_last<'w>(&mut self) -> Option<>::Item> { if self.current_index > 0 { if self.is_dense { Some(self.fetch.table_fetch(self.current_index - 1)) @@ -497,7 +497,7 @@ where tables: &'w Tables, archetypes: &'w Archetypes, query_state: &'s QueryState, - ) -> Option<>::Item> { + ) -> Option<>::Item> { if self.is_dense { loop { if self.current_index == self.current_len { diff --git a/crates/bevy_ecs/src/query/state.rs b/crates/bevy_ecs/src/query/state.rs index 05c009c6e8..6349d3101b 100644 --- a/crates/bevy_ecs/src/query/state.rs +++ b/crates/bevy_ecs/src/query/state.rs @@ -121,7 +121,7 @@ where &mut self, world: &'w World, entity: Entity, - ) -> Result<>::Item, QueryEntityError> + ) -> Result<>::Item, QueryEntityError> where Q::Fetch: ReadOnlyFetch, { @@ -134,7 +134,7 @@ where &mut self, world: &'w mut World, entity: Entity, - ) -> Result<>::Item, QueryEntityError> { + ) -> Result<>::Item, QueryEntityError> { // SAFETY: query has unique world access unsafe { self.get_unchecked(world, entity) } } @@ -148,7 +148,7 @@ where &mut self, world: &'w World, entity: Entity, - ) -> Result<>::Item, QueryEntityError> { + ) -> Result<>::Item, QueryEntityError> { self.validate_world_and_update_archetypes(world); self.get_unchecked_manual( world, @@ -167,7 +167,7 @@ where entity: Entity, last_change_tick: u32, change_tick: u32, - ) -> Result<>::Item, QueryEntityError> { + ) -> Result<>::Item, QueryEntityError> { let location = world .entities .get(entity) @@ -290,10 +290,10 @@ where } #[inline] - pub fn for_each<'w>( - &mut self, + pub fn for_each<'w, 's>( + &'s mut self, world: &'w World, - func: impl FnMut(>::Item), + func: impl FnMut(>::Item), ) where Q::Fetch: ReadOnlyFetch, { @@ -304,10 +304,10 @@ where } #[inline] - pub fn for_each_mut<'w>( - &mut self, + pub fn for_each_mut<'w, 's>( + &'s mut self, world: &'w mut World, - func: impl FnMut(>::Item), + func: impl FnMut(>::Item), ) { // SAFETY: query has unique world access unsafe { @@ -320,10 +320,10 @@ where /// 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>( - &mut self, + pub unsafe fn for_each_unchecked<'w, 's>( + &'s mut self, world: &'w World, - func: impl FnMut(>::Item), + func: impl FnMut(>::Item), ) { self.validate_world_and_update_archetypes(world); self.for_each_unchecked_manual( @@ -335,12 +335,12 @@ where } #[inline] - pub fn par_for_each<'w>( - &mut self, + pub fn par_for_each<'w, 's>( + &'s mut self, world: &'w World, task_pool: &TaskPool, batch_size: usize, - func: impl Fn(>::Item) + Send + Sync + Clone, + func: impl Fn(>::Item) + Send + Sync + Clone, ) where Q::Fetch: ReadOnlyFetch, { @@ -351,12 +351,12 @@ where } #[inline] - pub fn par_for_each_mut<'w>( - &mut self, + pub fn par_for_each_mut<'w, 's>( + &'s mut self, world: &'w mut World, task_pool: &TaskPool, batch_size: usize, - func: impl Fn(>::Item) + Send + Sync + Clone, + func: impl Fn(>::Item) + Send + Sync + Clone, ) { // SAFETY: query has unique world access unsafe { @@ -369,12 +369,12 @@ where /// 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>( - &mut self, + pub unsafe fn par_for_each_unchecked<'w, 's>( + &'s mut self, world: &'w World, task_pool: &TaskPool, batch_size: usize, - func: impl Fn(>::Item) + Send + Sync + Clone, + func: impl Fn(>::Item) + Send + Sync + Clone, ) { self.validate_world_and_update_archetypes(world); self.par_for_each_unchecked_manual( @@ -396,7 +396,7 @@ where pub(crate) unsafe fn for_each_unchecked_manual<'w, 's>( &'s self, world: &'w World, - mut func: impl FnMut(>::Item), + mut func: impl FnMut(>::Item), last_change_tick: u32, change_tick: u32, ) { @@ -450,7 +450,7 @@ where world: &'w World, task_pool: &TaskPool, batch_size: usize, - func: impl Fn(>::Item) + Send + Sync + Clone, + func: impl Fn(>::Item) + Send + Sync + Clone, last_change_tick: u32, change_tick: u32, ) { diff --git a/crates/bevy_ecs/src/system/query.rs b/crates/bevy_ecs/src/system/query.rs index 5efc282615..05ff84a798 100644 --- a/crates/bevy_ecs/src/system/query.rs +++ b/crates/bevy_ecs/src/system/query.rs @@ -266,7 +266,7 @@ where /// /// This can only be called for read-only queries, see [`Self::for_each_mut`] for write-queries. #[inline] - pub fn for_each(&self, f: impl FnMut(>::Item)) + pub fn for_each<'s>(&'s self, f: impl FnMut(>::Item)) where Q::Fetch: ReadOnlyFetch, { @@ -285,7 +285,7 @@ where /// Runs `f` on each query result. This is faster than the equivalent iter() method, but cannot /// be chained like a normal [`Iterator`]. #[inline] - pub fn for_each_mut(&mut self, f: impl FnMut(>::Item)) { + pub fn for_each_mut<'s>(&'s mut self, f: impl FnMut(>::Item)) { // SAFE: system runs without conflicts with other systems. same-system queries have runtime // borrow checks when they conflict unsafe { @@ -303,11 +303,11 @@ where /// This can only be called for read-only queries, see [`Self::par_for_each_mut`] for /// write-queries. #[inline] - pub fn par_for_each( - &self, + pub fn par_for_each<'s>( + &'s self, task_pool: &TaskPool, batch_size: usize, - f: impl Fn(>::Item) + Send + Sync + Clone, + f: impl Fn(>::Item) + Send + Sync + Clone, ) where Q::Fetch: ReadOnlyFetch, { @@ -327,11 +327,11 @@ where /// Runs `f` on each query result in parallel using the given task pool. #[inline] - pub fn par_for_each_mut( - &mut self, + pub fn par_for_each_mut<'s>( + &'s mut self, task_pool: &TaskPool, batch_size: usize, - f: impl Fn(>::Item) + Send + Sync + Clone, + f: impl Fn(>::Item) + Send + Sync + Clone, ) { // SAFE: system runs without conflicts with other systems. same-system queries have runtime // borrow checks when they conflict @@ -511,7 +511,7 @@ where /// ``` /// /// This can only be called for read-only queries, see [`Self::single_mut`] for write-queries. - pub fn single(&self) -> Result<>::Item, QuerySingleError> + pub fn single(&self) -> Result<>::Item, QuerySingleError> where Q::Fetch: ReadOnlyFetch, { @@ -530,7 +530,7 @@ where /// Gets the query result if it is only a single result, otherwise returns a /// [`QuerySingleError`]. - pub fn single_mut(&mut self) -> Result<>::Item, QuerySingleError> { + pub fn single_mut(&mut self) -> Result<>::Item, QuerySingleError> { let mut query = self.iter_mut(); let first = query.next(); let extra = query.next().is_some();