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

Code before:
        );

        let entities = table.entities();
        for row in rows {
            // SAFETY: set_table was called prior.
            // `current_row` is a table row in range of the current table, because if it was not, then the if above would have been executed.
            let entity = entities.get_unchecked(row);
            let row = TableRow::new(row);
            if !F::filter_fetch(&mut self.cursor.filter, *entity, row) {
                continue;
            }

            // SAFETY: set_table was called prior.
            // `current_row` is a table row in range of the current table, because if it was not, then the if above would have been executed.
            let item = Q::fetch(&mut self.cursor.fetch, *entity, row);

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

    /// Executes the equivalent of [`Iterator::fold`] over a contiguous segment
    /// from an archetype.
    ///
    /// # Safety
    ///  - all `indicies` 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 fold_archetype<B, Func>(
        &mut self,
        mut accum: B,
        func: &mut Func,
        archetype: &'w Archetype,
        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,

Review comment (suggestion):
```suggestion
    ///  - all `indices` must be in `[0, archetype.len())`.
```

Code after:

    /// 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]
    #[cfg(all(not(target = "wasm32"), feature = "multi-threaded"))]
    pub(super) unsafe fn for_each_in_archetype_range<Func>(
        &mut self,
        func: &mut Func,
        archetype: &'w Archetype,
        rows: Range<usize>,
    ) where
        Func: FnMut(Q::Item<'w>),
    {
        // SAFETY: Caller assures that either Q::IS_DENSE or F::IS_DENSE are falsae, that archetype matches Q and F
        // and all indicies in rows are in range.
        unsafe {
            self.fold_over_archetype_range((), &mut |_, item| func(item), archetype, rows);
        }
    }

    /// Executes the equivalent of [`Iterator::fold`] over a contiguous segment
    /// from an table.
    ///
    /// # Safety
    ///  - all `rows` must be in `[0, table.entity_count)`.
    ///  - `table` must match Q and F
    ///  - Both `Q::IS_DENSE` and `F::IS_DENSE` must be true.
    #[inline]
    pub(super) unsafe fn fold_over_table_range<B, Func>(
        &mut self,
        mut accum: B,
        func: &mut Func,
        table: &'w Table,
        rows: Range<usize>,
    ) -> B
    where
        Func: FnMut(B, Q::Item<'w>) -> B,
    {
        Q::set_table(&mut self.cursor.fetch, &self.query_state.fetch_state, table);
        F::set_table(
            &mut self.cursor.filter,
            &self.query_state.filter_state,
            table,
        );

        let entities = table.entities();