Repository: bevyengine/bevy
File: crates/bevy_ecs/src/query/iter.rs (Rust)

Code before:
    ///
    /// # Safety
    ///  - all `rows` must be in `[0, tables.entity_count)`.
    ///  - `table` must match Q and F
    ///  - Both `Q::IS_DENSE` and `F::IS_DENSE` must be true.
    #[inline]
    pub(super) unsafe fn for_each_table<Func>(
        &mut self,
        func: &mut Func,
        table: &'w Table,
        rows: Range<usize>,
    ) where
        Func: FnMut(Q::Item<'w>),
    {
        self.fold_table((), &mut |_, item| func(item), table, rows);
    }

    /// Executes the equivalent of [`Iterator::for_each`] over a contiguous segment
    /// from an archetype.
    ///
    /// # Safety
    ///  - all `indices` must be in `[0, archetype.len())`.
    ///  - `archetype` must match Q and F
    ///  - Either `Q::IS_DENSE` or `F::IS_DENSE` must be false.
    #[inline]
    pub(super) unsafe fn for_each_archetype<Func>(
        &mut self,
        func: &mut Func,
        archetype: &'w Archetype,
        indices: Range<usize>,
    ) where
        Func: FnMut(Q::Item<'w>),
    {
        self.fold_archetype((), &mut |_, item| func(item), archetype, indices);
    }
}

impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Iterator for QueryIter<'w, 's, Q, F> {
    type Item = Q::Item<'w>;

    #[inline(always)]
    fn next(&mut self) -> Option<Self::Item> {
        // SAFETY:
        // `tables` and `archetypes` belong to the same world that the cursor was initialized for.
        // `query_state` is the state that was passed to `QueryIterationCursor::init`.
        unsafe {
            self.cursor
                .next(self.tables, self.archetypes, self.query_state)
        }
    }


Review comment (suggestion):
not a fan of this name or `for_each_table` it reads to me as "for each table" rather than "for each row in table"

Code after:
        indices: Range<usize>,
    ) -> B
    where
        Func: FnMut(B, Q::Item<'w>) -> B,
    {
        let table = self.tables.get(archetype.table_id()).debug_checked_unwrap();
        Q::set_archetype(
            &mut self.cursor.fetch,
            &self.query_state.fetch_state,
            archetype,
            table,
        );
        F::set_archetype(
            &mut self.cursor.filter,
            &self.query_state.filter_state,
            archetype,
            table,
        );

        let entities = archetype.entities();
        for index in indices {
            // SAFETY: Caller assures `index` in range of the current archetype.
            let archetype_entity = entities.get_unchecked(index);
            // SAFETY: set_archetype was called prior.
            // Caller assures `index` in range of the current archetype.
            if !F::filter_fetch(
                &mut self.cursor.filter,
                archetype_entity.entity(),
                archetype_entity.table_row(),
            ) {
                continue;
            }

            // SAFETY: set_archetype was called prior, `index` is an archetype index in range of the current archetype
            // Caller assures `index` in range of the current archetype.
            let item = Q::fetch(
                &mut self.cursor.fetch,
                archetype_entity.entity(),
                archetype_entity.table_row(),
            );

            accum = func(accum, item);
        }
        accum
    }
}

impl<'w, 's, Q: WorldQueryData, F: WorldQueryFilter> Iterator for QueryIter<'w, 's, Q, F> {
    type Item = Q::Item<'w>;

    #[inline(always)]