Skip to content

Commit 5adacf0

Browse files
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 💀
1 parent 85e41dd commit 5adacf0

File tree

2 files changed

+14
-32
lines changed

2 files changed

+14
-32
lines changed

crates/bevy_ecs/src/query/state.rs

+8-17
Original file line numberDiff line numberDiff line change
@@ -1174,14 +1174,11 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
11741174
///
11751175
/// - [`iter_many_mut`](Self::iter_many_mut) to get mutable query items.
11761176
#[inline]
1177-
pub fn iter_many<'w, 's, EntityList: IntoIterator>(
1177+
pub fn iter_many<'w, 's, EntityList: IntoIterator<Item: Borrow<Entity>>>(
11781178
&'s mut self,
11791179
world: &'w World,
11801180
entities: EntityList,
1181-
) -> QueryManyIter<'w, 's, D::ReadOnly, F, EntityList::IntoIter>
1182-
where
1183-
EntityList::Item: Borrow<Entity>,
1184-
{
1181+
) -> QueryManyIter<'w, 's, D::ReadOnly, F, EntityList::IntoIter> {
11851182
self.update_archetypes(world);
11861183
// SAFETY: query is read only
11871184
unsafe {
@@ -1209,14 +1206,11 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
12091206
/// - [`iter_many`](Self::iter_many) to update archetypes.
12101207
/// - [`iter_manual`](Self::iter_manual) to iterate over all query items.
12111208
#[inline]
1212-
pub fn iter_many_manual<'w, 's, EntityList: IntoIterator>(
1209+
pub fn iter_many_manual<'w, 's, EntityList: IntoIterator<Item: Borrow<Entity>>>(
12131210
&'s self,
12141211
world: &'w World,
12151212
entities: EntityList,
1216-
) -> QueryManyIter<'w, 's, D::ReadOnly, F, EntityList::IntoIter>
1217-
where
1218-
EntityList::Item: Borrow<Entity>,
1219-
{
1213+
) -> QueryManyIter<'w, 's, D::ReadOnly, F, EntityList::IntoIter> {
12201214
self.validate_world(world.id());
12211215
// SAFETY: query is read only, world id is validated
12221216
unsafe {
@@ -1234,14 +1228,11 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
12341228
/// Items are returned in the order of the list of entities.
12351229
/// Entities that don't match the query are skipped.
12361230
#[inline]
1237-
pub fn iter_many_mut<'w, 's, EntityList: IntoIterator>(
1231+
pub fn iter_many_mut<'w, 's, EntityList: IntoIterator<Item: Borrow<Entity>>>(
12381232
&'s mut self,
12391233
world: &'w mut World,
12401234
entities: EntityList,
1241-
) -> QueryManyIter<'w, 's, D, F, EntityList::IntoIter>
1242-
where
1243-
EntityList::Item: Borrow<Entity>,
1244-
{
1235+
) -> QueryManyIter<'w, 's, D, F, EntityList::IntoIter> {
12451236
self.update_archetypes(world);
12461237
let change_tick = world.change_tick();
12471238
let last_change_tick = world.last_change_tick();
@@ -1334,15 +1325,15 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
13341325
/// This does not validate that `world.id()` matches `self.world_id`. Calling this on a `world`
13351326
/// with a mismatched [`WorldId`] is unsound.
13361327
#[inline]
1337-
pub(crate) unsafe fn iter_many_unchecked_manual<'w, 's, EntityList: IntoIterator>(
1328+
pub(crate) unsafe fn iter_many_unchecked_manual<'w, 's, EntityList>(
13381329
&'s self,
13391330
entities: EntityList,
13401331
world: UnsafeWorldCell<'w>,
13411332
last_run: Tick,
13421333
this_run: Tick,
13431334
) -> QueryManyIter<'w, 's, D, F, EntityList::IntoIter>
13441335
where
1345-
EntityList::Item: Borrow<Entity>,
1336+
EntityList: IntoIterator<Item: Borrow<Entity>>,
13461337
{
13471338
QueryManyIter::new(world, self, entities, last_run, this_run)
13481339
}

crates/bevy_ecs/src/system/query.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -616,13 +616,10 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> {
616616
///
617617
/// - [`iter_many_mut`](Self::iter_many_mut) to get mutable query items.
618618
#[inline]
619-
pub fn iter_many<EntityList: IntoIterator>(
619+
pub fn iter_many<EntityList: IntoIterator<Item: Borrow<Entity>>>(
620620
&self,
621621
entities: EntityList,
622-
) -> QueryManyIter<'_, 's, D::ReadOnly, F, EntityList::IntoIter>
623-
where
624-
EntityList::Item: Borrow<Entity>,
625-
{
622+
) -> QueryManyIter<'_, 's, D::ReadOnly, F, EntityList::IntoIter> {
626623
// SAFETY:
627624
// - `self.world` has permission to access the required components.
628625
// - 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> {
670667
/// # bevy_ecs::system::assert_is_system(system);
671668
/// ```
672669
#[inline]
673-
pub fn iter_many_mut<EntityList: IntoIterator>(
670+
pub fn iter_many_mut<EntityList: IntoIterator<Item: Borrow<Entity>>>(
674671
&mut self,
675672
entities: EntityList,
676-
) -> QueryManyIter<'_, 's, D, F, EntityList::IntoIter>
677-
where
678-
EntityList::Item: Borrow<Entity>,
679-
{
673+
) -> QueryManyIter<'_, 's, D, F, EntityList::IntoIter> {
680674
// SAFETY: `self.world` has permission to access the required components.
681675
unsafe {
682676
self.state.iter_many_unchecked_manual(
@@ -752,13 +746,10 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> {
752746
/// # See also
753747
///
754748
/// - [`iter_many_mut`](Self::iter_many_mut) to safely access the query items.
755-
pub unsafe fn iter_many_unsafe<EntityList: IntoIterator>(
749+
pub unsafe fn iter_many_unsafe<EntityList: IntoIterator<Item: Borrow<Entity>>>(
756750
&self,
757751
entities: EntityList,
758-
) -> QueryManyIter<'_, 's, D, F, EntityList::IntoIter>
759-
where
760-
EntityList::Item: Borrow<Entity>,
761-
{
752+
) -> QueryManyIter<'_, 's, D, F, EntityList::IntoIter> {
762753
// SAFETY:
763754
// - `self.world` has permission to access the required components.
764755
// - The caller ensures that this operation will not result in any aliased mutable accesses.

0 commit comments

Comments
 (0)