Skip to content

Commit

Permalink
simple Debug impls for query iterators (#13476)
Browse files Browse the repository at this point in the history
# Objective

The current query iterators cannot be used in positions with a `Debug`
bound.
F.e. when they are packaged in `Result` in the error position, `expect`
cannot be called on them.
Required for `QueryManyIter::entities_all_unique` in #13477.

## Solution

Add simple `Debug` impls that print the query iterator names.

## Changelog

`QueryIter`, `QueryManyIter`, `QueryCombinationIter`, and
`QuerySortedIter` now implement `Debug`.
  • Loading branch information
Victoronz authored May 22, 2024
1 parent dda7a74 commit c4cedb1
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion crates/bevy_ecs/src/query/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ use crate::{
storage::{Table, TableRow, Tables},
world::unsafe_world_cell::UnsafeWorldCell,
};
use std::{borrow::Borrow, cmp::Ordering, iter::FusedIterator, mem::MaybeUninit, ops::Range};
use std::{
borrow::Borrow,
cmp::Ordering,
fmt::{self, Debug, Formatter},
iter::FusedIterator,
mem::MaybeUninit,
ops::Range,
};

use super::{QueryData, QueryFilter, ReadOnlyQueryData};

Expand Down Expand Up @@ -857,6 +864,12 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Iterator for QueryIter<'w, 's, D, F>
// This is correct as [`QueryIter`] always returns `None` once exhausted.
impl<'w, 's, D: QueryData, F: QueryFilter> FusedIterator for QueryIter<'w, 's, D, F> {}

impl<'w, 's, D: QueryData, F: QueryFilter> Debug for QueryIter<'w, 's, D, F> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("QueryIter").finish()
}
}

/// An [`Iterator`] over sorted query results of a [`Query`](crate::system::Query).
///
/// This struct is created by the [`QueryIter::sort`], [`QueryIter::sort_unstable`],
Expand Down Expand Up @@ -985,6 +998,14 @@ where
{
}

impl<'w, 's, D: QueryData, F: QueryFilter, I: Iterator<Item = Entity>> Debug
for QuerySortedIter<'w, 's, D, F, I>
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("QuerySortedIter").finish()
}
}

/// An [`Iterator`] over the query items generated from an iterator of [`Entity`]s.
///
/// Items are returned in the order of the provided iterator.
Expand Down Expand Up @@ -1131,6 +1152,15 @@ where
{
}

impl<'w, 's, D: QueryData, F: QueryFilter, I: Iterator> Debug for QueryManyIter<'w, 's, D, F, I>
where
I::Item: Borrow<Entity>,
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("QueryManyIter").finish()
}
}

/// An iterator over `K`-sized combinations of query items without repetition.
///
/// A combination is an arrangement of a collection of items where order does not matter.
Expand Down Expand Up @@ -1364,6 +1394,14 @@ impl<'w, 's, D: ReadOnlyQueryData, F: QueryFilter, const K: usize> FusedIterator
{
}

impl<'w, 's, D: QueryData, F: QueryFilter, const K: usize> Debug
for QueryCombinationIter<'w, 's, D, F, K>
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("QueryCombinationIter").finish()
}
}

struct QueryIterationCursor<'w, 's, D: QueryData, F: QueryFilter> {
storage_id_iter: std::slice::Iter<'s, StorageId>,
table_entities: &'w [Entity],
Expand Down

0 comments on commit c4cedb1

Please sign in to comment.