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

Code before:
            self.cursor
                .next(self.tables, self.archetypes, self.query_state)
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let max_size = self.cursor.max_remaining(self.tables, self.archetypes);
        let archetype_query = Q::IS_ARCHETYPAL && F::IS_ARCHETYPAL;
        let min_size = if archetype_query { max_size } else { 0 };
        (min_size, Some(max_size))
    }

    #[inline]
    fn fold<B, Func>(mut self, init: B, mut func: Func) -> B
    where
        Func: FnMut(B, Self::Item) -> B,
    {
        let mut accum = init;
        // Empty any remaining uniterated values from the current table/archetype
        while self.cursor.current_row != self.cursor.current_len {
            let Some(item) = self.next() else { break };
            accum = func(accum, item);
        }
        if Q::IS_DENSE && F::IS_DENSE {
            for table_id in self.cursor.table_id_iter.clone() {
                // SAFETY: Matched table IDs are guarenteed to still exist.
                let table = unsafe { self.tables.get(*table_id).debug_checked_unwrap() };
                accum =
                    // SAFETY: The fetched table matches the query
                    unsafe { self.fold_table(accum, &mut func, table, 0..table.entity_count()) };
            }
        } else {
            for archetype_id in self.cursor.archetype_id_iter.clone() {
                let archetype =
                    // SAFETY: Matched archetype IDs are guarenteed to still exist.
                    unsafe { self.archetypes.get(*archetype_id).debug_checked_unwrap() };
                accum =
                    // SAFETY: The fetched archetype and table matches the query
                    unsafe { self.fold_archetype(accum, &mut func, archetype, 0..archetype.len()) };
            }
        }
        accum
    }
}

// This is correct as [`QueryIter`] always returns `None` once exhausted.
impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> FusedIterator for QueryIter<'w, 's, Q, F> {}

/// An [`Iterator`] over the query items generated from an iterator of [`Entity`]s.
///
/// Items are returned in the order of the provided iterator.

Review comment (suggestion):
```suggestion
                // SAFETY: Matched table IDs are guaranteed to still exist.
```

Code after:
    }
}

impl<'w, 's, Q: WorldQueryData, F: WorldQueryFilter> 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)
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let max_size = self.cursor.max_remaining(self.tables, self.archetypes);
        let archetype_query = F::IS_ARCHETYPAL;
        let min_size = if archetype_query { max_size } else { 0 };
        (min_size, Some(max_size))
    }

    #[inline]
    fn fold<B, Func>(mut self, init: B, mut func: Func) -> B
    where
        Func: FnMut(B, Self::Item) -> B,
    {
        let mut accum = init;
        // Empty any remaining uniterated values from the current table/archetype
        while self.cursor.current_row != self.cursor.current_len {
            let Some(item) = self.next() else { break };
            accum = func(accum, item);
        }
        if Q::IS_DENSE && F::IS_DENSE {
            for table_id in self.cursor.table_id_iter.clone() {
                // SAFETY: Matched table IDs are guaranteed to still exist.
                let table = unsafe { self.tables.get(*table_id).debug_checked_unwrap() };
                accum =
                    // SAFETY: 
                    // - The fetched table matches both Q and F
                    // - The provided range is equivalent to [0, table.entity_count)
                    // - The if block ensures that Q::IS_DENSE and F::IS_DENSE are both true
                    unsafe { self.fold_over_table_range(accum, &mut func, table, 0..table.entity_count()) };
            }
        } else {
            for archetype_id in self.cursor.archetype_id_iter.clone() {
                let archetype =
                    // SAFETY: Matched archetype IDs are guaranteed to still exist.
                    unsafe { self.archetypes.get(*archetype_id).debug_checked_unwrap() };