Skip to content

Commit

Permalink
Use associated type bounds for iter_many and friends (#15040)
Browse files Browse the repository at this point in the history
# Objective

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

<sub>My last pr was back in february 💀
  • Loading branch information
tim-blackbird authored Sep 9, 2024
1 parent 85e41dd commit 5adacf0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 32 deletions.
25 changes: 8 additions & 17 deletions crates/bevy_ecs/src/query/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,14 +1174,11 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
///
/// - [`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<Item: Borrow<Entity>>>(
&'s mut self,
world: &'w World,
entities: EntityList,
) -> QueryManyIter<'w, 's, D::ReadOnly, F, EntityList::IntoIter>
where
EntityList::Item: Borrow<Entity>,
{
) -> QueryManyIter<'w, 's, D::ReadOnly, F, EntityList::IntoIter> {
self.update_archetypes(world);
// SAFETY: query is read only
unsafe {
Expand Down Expand Up @@ -1209,14 +1206,11 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
/// - [`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<Item: Borrow<Entity>>>(
&'s self,
world: &'w World,
entities: EntityList,
) -> QueryManyIter<'w, 's, D::ReadOnly, F, EntityList::IntoIter>
where
EntityList::Item: Borrow<Entity>,
{
) -> QueryManyIter<'w, 's, D::ReadOnly, F, EntityList::IntoIter> {
self.validate_world(world.id());
// SAFETY: query is read only, world id is validated
unsafe {
Expand All @@ -1234,14 +1228,11 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
/// 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<Item: Borrow<Entity>>>(
&'s mut self,
world: &'w mut World,
entities: EntityList,
) -> QueryManyIter<'w, 's, D, F, EntityList::IntoIter>
where
EntityList::Item: Borrow<Entity>,
{
) -> 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();
Expand Down Expand Up @@ -1334,15 +1325,15 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
/// 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>,
last_run: Tick,
this_run: Tick,
) -> QueryManyIter<'w, 's, D, F, EntityList::IntoIter>
where
EntityList::Item: Borrow<Entity>,
EntityList: IntoIterator<Item: Borrow<Entity>>,
{
QueryManyIter::new(world, self, entities, last_run, this_run)
}
Expand Down
21 changes: 6 additions & 15 deletions crates/bevy_ecs/src/system/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EntityList: IntoIterator>(
pub fn iter_many<EntityList: IntoIterator<Item: Borrow<Entity>>>(
&self,
entities: EntityList,
) -> QueryManyIter<'_, 's, D::ReadOnly, F, EntityList::IntoIter>
where
EntityList::Item: Borrow<Entity>,
{
) -> 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.
Expand Down Expand Up @@ -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<EntityList: IntoIterator>(
pub fn iter_many_mut<EntityList: IntoIterator<Item: Borrow<Entity>>>(
&mut self,
entities: EntityList,
) -> QueryManyIter<'_, 's, D, F, EntityList::IntoIter>
where
EntityList::Item: Borrow<Entity>,
{
) -> QueryManyIter<'_, 's, D, F, EntityList::IntoIter> {
// SAFETY: `self.world` has permission to access the required components.
unsafe {
self.state.iter_many_unchecked_manual(
Expand Down Expand Up @@ -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<EntityList: IntoIterator>(
pub unsafe fn iter_many_unsafe<EntityList: IntoIterator<Item: Borrow<Entity>>>(
&self,
entities: EntityList,
) -> QueryManyIter<'_, 's, D, F, EntityList::IntoIter>
where
EntityList::Item: Borrow<Entity>,
{
) -> 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.
Expand Down

0 comments on commit 5adacf0

Please sign in to comment.