From 5adacf014c9510285365b3325c9e6faa34d7fcce Mon Sep 17 00:00:00 2001 From: Tim Date: Mon, 9 Sep 2024 16:24:39 +0000 Subject: [PATCH] Use associated type bounds for `iter_many` and friends (#15040) # Objective Make the bounds for these query methods less intimidating. Continuation of #14107 My last pr was back in february :skull: --- crates/bevy_ecs/src/query/state.rs | 25 ++++++++----------------- crates/bevy_ecs/src/system/query.rs | 21 ++++++--------------- 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/crates/bevy_ecs/src/query/state.rs b/crates/bevy_ecs/src/query/state.rs index 93d71551492d6..9b4e108624684 100644 --- a/crates/bevy_ecs/src/query/state.rs +++ b/crates/bevy_ecs/src/query/state.rs @@ -1174,14 +1174,11 @@ impl QueryState { /// /// - [`iter_many_mut`](Self::iter_many_mut) to get mutable query items. #[inline] - pub fn iter_many<'w, 's, EntityList: IntoIterator>( + pub fn iter_many<'w, 's, EntityList: IntoIterator>>( &'s mut self, world: &'w World, entities: EntityList, - ) -> QueryManyIter<'w, 's, D::ReadOnly, F, EntityList::IntoIter> - where - EntityList::Item: Borrow, - { + ) -> QueryManyIter<'w, 's, D::ReadOnly, F, EntityList::IntoIter> { self.update_archetypes(world); // SAFETY: query is read only unsafe { @@ -1209,14 +1206,11 @@ impl QueryState { /// - [`iter_many`](Self::iter_many) to update archetypes. /// - [`iter_manual`](Self::iter_manual) to iterate over all query items. #[inline] - pub fn iter_many_manual<'w, 's, EntityList: IntoIterator>( + pub fn iter_many_manual<'w, 's, EntityList: IntoIterator>>( &'s self, world: &'w World, entities: EntityList, - ) -> QueryManyIter<'w, 's, D::ReadOnly, F, EntityList::IntoIter> - where - EntityList::Item: Borrow, - { + ) -> QueryManyIter<'w, 's, D::ReadOnly, F, EntityList::IntoIter> { self.validate_world(world.id()); // SAFETY: query is read only, world id is validated unsafe { @@ -1234,14 +1228,11 @@ impl QueryState { /// Items are returned in the order of the list of entities. /// Entities that don't match the query are skipped. #[inline] - pub fn iter_many_mut<'w, 's, EntityList: IntoIterator>( + pub fn iter_many_mut<'w, 's, EntityList: IntoIterator>>( &'s mut self, world: &'w mut World, entities: EntityList, - ) -> QueryManyIter<'w, 's, D, F, EntityList::IntoIter> - where - EntityList::Item: Borrow, - { + ) -> QueryManyIter<'w, 's, D, F, EntityList::IntoIter> { self.update_archetypes(world); let change_tick = world.change_tick(); let last_change_tick = world.last_change_tick(); @@ -1334,7 +1325,7 @@ impl QueryState { /// This does not validate that `world.id()` matches `self.world_id`. Calling this on a `world` /// with a mismatched [`WorldId`] is unsound. #[inline] - pub(crate) unsafe fn iter_many_unchecked_manual<'w, 's, EntityList: IntoIterator>( + pub(crate) unsafe fn iter_many_unchecked_manual<'w, 's, EntityList>( &'s self, entities: EntityList, world: UnsafeWorldCell<'w>, @@ -1342,7 +1333,7 @@ impl QueryState { this_run: Tick, ) -> QueryManyIter<'w, 's, D, F, EntityList::IntoIter> where - EntityList::Item: Borrow, + EntityList: IntoIterator>, { QueryManyIter::new(world, self, entities, last_run, this_run) } diff --git a/crates/bevy_ecs/src/system/query.rs b/crates/bevy_ecs/src/system/query.rs index ffe595b2cfd01..ddff01e19fa11 100644 --- a/crates/bevy_ecs/src/system/query.rs +++ b/crates/bevy_ecs/src/system/query.rs @@ -616,13 +616,10 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> { /// /// - [`iter_many_mut`](Self::iter_many_mut) to get mutable query items. #[inline] - pub fn iter_many( + pub fn iter_many>>( &self, entities: EntityList, - ) -> QueryManyIter<'_, 's, D::ReadOnly, F, EntityList::IntoIter> - where - EntityList::Item: Borrow, - { + ) -> QueryManyIter<'_, 's, D::ReadOnly, F, EntityList::IntoIter> { // SAFETY: // - `self.world` has permission to access the required components. // - The query is read-only, so it can be aliased even if it was originally mutable. @@ -670,13 +667,10 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> { /// # bevy_ecs::system::assert_is_system(system); /// ``` #[inline] - pub fn iter_many_mut( + pub fn iter_many_mut>>( &mut self, entities: EntityList, - ) -> QueryManyIter<'_, 's, D, F, EntityList::IntoIter> - where - EntityList::Item: Borrow, - { + ) -> QueryManyIter<'_, 's, D, F, EntityList::IntoIter> { // SAFETY: `self.world` has permission to access the required components. unsafe { self.state.iter_many_unchecked_manual( @@ -752,13 +746,10 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> { /// # See also /// /// - [`iter_many_mut`](Self::iter_many_mut) to safely access the query items. - pub unsafe fn iter_many_unsafe( + pub unsafe fn iter_many_unsafe>>( &self, entities: EntityList, - ) -> QueryManyIter<'_, 's, D, F, EntityList::IntoIter> - where - EntityList::Item: Borrow, - { + ) -> QueryManyIter<'_, 's, D, F, EntityList::IntoIter> { // SAFETY: // - `self.world` has permission to access the required components. // - The caller ensures that this operation will not result in any aliased mutable accesses.