Use associated type bounds for iter_many and friends (#15040)

# Objective

Make the bounds for these query methods less intimidating.
Continuation of #14107

<sub>My last pr was back in february 💀
This commit is contained in:
Tim 2024-09-09 16:24:39 +00:00 committed by GitHub
parent 85e41ddace
commit 5adacf014c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 32 deletions

View File

@ -1174,14 +1174,11 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
/// ///
/// - [`iter_many_mut`](Self::iter_many_mut) to get mutable query items. /// - [`iter_many_mut`](Self::iter_many_mut) to get mutable query items.
#[inline] #[inline]
pub fn iter_many<'w, 's, EntityList: IntoIterator>( pub fn iter_many<'w, 's, EntityList: IntoIterator<Item: Borrow<Entity>>>(
&'s mut self, &'s mut self,
world: &'w World, world: &'w World,
entities: EntityList, entities: EntityList,
) -> QueryManyIter<'w, 's, D::ReadOnly, F, EntityList::IntoIter> ) -> QueryManyIter<'w, 's, D::ReadOnly, F, EntityList::IntoIter> {
where
EntityList::Item: Borrow<Entity>,
{
self.update_archetypes(world); self.update_archetypes(world);
// SAFETY: query is read only // SAFETY: query is read only
unsafe { unsafe {
@ -1209,14 +1206,11 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
/// - [`iter_many`](Self::iter_many) to update archetypes. /// - [`iter_many`](Self::iter_many) to update archetypes.
/// - [`iter_manual`](Self::iter_manual) to iterate over all query items. /// - [`iter_manual`](Self::iter_manual) to iterate over all query items.
#[inline] #[inline]
pub fn iter_many_manual<'w, 's, EntityList: IntoIterator>( pub fn iter_many_manual<'w, 's, EntityList: IntoIterator<Item: Borrow<Entity>>>(
&'s self, &'s self,
world: &'w World, world: &'w World,
entities: EntityList, entities: EntityList,
) -> QueryManyIter<'w, 's, D::ReadOnly, F, EntityList::IntoIter> ) -> QueryManyIter<'w, 's, D::ReadOnly, F, EntityList::IntoIter> {
where
EntityList::Item: Borrow<Entity>,
{
self.validate_world(world.id()); self.validate_world(world.id());
// SAFETY: query is read only, world id is validated // SAFETY: query is read only, world id is validated
unsafe { unsafe {
@ -1234,14 +1228,11 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
/// Items are returned in the order of the list of entities. /// Items are returned in the order of the list of entities.
/// Entities that don't match the query are skipped. /// Entities that don't match the query are skipped.
#[inline] #[inline]
pub fn iter_many_mut<'w, 's, EntityList: IntoIterator>( pub fn iter_many_mut<'w, 's, EntityList: IntoIterator<Item: Borrow<Entity>>>(
&'s mut self, &'s mut self,
world: &'w mut World, world: &'w mut World,
entities: EntityList, entities: EntityList,
) -> QueryManyIter<'w, 's, D, F, EntityList::IntoIter> ) -> QueryManyIter<'w, 's, D, F, EntityList::IntoIter> {
where
EntityList::Item: Borrow<Entity>,
{
self.update_archetypes(world); self.update_archetypes(world);
let change_tick = world.change_tick(); let change_tick = world.change_tick();
let last_change_tick = world.last_change_tick(); let last_change_tick = world.last_change_tick();
@ -1334,7 +1325,7 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
/// This does not validate that `world.id()` matches `self.world_id`. Calling this on a `world` /// This does not validate that `world.id()` matches `self.world_id`. Calling this on a `world`
/// with a mismatched [`WorldId`] is unsound. /// with a mismatched [`WorldId`] is unsound.
#[inline] #[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, &'s self,
entities: EntityList, entities: EntityList,
world: UnsafeWorldCell<'w>, world: UnsafeWorldCell<'w>,
@ -1342,7 +1333,7 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
this_run: Tick, this_run: Tick,
) -> QueryManyIter<'w, 's, D, F, EntityList::IntoIter> ) -> QueryManyIter<'w, 's, D, F, EntityList::IntoIter>
where where
EntityList::Item: Borrow<Entity>, EntityList: IntoIterator<Item: Borrow<Entity>>,
{ {
QueryManyIter::new(world, self, entities, last_run, this_run) QueryManyIter::new(world, self, entities, last_run, this_run)
} }

View File

@ -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. /// - [`iter_many_mut`](Self::iter_many_mut) to get mutable query items.
#[inline] #[inline]
pub fn iter_many<EntityList: IntoIterator>( pub fn iter_many<EntityList: IntoIterator<Item: Borrow<Entity>>>(
&self, &self,
entities: EntityList, entities: EntityList,
) -> QueryManyIter<'_, 's, D::ReadOnly, F, EntityList::IntoIter> ) -> QueryManyIter<'_, 's, D::ReadOnly, F, EntityList::IntoIter> {
where
EntityList::Item: Borrow<Entity>,
{
// SAFETY: // SAFETY:
// - `self.world` has permission to access the required components. // - `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. // - 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); /// # bevy_ecs::system::assert_is_system(system);
/// ``` /// ```
#[inline] #[inline]
pub fn iter_many_mut<EntityList: IntoIterator>( pub fn iter_many_mut<EntityList: IntoIterator<Item: Borrow<Entity>>>(
&mut self, &mut self,
entities: EntityList, entities: EntityList,
) -> QueryManyIter<'_, 's, D, F, EntityList::IntoIter> ) -> QueryManyIter<'_, 's, D, F, EntityList::IntoIter> {
where
EntityList::Item: Borrow<Entity>,
{
// SAFETY: `self.world` has permission to access the required components. // SAFETY: `self.world` has permission to access the required components.
unsafe { unsafe {
self.state.iter_many_unchecked_manual( self.state.iter_many_unchecked_manual(
@ -752,13 +746,10 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> {
/// # See also /// # See also
/// ///
/// - [`iter_many_mut`](Self::iter_many_mut) to safely access the query items. /// - [`iter_many_mut`](Self::iter_many_mut) to safely access the query items.
pub unsafe fn iter_many_unsafe<EntityList: IntoIterator>( pub unsafe fn iter_many_unsafe<EntityList: IntoIterator<Item: Borrow<Entity>>>(
&self, &self,
entities: EntityList, entities: EntityList,
) -> QueryManyIter<'_, 's, D, F, EntityList::IntoIter> ) -> QueryManyIter<'_, 's, D, F, EntityList::IntoIter> {
where
EntityList::Item: Borrow<Entity>,
{
// SAFETY: // SAFETY:
// - `self.world` has permission to access the required components. // - `self.world` has permission to access the required components.
// - The caller ensures that this operation will not result in any aliased mutable accesses. // - The caller ensures that this operation will not result in any aliased mutable accesses.