bevy_ecs/query/iter.rs
1use super::{QueryData, QueryFilter, ReadOnlyQueryData};
2use crate::{
3 archetype::{Archetype, ArchetypeEntity, Archetypes},
4 bundle::Bundle,
5 change_detection::Tick,
6 entity::{Entities, Entity, EntityEquivalent, EntitySet, EntitySetIterator},
7 query::{
8 ArchetypeFilter, ArchetypeQueryData, ContiguousQueryData, DebugCheckedUnwrap,
9 IterQueryData, QueryEntityError, QueryState, SingleEntityQueryData, StorageId,
10 },
11 storage::{Table, TableRow, Tables},
12 world::{
13 unsafe_world_cell::UnsafeWorldCell, EntityMut, EntityMutExcept, EntityRef, EntityRefExcept,
14 FilteredEntityMut, FilteredEntityRef,
15 },
16};
17use alloc::vec::Vec;
18use core::{
19 cmp::Ordering,
20 fmt::{self, Debug, Formatter},
21 iter::FusedIterator,
22 mem::MaybeUninit,
23 ops::Range,
24};
25use nonmax::NonMaxU32;
26
27/// An [`Iterator`] over query results of a [`Query`](crate::system::Query).
28///
29/// This struct is created by the [`Query::iter`](crate::system::Query::iter) and
30/// [`Query::iter_mut`](crate::system::Query::iter_mut) methods.
31pub struct QueryIter<'w, 's, D: QueryData, F: QueryFilter> {
32 world: UnsafeWorldCell<'w>,
33 tables: &'w Tables,
34 archetypes: &'w Archetypes,
35 query_state: &'s QueryState<D, F>,
36 cursor: QueryIterationCursor<'w, 's, D, F>,
37}
38
39impl<'w, 's, D: QueryData, F: QueryFilter> QueryIter<'w, 's, D, F> {
40 /// # Safety
41 /// - `world` must have permission to access any of the components registered in `query_state`.
42 /// - `world` must be the same one used to initialize `query_state`.
43 pub(crate) unsafe fn new(
44 world: UnsafeWorldCell<'w>,
45 query_state: &'s QueryState<D, F>,
46 last_run: Tick,
47 this_run: Tick,
48 ) -> Self {
49 QueryIter {
50 world,
51 query_state,
52 // SAFETY: We only access table data that has been registered in `query_state`.
53 tables: unsafe { &world.storages().tables },
54 archetypes: world.archetypes(),
55 // SAFETY: The invariants are upheld by the caller.
56 cursor: unsafe { QueryIterationCursor::init(world, query_state, last_run, this_run) },
57 }
58 }
59
60 /// Creates a new separate iterator yielding the same remaining items of the current one.
61 /// Advancing the new iterator will not advance the original one, which will resume at the
62 /// point it was left at.
63 ///
64 /// Differently from [`remaining_mut`](QueryIter::remaining_mut) the new iterator does not
65 /// borrow from the original one. However it can only be called from an iterator over read only
66 /// items.
67 ///
68 /// # Example
69 ///
70 /// ```
71 /// # use bevy_ecs::prelude::*;
72 /// #
73 /// # #[derive(Component)]
74 /// # struct ComponentA;
75 ///
76 /// fn combinations(query: Query<&ComponentA>) {
77 /// let mut iter = query.iter();
78 /// while let Some(a) = iter.next() {
79 /// for b in iter.remaining() {
80 /// // Check every combination (a, b)
81 /// }
82 /// }
83 /// }
84 /// ```
85 pub fn remaining(&self) -> QueryIter<'w, 's, D, F>
86 where
87 D: ReadOnlyQueryData,
88 {
89 QueryIter {
90 world: self.world,
91 tables: self.tables,
92 archetypes: self.archetypes,
93 query_state: self.query_state,
94 cursor: self.cursor.clone(),
95 }
96 }
97
98 /// Creates a new separate iterator yielding the same remaining items of the current one.
99 /// Advancing the new iterator will not advance the original one, which will resume at the
100 /// point it was left at.
101 ///
102 /// This method can be called on iterators over mutable items. However the original iterator
103 /// will be borrowed while the new iterator exists and will thus not be usable in that timespan.
104 ///
105 /// # Example
106 ///
107 /// ```
108 /// # use bevy_ecs::prelude::*;
109 /// #
110 /// # #[derive(Component)]
111 /// # struct ComponentA;
112 ///
113 /// fn combinations(mut query: Query<&mut ComponentA>) {
114 /// let mut iter = query.iter_mut();
115 /// while let Some(a) = iter.next() {
116 /// for b in iter.remaining_mut() {
117 /// // Check every combination (a, b)
118 /// }
119 /// }
120 /// }
121 /// ```
122 pub fn remaining_mut(&mut self) -> QueryIter<'_, 's, D, F> {
123 QueryIter {
124 world: self.world,
125 tables: self.tables,
126 archetypes: self.archetypes,
127 query_state: self.query_state,
128 cursor: self.cursor.reborrow(),
129 }
130 }
131
132 /// Get the next result from the query.
133 ///
134 /// If the [`QueryData`] does not implement [`IterQueryData`],
135 /// then it is not sound to yield multiple items concurrently
136 /// and the resulting [`QueryIter`] will not implement [`Iterator`].
137 /// In that case, this method can be used to iterate over the items
138 /// while ensuring only one is alive at a time.
139 ///
140 /// Most queries do implement [`IterQueryData`],
141 /// and can use the ordinary [`Iterator::next`]
142 /// method or a `for` loop.
143 ///
144 /// # Example
145 ///
146 /// ```
147 /// # use bevy_ecs::prelude::*;
148 /// # #[derive(Component)]
149 /// # struct C;
150 /// fn system(mut query: Query<&mut C>) {
151 /// let mut iter = query.iter_mut();
152 /// while let Some(mut c) = iter.fetch_next() {
153 /// //
154 /// }
155 /// }
156 /// # bevy_ecs::system::assert_is_system(system);
157 /// ```
158 pub fn fetch_next(&mut self) -> Option<D::Item<'_, 's>> {
159 // SAFETY:
160 // - `tables` and `archetypes` belong to the same world that the cursor was initialized for.
161 // - `query_state` is the state that was passed to `QueryIterationCursor::init`.
162 // - `self` is mutably borrowed, so there are no other items alive for any entity.
163 unsafe {
164 self.cursor
165 .next(self.tables, self.archetypes, self.query_state)
166 .map(D::shrink)
167 }
168 }
169}
170
171impl<'w, 's, D: IterQueryData, F: QueryFilter> QueryIter<'w, 's, D, F> {
172 /// Executes the equivalent of [`Iterator::fold`] over a contiguous segment
173 /// from a storage.
174 ///
175 /// # Safety
176 /// - `range` must be in `[0, storage::entity_count)` or None.
177 #[inline]
178 pub(super) unsafe fn fold_over_storage_range<B, Func>(
179 &mut self,
180 mut accum: B,
181 func: &mut Func,
182 storage: StorageId,
183 range: Option<Range<u32>>,
184 ) -> B
185 where
186 Func: FnMut(B, D::Item<'w, 's>) -> B,
187 {
188 if self.cursor.is_dense {
189 // SAFETY: `self.cursor.is_dense` is true, so storage ids are guaranteed to be table ids.
190 let table_id = unsafe { storage.table_id };
191 // SAFETY: Matched table IDs are guaranteed to still exist.
192 let table = unsafe { self.tables.get(table_id).debug_checked_unwrap() };
193
194 let range = range.unwrap_or(0..table.entity_count());
195 accum =
196 // SAFETY:
197 // - The fetched table matches both D and F
198 // - caller ensures `range` is within `[0, table.entity_count)`
199 // - The if block ensures that the query iteration is dense
200 unsafe { self.fold_over_table_range(accum, func, table, range) };
201 } else {
202 // SAFETY: `self.cursor.is_dense` is false, so storage ids are guaranteed to be archetype ids.
203 let archetype_id = unsafe { storage.archetype_id };
204 // SAFETY: Matched archetype IDs are guaranteed to still exist.
205 let archetype = unsafe { self.archetypes.get(archetype_id).debug_checked_unwrap() };
206 // SAFETY: Matched table IDs are guaranteed to still exist.
207 let table = unsafe { self.tables.get(archetype.table_id()).debug_checked_unwrap() };
208
209 let range = range.unwrap_or(0..archetype.len());
210
211 // When an archetype and its table have equal entity counts, dense iteration can be safely used.
212 // this leverages cache locality to optimize performance.
213 if table.entity_count() == archetype.len() {
214 accum =
215 // SAFETY:
216 // - The fetched archetype matches both D and F
217 // - The provided archetype and its' table have the same length.
218 // - caller ensures `range` is within `[0, archetype.len)`
219 // - The if block ensures that the query iteration is not dense.
220 unsafe { self.fold_over_dense_archetype_range(accum, func, archetype,range) };
221 } else {
222 accum =
223 // SAFETY:
224 // - The fetched archetype matches both D and F
225 // - caller ensures `range` is within `[0, archetype.len)`
226 // - The if block ensures that the query iteration is not dense.
227 unsafe { self.fold_over_archetype_range(accum, func, archetype,range) };
228 }
229 }
230 accum
231 }
232
233 /// Executes the equivalent of [`Iterator::fold`] over a contiguous segment
234 /// from a table.
235 ///
236 /// # Safety
237 /// - all `rows` must be in `[0, table.entity_count)`.
238 /// - `table` must match D and F
239 /// - The query iteration must be dense (i.e. `self.query_state.is_dense` must be true).
240 #[inline]
241 pub(super) unsafe fn fold_over_table_range<B, Func>(
242 &mut self,
243 mut accum: B,
244 func: &mut Func,
245 table: &'w Table,
246 rows: Range<u32>,
247 ) -> B
248 where
249 Func: FnMut(B, D::Item<'w, 's>) -> B,
250 {
251 if table.is_empty() {
252 return accum;
253 }
254
255 D::set_table(&mut self.cursor.fetch, &self.query_state.fetch_state, table);
256 F::set_table(
257 &mut self.cursor.filter,
258 &self.query_state.filter_state,
259 table,
260 );
261
262 let entities = table.entities();
263 for row in rows {
264 // SAFETY: Caller assures `row` in range of the current archetype.
265 let entity = unsafe { entities.get_unchecked(row as usize) };
266 // SAFETY: This is from an exclusive range, so it can't be max.
267 let row = unsafe { TableRow::new(NonMaxU32::new_unchecked(row)) };
268
269 // SAFETY: set_table was called prior.
270 // Caller assures `row` in range of the current archetype.
271 let fetched = unsafe {
272 !F::filter_fetch(
273 &self.query_state.filter_state,
274 &mut self.cursor.filter,
275 *entity,
276 row,
277 )
278 };
279 if fetched {
280 continue;
281 }
282
283 // SAFETY:
284 // - set_table was called prior.
285 // - Caller assures `row` in range of the current archetype.
286 // - Each row is unique, so each entity is only alive once
287 // - `D: IterQueryData`
288 if let Some(item) = D::fetch(
289 &self.query_state.fetch_state,
290 &mut self.cursor.fetch,
291 *entity,
292 row,
293 ) {
294 accum = func(accum, item);
295 }
296 }
297 accum
298 }
299
300 /// Executes the equivalent of [`Iterator::fold`] over a contiguous segment
301 /// from an archetype.
302 ///
303 /// # Safety
304 /// - all `indices` must be in `[0, archetype.len())`.
305 /// - `archetype` must match D and F
306 /// - The query iteration must not be dense (i.e. `self.query_state.is_dense` must be false).
307 #[inline]
308 pub(super) unsafe fn fold_over_archetype_range<B, Func>(
309 &mut self,
310 mut accum: B,
311 func: &mut Func,
312 archetype: &'w Archetype,
313 indices: Range<u32>,
314 ) -> B
315 where
316 Func: FnMut(B, D::Item<'w, 's>) -> B,
317 {
318 if archetype.is_empty() {
319 return accum;
320 }
321 let table = self.tables.get(archetype.table_id()).debug_checked_unwrap();
322 D::set_archetype(
323 &mut self.cursor.fetch,
324 &self.query_state.fetch_state,
325 archetype,
326 table,
327 );
328 F::set_archetype(
329 &mut self.cursor.filter,
330 &self.query_state.filter_state,
331 archetype,
332 table,
333 );
334
335 let entities = archetype.entities();
336 for index in indices {
337 // SAFETY: Caller assures `index` in range of the current archetype.
338 let archetype_entity = unsafe { entities.get_unchecked(index as usize) };
339
340 // SAFETY: set_archetype was called prior.
341 // Caller assures `index` in range of the current archetype.
342 let fetched = unsafe {
343 !F::filter_fetch(
344 &self.query_state.filter_state,
345 &mut self.cursor.filter,
346 archetype_entity.id(),
347 archetype_entity.table_row(),
348 )
349 };
350 if fetched {
351 continue;
352 }
353
354 // SAFETY:
355 // - set_archetype was called prior, `index` is an archetype index in range of the current archetype
356 // - Caller assures `index` in range of the current archetype.
357 // - Each row is unique, so each entity is only alive once
358 // - `D: IterQueryData`
359 if let Some(item) = unsafe {
360 D::fetch(
361 &self.query_state.fetch_state,
362 &mut self.cursor.fetch,
363 archetype_entity.id(),
364 archetype_entity.table_row(),
365 )
366 } {
367 accum = func(accum, item);
368 }
369 }
370 accum
371 }
372
373 /// Executes the equivalent of [`Iterator::fold`] over a contiguous segment
374 /// from an archetype which has the same entity count as its table.
375 ///
376 /// # Safety
377 /// - all `indices` must be in `[0, archetype.len())`.
378 /// - `archetype` must match D and F
379 /// - `archetype` must have the same length as its table.
380 /// - The query iteration must not be dense (i.e. `self.query_state.is_dense` must be false).
381 #[inline]
382 pub(super) unsafe fn fold_over_dense_archetype_range<B, Func>(
383 &mut self,
384 mut accum: B,
385 func: &mut Func,
386 archetype: &'w Archetype,
387 rows: Range<u32>,
388 ) -> B
389 where
390 Func: FnMut(B, D::Item<'w, 's>) -> B,
391 {
392 if archetype.is_empty() {
393 return accum;
394 }
395 let table = self.tables.get(archetype.table_id()).debug_checked_unwrap();
396 debug_assert!(
397 archetype.len() == table.entity_count(),
398 "archetype and its table must have the same length. "
399 );
400
401 D::set_archetype(
402 &mut self.cursor.fetch,
403 &self.query_state.fetch_state,
404 archetype,
405 table,
406 );
407 F::set_archetype(
408 &mut self.cursor.filter,
409 &self.query_state.filter_state,
410 archetype,
411 table,
412 );
413 let entities = table.entities();
414 for row in rows {
415 // SAFETY: Caller assures `row` in range of the current archetype.
416 let entity = unsafe { *entities.get_unchecked(row as usize) };
417 // SAFETY: This is from an exclusive range, so it can't be max.
418 let row = unsafe { TableRow::new(NonMaxU32::new_unchecked(row)) };
419
420 // SAFETY: set_table was called prior.
421 // Caller assures `row` in range of the current archetype.
422 let filter_matched = unsafe {
423 F::filter_fetch(
424 &self.query_state.filter_state,
425 &mut self.cursor.filter,
426 entity,
427 row,
428 )
429 };
430 if !filter_matched {
431 continue;
432 }
433
434 // SAFETY:
435 // - set_table was called prior.
436 // - Caller assures `row` in range of the current archetype.
437 // - Each row is unique, so each entity is only alive once
438 // - `D: IterQueryData`
439 if let Some(item) = D::fetch(
440 &self.query_state.fetch_state,
441 &mut self.cursor.fetch,
442 entity,
443 row,
444 ) {
445 accum = func(accum, item);
446 }
447 }
448 accum
449 }
450}
451
452impl<'w, 's, D: QueryData, F: QueryFilter> QueryIter<'w, 's, D, F> {
453 /// Sorts all query items into a new iterator, using the query lens as a key.
454 ///
455 /// This sort is stable (i.e., does not reorder equal elements).
456 ///
457 /// This uses [`slice::sort`] internally.
458 ///
459 /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
460 /// This includes the allowed parameter type changes listed under [allowed transmutes].
461 /// However, the lens uses the filter of the original query when present.
462 ///
463 /// The lens needs to be a [`SingleEntityQueryData`] because the current implementation
464 /// of query transmutes does not support nested queries.
465 /// This restriction may be lifted in the future.
466 ///
467 /// The sort is not cached across system runs.
468 ///
469 /// If the [`QueryData`] does not implement [`IterQueryData`],
470 /// then it is not sound to yield multiple items concurrently
471 /// and the resulting [`QuerySortedIter`] will not implement [`Iterator`].
472 /// To iterate over the items in that case,
473 /// use the [`QuerySortedIter::fetch_next()`](crate::query::QuerySortedIter::fetch_next) method,
474 /// which ensures only one item is alive at a time.
475 ///
476 /// [allowed transmutes]: crate::system::Query#allowed-transmutes
477 ///
478 /// # Panics
479 ///
480 /// This will panic if `next` has been called on `QueryIter` before, unless the underlying `Query` is empty.
481 ///
482 /// # Examples
483 /// ```rust
484 /// # use bevy_ecs::prelude::*;
485 /// # use std::{ops::{Deref, DerefMut}, iter::Sum};
486 /// #
487 /// # #[derive(Component)]
488 /// # struct PartMarker;
489 /// #
490 /// # #[derive(Component, PartialEq, Eq, PartialOrd, Ord)]
491 /// # struct PartIndex(usize);
492 /// #
493 /// # #[derive(Component, Clone, Copy)]
494 /// # struct PartValue(f32);
495 /// #
496 /// # impl Deref for PartValue {
497 /// # type Target = f32;
498 /// #
499 /// # fn deref(&self) -> &Self::Target {
500 /// # &self.0
501 /// # }
502 /// # }
503 /// #
504 /// # #[derive(Component)]
505 /// # struct ParentValue(f32);
506 /// #
507 /// # impl Deref for ParentValue {
508 /// # type Target = f32;
509 /// #
510 /// # fn deref(&self) -> &Self::Target {
511 /// # &self.0
512 /// # }
513 /// # }
514 /// #
515 /// # impl DerefMut for ParentValue {
516 /// # fn deref_mut(&mut self) -> &mut Self::Target {
517 /// # &mut self.0
518 /// # }
519 /// # }
520 /// #
521 /// # #[derive(Component, Debug, PartialEq, Eq, PartialOrd, Ord)]
522 /// # struct Length(usize);
523 /// #
524 /// # #[derive(Component, Debug, PartialEq, Eq, PartialOrd, Ord)]
525 /// # struct Width(usize);
526 /// #
527 /// # #[derive(Component, Debug, PartialEq, Eq, PartialOrd, Ord)]
528 /// # struct Height(usize);
529 /// #
530 /// # #[derive(Component, PartialEq, Eq, PartialOrd, Ord)]
531 /// # struct ParentEntity(Entity);
532 /// #
533 /// # #[derive(Component, Clone, Copy)]
534 /// # struct ChildPartCount(usize);
535 /// #
536 /// # impl Deref for ChildPartCount {
537 /// # type Target = usize;
538 /// #
539 /// # fn deref(&self) -> &Self::Target {
540 /// # &self.0
541 /// # }
542 /// # }
543 /// # let mut world = World::new();
544 /// // We can ensure that a query always returns in the same order.
545 /// fn system_1(query: Query<(Entity, &PartIndex)>) {
546 /// let parts: Vec<(Entity, &PartIndex)> = query.iter().sort::<&PartIndex>().collect();
547 /// }
548 ///
549 /// // We can freely rearrange query components in the key.
550 /// fn system_2(query: Query<(&Length, &Width, &Height), With<PartMarker>>) {
551 /// for (length, width, height) in query.iter().sort::<(&Height, &Length, &Width)>() {
552 /// println!("height: {height:?}, width: {width:?}, length: {length:?}")
553 /// }
554 /// }
555 ///
556 /// // We can sort by Entity without including it in the original Query.
557 /// // Here, we match iteration orders between query iterators.
558 /// fn system_3(
559 /// part_query: Query<(&PartValue, &ParentEntity)>,
560 /// mut parent_query: Query<(&ChildPartCount, &mut ParentValue)>,
561 /// ) {
562 /// let part_values = &mut part_query
563 /// .into_iter()
564 /// .sort::<&ParentEntity>()
565 /// .map(|(&value, parent_entity)| *value);
566 ///
567 /// for (&child_count, mut parent_value) in parent_query.iter_mut().sort::<Entity>() {
568 /// **parent_value = part_values.take(*child_count).sum();
569 /// }
570 /// }
571 /// #
572 /// # let mut schedule = Schedule::default();
573 /// # schedule.add_systems((system_1, system_2, system_3));
574 /// # schedule.run(&mut world);
575 /// ```
576 pub fn sort<L: ReadOnlyQueryData + SingleEntityQueryData + 'w>(
577 self,
578 ) -> QuerySortedIter<
579 'w,
580 's,
581 D,
582 F,
583 impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
584 >
585 where
586 for<'lw, 'ls> L::Item<'lw, 'ls>: Ord,
587 {
588 self.sort_impl::<L>(|keyed_query| keyed_query.sort())
589 }
590
591 /// Sorts all query items into a new iterator, using the query lens as a key.
592 ///
593 /// This sort is unstable (i.e., may reorder equal elements).
594 ///
595 /// This uses [`slice::sort_unstable`] internally.
596 ///
597 /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
598 /// This includes the allowed parameter type changes listed under [allowed transmutes]..
599 /// However, the lens uses the filter of the original query when present.
600 ///
601 /// The lens needs to be a [`SingleEntityQueryData`] because the current implementation
602 /// of query transmutes does not support nested queries.
603 /// This restriction may be lifted in the future.
604 ///
605 /// The sort is not cached across system runs.
606 ///
607 /// If the [`QueryData`] does not implement [`IterQueryData`],
608 /// then it is not sound to yield multiple items concurrently
609 /// and the resulting [`QuerySortedIter`] will not implement [`Iterator`].
610 /// To iterate over the items in that case,
611 /// use the [`QuerySortedIter::fetch_next()`](crate::query::QuerySortedIter::fetch_next) method,
612 /// which ensures only one item is alive at a time.
613 ///
614 /// [allowed transmutes]: crate::system::Query#allowed-transmutes
615 ///
616 /// # Panics
617 ///
618 /// This will panic if `next` has been called on `QueryIter` before, unless the underlying `Query` is empty.
619 ///
620 /// # Example
621 /// ```
622 /// # use bevy_ecs::prelude::*;
623 /// #
624 /// # let mut world = World::new();
625 /// #
626 /// # #[derive(Component)]
627 /// # struct PartMarker;
628 /// #
629 /// #[derive(Component, PartialEq, Eq, PartialOrd, Ord)]
630 /// enum Flying {
631 /// Enabled,
632 /// Disabled
633 /// };
634 ///
635 /// // We perform an unstable sort by a Component with few values.
636 /// fn system_1(query: Query<&Flying, With<PartMarker>>) {
637 /// let part_values: Vec<&Flying> = query.iter().sort_unstable::<&Flying>().collect();
638 /// }
639 /// #
640 /// # let mut schedule = Schedule::default();
641 /// # schedule.add_systems((system_1));
642 /// # schedule.run(&mut world);
643 /// ```
644 pub fn sort_unstable<L: ReadOnlyQueryData + SingleEntityQueryData + 'w>(
645 self,
646 ) -> QuerySortedIter<
647 'w,
648 's,
649 D,
650 F,
651 impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
652 >
653 where
654 for<'lw, 'ls> L::Item<'lw, 'ls>: Ord,
655 {
656 self.sort_impl::<L>(|keyed_query| keyed_query.sort_unstable())
657 }
658
659 /// Sorts all query items into a new iterator with a comparator function over the query lens.
660 ///
661 /// This sort is stable (i.e., does not reorder equal elements).
662 ///
663 /// This uses [`slice::sort_by`] internally.
664 ///
665 /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
666 /// This includes the allowed parameter type changes listed under [allowed transmutes].
667 /// However, the lens uses the filter of the original query when present.
668 ///
669 /// The lens needs to be a [`SingleEntityQueryData`] because the current implementation
670 /// of query transmutes does not support nested queries.
671 /// This restriction may be lifted in the future.
672 ///
673 /// The sort is not cached across system runs.
674 ///
675 /// If the [`QueryData`] does not implement [`IterQueryData`],
676 /// then it is not sound to yield multiple items concurrently
677 /// and the resulting [`QuerySortedIter`] will not implement [`Iterator`].
678 /// To iterate over the items in that case,
679 /// use the [`QuerySortedIter::fetch_next()`](crate::query::QuerySortedIter::fetch_next) method,
680 /// which ensures only one item is alive at a time.
681 ///
682 /// [allowed transmutes]: crate::system::Query#allowed-transmutes
683 ///
684 /// # Panics
685 ///
686 /// This will panic if `next` has been called on `QueryIter` before, unless the underlying `Query` is empty.
687 ///
688 /// # Example
689 /// ```
690 /// # use bevy_ecs::prelude::*;
691 /// # use std::ops::Deref;
692 /// #
693 /// # impl Deref for PartValue {
694 /// # type Target = f32;
695 /// #
696 /// # fn deref(&self) -> &Self::Target {
697 /// # &self.0
698 /// # }
699 /// # }
700 /// #
701 /// # let mut world = World::new();
702 /// #
703 /// #[derive(Component)]
704 /// struct PartValue(f32);
705 ///
706 /// // We can use a cmp function on components do not implement Ord.
707 /// fn system_1(query: Query<&PartValue>) {
708 /// // Sort part values according to `f32::total_comp`.
709 /// let part_values: Vec<&PartValue> = query
710 /// .iter()
711 /// .sort_by::<&PartValue>(|value_1, value_2| value_1.total_cmp(*value_2))
712 /// .collect();
713 /// }
714 /// #
715 /// # let mut schedule = Schedule::default();
716 /// # schedule.add_systems((system_1));
717 /// # schedule.run(&mut world);
718 /// ```
719 pub fn sort_by<L: ReadOnlyQueryData + SingleEntityQueryData + 'w>(
720 self,
721 mut compare: impl FnMut(&L::Item<'_, '_>, &L::Item<'_, '_>) -> Ordering,
722 ) -> QuerySortedIter<
723 'w,
724 's,
725 D,
726 F,
727 impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
728 > {
729 self.sort_impl::<L>(move |keyed_query| {
730 keyed_query.sort_by(|(key_1, _), (key_2, _)| compare(key_1, key_2));
731 })
732 }
733
734 /// Sorts all query items into a new iterator with a comparator function over the query lens.
735 ///
736 /// This sort is unstable (i.e., may reorder equal elements).
737 ///
738 /// This uses [`slice::sort_unstable_by`] internally.
739 ///
740 /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
741 /// This includes the allowed parameter type changes listed under [allowed transmutes].
742 /// However, the lens uses the filter of the original query when present.
743 ///
744 /// The lens needs to be a [`SingleEntityQueryData`] because the current implementation
745 /// of query transmutes does not support nested queries.
746 /// This restriction may be lifted in the future.
747 ///
748 /// The sort is not cached across system runs.
749 ///
750 /// If the [`QueryData`] does not implement [`IterQueryData`],
751 /// then it is not sound to yield multiple items concurrently
752 /// and the resulting [`QuerySortedIter`] will not implement [`Iterator`].
753 /// To iterate over the items in that case,
754 /// use the [`QuerySortedIter::fetch_next()`](crate::query::QuerySortedIter::fetch_next) method,
755 /// which ensures only one item is alive at a time.
756 ///
757 /// [allowed transmutes]: crate::system::Query#allowed-transmutes
758 ///
759 /// # Panics
760 ///
761 /// This will panic if `next` has been called on `QueryIter` before, unless the underlying `Query` is empty.
762 pub fn sort_unstable_by<L: ReadOnlyQueryData + SingleEntityQueryData + 'w>(
763 self,
764 mut compare: impl FnMut(&L::Item<'_, '_>, &L::Item<'_, '_>) -> Ordering,
765 ) -> QuerySortedIter<
766 'w,
767 's,
768 D,
769 F,
770 impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
771 > {
772 self.sort_impl::<L>(move |keyed_query| {
773 keyed_query.sort_unstable_by(|(key_1, _), (key_2, _)| compare(key_1, key_2));
774 })
775 }
776
777 /// Sorts all query items into a new iterator with a key extraction function over the query lens.
778 ///
779 /// This sort is stable (i.e., does not reorder equal elements).
780 ///
781 /// This uses [`slice::sort_by_key`] internally.
782 ///
783 /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
784 /// This includes the allowed parameter type changes listed under [allowed transmutes].
785 /// However, the lens uses the filter of the original query when present.
786 ///
787 /// The lens needs to be a [`SingleEntityQueryData`] because the current implementation
788 /// of query transmutes does not support nested queries.
789 /// This restriction may be lifted in the future.
790 ///
791 /// The sort is not cached across system runs.
792 ///
793 /// If the [`QueryData`] does not implement [`IterQueryData`],
794 /// then it is not sound to yield multiple items concurrently
795 /// and the resulting [`QuerySortedIter`] will not implement [`Iterator`].
796 /// To iterate over the items in that case,
797 /// use the [`QuerySortedIter::fetch_next()`](crate::query::QuerySortedIter::fetch_next) method,
798 /// which ensures only one item is alive at a time.
799 ///
800 /// [allowed transmutes]: crate::system::Query#allowed-transmutes
801 ///
802 /// # Panics
803 ///
804 /// This will panic if `next` has been called on `QueryIter` before, unless the underlying `Query` is empty.
805 ///
806 /// # Example
807 /// ```
808 /// # use bevy_ecs::prelude::*;
809 /// # use std::ops::Deref;
810 /// #
811 /// # #[derive(Component)]
812 /// # struct PartMarker;
813 /// #
814 /// # impl Deref for PartValue {
815 /// # type Target = f32;
816 /// #
817 /// # fn deref(&self) -> &Self::Target {
818 /// # &self.0
819 /// # }
820 /// # }
821 /// #
822 /// # let mut world = World::new();
823 /// #
824 /// #[derive(Component)]
825 /// struct AvailableMarker;
826 ///
827 /// #[derive(Component, PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
828 /// enum Rarity {
829 /// Common,
830 /// Rare,
831 /// Epic,
832 /// Legendary
833 /// };
834 ///
835 /// #[derive(Component)]
836 /// struct PartValue(f32);
837 ///
838 /// // We can sort with the internals of components that do not implement Ord.
839 /// fn system_1(query: Query<(Entity, &PartValue)>) {
840 /// // Sort by the sines of the part values.
841 /// let parts: Vec<(Entity, &PartValue)> = query
842 /// .iter()
843 /// .sort_by_key::<&PartValue, _>(|value| value.sin() as usize)
844 /// .collect();
845 /// }
846 ///
847 /// // We can define our own custom comparison functions over an EntityRef.
848 /// fn system_2(query: Query<EntityRef, With<PartMarker>>) {
849 /// // Sort by whether parts are available and their rarity.
850 /// // We want the available legendaries to come first, so we reverse the iterator.
851 /// let parts: Vec<EntityRef> = query.iter()
852 /// .sort_by_key::<EntityRef, _>(|entity_ref| {
853 /// (
854 /// entity_ref.contains::<AvailableMarker>(),
855 /// entity_ref.get::<Rarity>().copied()
856 /// )
857 /// })
858 /// .rev()
859 /// .collect();
860 /// }
861 /// # let mut schedule = Schedule::default();
862 /// # schedule.add_systems((system_1, system_2));
863 /// # schedule.run(&mut world);
864 /// ```
865 pub fn sort_by_key<L: ReadOnlyQueryData + SingleEntityQueryData + 'w, K>(
866 self,
867 mut f: impl FnMut(&L::Item<'_, '_>) -> K,
868 ) -> QuerySortedIter<
869 'w,
870 's,
871 D,
872 F,
873 impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
874 >
875 where
876 K: Ord,
877 {
878 self.sort_impl::<L>(move |keyed_query| keyed_query.sort_by_key(|(lens, _)| f(lens)))
879 }
880
881 /// Sorts all query items into a new iterator with a key extraction function over the query lens.
882 ///
883 /// This sort is unstable (i.e., may reorder equal elements).
884 ///
885 /// This uses [`slice::sort_unstable_by_key`] internally.
886 ///
887 /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
888 /// This includes the allowed parameter type changes listed under [allowed transmutes].
889 /// However, the lens uses the filter of the original query when present.
890 ///
891 /// The lens needs to be a [`SingleEntityQueryData`] because the current implementation
892 /// of query transmutes does not support nested queries.
893 /// This restriction may be lifted in the future.
894 ///
895 /// The sort is not cached across system runs.
896 ///
897 /// If the [`QueryData`] does not implement [`IterQueryData`],
898 /// then it is not sound to yield multiple items concurrently
899 /// and the resulting [`QuerySortedIter`] will not implement [`Iterator`].
900 /// To iterate over the items in that case,
901 /// use the [`QuerySortedIter::fetch_next()`](crate::query::QuerySortedIter::fetch_next) method,
902 /// which ensures only one item is alive at a time.
903 ///
904 /// [allowed transmutes]: crate::system::Query#allowed-transmutes
905 ///
906 /// # Panics
907 ///
908 /// This will panic if `next` has been called on `QueryIter` before, unless the underlying `Query` is empty.
909 pub fn sort_unstable_by_key<L: ReadOnlyQueryData + SingleEntityQueryData + 'w, K>(
910 self,
911 mut f: impl FnMut(&L::Item<'_, '_>) -> K,
912 ) -> QuerySortedIter<
913 'w,
914 's,
915 D,
916 F,
917 impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
918 >
919 where
920 K: Ord,
921 {
922 self.sort_impl::<L>(move |keyed_query| {
923 keyed_query.sort_unstable_by_key(|(lens, _)| f(lens));
924 })
925 }
926
927 /// Sort all query items into a new iterator with a key extraction function over the query lens.
928 ///
929 /// This sort is stable (i.e., does not reorder equal elements).
930 ///
931 /// This uses [`slice::sort_by_cached_key`] internally.
932 ///
933 /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
934 /// This includes the allowed parameter type changes listed under [allowed transmutes].
935 /// However, the lens uses the filter of the original query when present.
936 ///
937 /// The lens needs to be a [`SingleEntityQueryData`] because the current implementation
938 /// of query transmutes does not support nested queries.
939 /// This restriction may be lifted in the future.
940 ///
941 /// The sort is not cached across system runs.
942 ///
943 /// If the [`QueryData`] does not implement [`IterQueryData`],
944 /// then it is not sound to yield multiple items concurrently
945 /// and the resulting [`QuerySortedIter`] will not implement [`Iterator`].
946 /// To iterate over the items in that case,
947 /// use the [`QuerySortedIter::fetch_next()`](crate::query::QuerySortedIter::fetch_next) method,
948 /// which ensures only one item is alive at a time.
949 ///
950 /// [allowed transmutes]: crate::system::Query#allowed-transmutes
951 ///
952 /// # Panics
953 ///
954 /// This will panic if `next` has been called on `QueryIter` before, unless the underlying `Query` is empty.
955 pub fn sort_by_cached_key<L: ReadOnlyQueryData + SingleEntityQueryData + 'w, K>(
956 self,
957 mut f: impl FnMut(&L::Item<'_, '_>) -> K,
958 ) -> QuerySortedIter<
959 'w,
960 's,
961 D,
962 F,
963 impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
964 >
965 where
966 K: Ord,
967 {
968 self.sort_impl::<L>(move |keyed_query| keyed_query.sort_by_cached_key(|(lens, _)| f(lens)))
969 }
970
971 /// Shared implementation for the various `sort` methods.
972 /// This uses the lens to collect the items for sorting, but delegates the actual sorting to the provided closure.
973 ///
974 /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
975 /// This includes the allowed parameter type changes listed under [allowed transmutes].
976 /// However, the lens uses the filter of the original query when present.
977 ///
978 /// The lens needs to be a [`SingleEntityQueryData`] because the current implementation
979 /// of query transmutes does not support nested queries.
980 /// This restriction may be lifted in the future.
981 ///
982 /// The sort is not cached across system runs.
983 ///
984 /// If the [`QueryData`] does not implement [`IterQueryData`],
985 /// then it is not sound to yield multiple items concurrently
986 /// and the resulting [`QuerySortedIter`] will not implement [`Iterator`].
987 /// To iterate over the items in that case,
988 /// use the [`QuerySortedIter::fetch_next()`](crate::query::QuerySortedIter::fetch_next) method,
989 /// which ensures only one item is alive at a time.
990 ///
991 /// [allowed transmutes]: crate::system::Query#allowed-transmutes
992 ///
993 /// # Panics
994 ///
995 /// This will panic if `next` has been called on `QueryIter` before, unless the underlying `Query` is empty.
996 fn sort_impl<L: ReadOnlyQueryData + SingleEntityQueryData + 'w>(
997 self,
998 f: impl FnOnce(&mut Vec<(L::Item<'_, '_>, NeutralOrd<Entity>)>),
999 ) -> QuerySortedIter<
1000 'w,
1001 's,
1002 D,
1003 F,
1004 impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
1005 > {
1006 // On the first successful iteration of `QueryIterationCursor`, `archetype_entities` or `table_entities`
1007 // will be set to a non-zero value. The correctness of this method relies on this.
1008 // I.e. this sort method will execute if and only if `next` on `QueryIterationCursor` of a
1009 // non-empty `QueryIter` has not yet been called. When empty, this sort method will not panic.
1010 if !self.cursor.archetype_entities.is_empty() || !self.cursor.table_entities.is_empty() {
1011 panic!("it is not valid to call sort() after next()")
1012 }
1013
1014 let world = self.world;
1015
1016 let query_lens_state = self.query_state.transmute_filtered::<(L, Entity), F>(world);
1017
1018 // SAFETY:
1019 // `self.world` has permission to access the required components.
1020 // The original query iter has not been iterated on, so no items are aliased from it.
1021 // `QueryIter::new` ensures `world` is the same one used to initialize `query_state`.
1022 let query_lens = unsafe { query_lens_state.query_unchecked_manual(world) }.into_iter();
1023 let mut keyed_query: Vec<_> = query_lens
1024 .map(|(key, entity)| (key, NeutralOrd(entity)))
1025 .collect();
1026 f(&mut keyed_query);
1027 let entity_iter = keyed_query
1028 .into_iter()
1029 .map(|(.., entity)| entity.0)
1030 .collect::<Vec<_>>()
1031 .into_iter();
1032 // SAFETY:
1033 // `self.world` has permission to access the required components.
1034 // Each lens query item is dropped before the respective actual query item is accessed.
1035 unsafe {
1036 QuerySortedIter::new(
1037 world,
1038 self.query_state,
1039 entity_iter,
1040 world.last_change_tick(),
1041 world.change_tick(),
1042 )
1043 }
1044 }
1045}
1046
1047impl<'w, 's, D: IterQueryData, F: QueryFilter> Iterator for QueryIter<'w, 's, D, F> {
1048 type Item = D::Item<'w, 's>;
1049
1050 #[inline(always)]
1051 fn next(&mut self) -> Option<Self::Item> {
1052 // SAFETY:
1053 // - `tables` and `archetypes` belong to the same world that the cursor was initialized for.
1054 // - `query_state` is the state that was passed to `QueryIterationCursor::init`.
1055 // - `D: IterQueryData`
1056 unsafe {
1057 self.cursor
1058 .next(self.tables, self.archetypes, self.query_state)
1059 }
1060 }
1061
1062 fn size_hint(&self) -> (usize, Option<usize>) {
1063 let max_size = self.cursor.max_remaining(self.tables, self.archetypes);
1064 let archetype_query = D::IS_ARCHETYPAL && F::IS_ARCHETYPAL;
1065 let min_size = if archetype_query { max_size } else { 0 };
1066 (min_size as usize, Some(max_size as usize))
1067 }
1068
1069 #[inline]
1070 fn fold<B, Func>(mut self, init: B, mut func: Func) -> B
1071 where
1072 Func: FnMut(B, Self::Item) -> B,
1073 {
1074 let mut accum = init;
1075 // Empty any remaining uniterated values from the current table/archetype
1076 while self.cursor.current_row != self.cursor.current_len {
1077 let Some(item) = self.next() else { break };
1078 accum = func(accum, item);
1079 }
1080
1081 for id in self.cursor.storage_id_iter.clone().copied() {
1082 // SAFETY:
1083 // - The range(None) is equivalent to [0, storage.entity_count)
1084 accum = unsafe { self.fold_over_storage_range(accum, &mut func, id, None) };
1085 }
1086 accum
1087 }
1088}
1089
1090// This is correct as [`QueryIter`] always returns `None` once exhausted.
1091impl<'w, 's, D: IterQueryData, F: QueryFilter> FusedIterator for QueryIter<'w, 's, D, F> {}
1092
1093// SAFETY: [`QueryIter`] is guaranteed to return every matching entity once and only once.
1094unsafe impl<'w, 's, F: QueryFilter> EntitySetIterator for QueryIter<'w, 's, Entity, F> {}
1095
1096// SAFETY: [`QueryIter`] is guaranteed to return every matching entity once and only once.
1097unsafe impl<'w, 's, F: QueryFilter> EntitySetIterator for QueryIter<'w, 's, EntityRef<'_>, F> {}
1098
1099// SAFETY: [`QueryIter`] is guaranteed to return every matching entity once and only once.
1100unsafe impl<'w, 's, F: QueryFilter> EntitySetIterator for QueryIter<'w, 's, EntityMut<'_>, F> {}
1101
1102// SAFETY: [`QueryIter`] is guaranteed to return every matching entity once and only once.
1103unsafe impl<'w, 's, F: QueryFilter> EntitySetIterator
1104 for QueryIter<'w, 's, FilteredEntityRef<'_, '_>, F>
1105{
1106}
1107
1108// SAFETY: [`QueryIter`] is guaranteed to return every matching entity once and only once.
1109unsafe impl<'w, 's, F: QueryFilter> EntitySetIterator
1110 for QueryIter<'w, 's, FilteredEntityMut<'_, '_>, F>
1111{
1112}
1113
1114// SAFETY: [`QueryIter`] is guaranteed to return every matching entity once and only once.
1115unsafe impl<'w, 's, F: QueryFilter, B: Bundle> EntitySetIterator
1116 for QueryIter<'w, 's, EntityRefExcept<'_, '_, B>, F>
1117{
1118}
1119
1120// SAFETY: [`QueryIter`] is guaranteed to return every matching entity once and only once.
1121unsafe impl<'w, 's, F: QueryFilter, B: Bundle> EntitySetIterator
1122 for QueryIter<'w, 's, EntityMutExcept<'_, '_, B>, F>
1123{
1124}
1125
1126impl<'w, 's, D: QueryData, F: QueryFilter> Debug for QueryIter<'w, 's, D, F> {
1127 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1128 f.debug_struct("QueryIter").finish()
1129 }
1130}
1131
1132impl<'w, 's, D: ReadOnlyQueryData, F: QueryFilter> Clone for QueryIter<'w, 's, D, F> {
1133 fn clone(&self) -> Self {
1134 self.remaining()
1135 }
1136}
1137
1138/// Iterator for contiguous chunks of memory
1139pub struct QueryContiguousIter<'w, 's, D: ContiguousQueryData, F: ArchetypeFilter> {
1140 tables: &'w Tables,
1141 storage_id_iter: core::slice::Iter<'s, StorageId>,
1142 query_state: &'s QueryState<D, F>,
1143 fetch: D::Fetch<'w>,
1144 // NOTE: no need for F::Fetch because it always returns true
1145}
1146
1147impl<'w, 's, D: ContiguousQueryData, F: ArchetypeFilter> QueryContiguousIter<'w, 's, D, F> {
1148 /// # Safety
1149 /// - `world` must have permission to access any of the components registered in `query_state`.
1150 /// - `world` must be the same one used to initialize `query_state`.
1151 pub(crate) unsafe fn new(
1152 world: UnsafeWorldCell<'w>,
1153 query_state: &'s QueryState<D, F>,
1154 last_run: Tick,
1155 this_run: Tick,
1156 ) -> Option<Self> {
1157 query_state.is_dense.then(|| Self {
1158 // SAFETY: We only access table data that has been registered in `query_state`
1159 tables: unsafe { &world.storages().tables },
1160 storage_id_iter: query_state.matched_storage_ids.iter(),
1161 // SAFETY: The invariants are upheld by the caller.
1162 fetch: unsafe { D::init_fetch(world, &query_state.fetch_state, last_run, this_run) },
1163 query_state,
1164 })
1165 }
1166}
1167
1168impl<'w, 's, D: ContiguousQueryData, F: ArchetypeFilter> Iterator
1169 for QueryContiguousIter<'w, 's, D, F>
1170{
1171 type Item = D::Contiguous<'w, 's>;
1172
1173 #[inline(always)]
1174 fn next(&mut self) -> Option<Self::Item> {
1175 loop {
1176 // SAFETY: Query is dense
1177 let table_id = unsafe { self.storage_id_iter.next()?.table_id };
1178 // SAFETY: `table_id` was returned by `self.storage_id_iter` which always returns a
1179 // valid id
1180 let table = unsafe { self.tables.get(table_id).debug_checked_unwrap() };
1181 if table.is_empty() {
1182 continue;
1183 }
1184 // SAFETY:
1185 // - `table` is from the same world as `self.query_state` (`self.storage_id_iter` is
1186 // from the same world as `self.query_state`, see [`Self::new`])
1187 // - `self.fetch` was initialized with `self.query_state` (in [`Self::new`])
1188 unsafe {
1189 D::set_table(&mut self.fetch, &self.query_state.fetch_state, table);
1190 }
1191
1192 // no filtering because `F` implements `ArchetypeFilter` which ensures that `QueryFilter::fetch`
1193 // always returns true
1194
1195 let entities = table.entities();
1196
1197 // SAFETY:
1198 // - [`D::set_table`] is executed prior.
1199 // - `table.entities()` return a valid entity array
1200 // - the caller of [`Self::new`] ensures that the world has permission to access any of
1201 // the components registered in `self.query_state`
1202 let item = unsafe {
1203 D::fetch_contiguous(
1204 &self.query_state.fetch_state,
1205 &mut self.fetch,
1206 entities,
1207 0..(entities.len() as u32),
1208 )
1209 };
1210
1211 return Some(item);
1212 }
1213 }
1214
1215 fn size_hint(&self) -> (usize, Option<usize>) {
1216 (0, self.storage_id_iter.size_hint().1)
1217 }
1218}
1219
1220// [`QueryIterationCursor::next_contiguous`] always returns None when exhausted
1221impl<'w, 's, D: ContiguousQueryData, F: ArchetypeFilter> FusedIterator
1222 for QueryContiguousIter<'w, 's, D, F>
1223{
1224}
1225
1226/// An [`Iterator`] over sorted query results of a [`Query`](crate::system::Query).
1227///
1228/// This struct is created by the [`QueryIter::sort`], [`QueryIter::sort_unstable`],
1229/// [`QueryIter::sort_by`], [`QueryIter::sort_unstable_by`], [`QueryIter::sort_by_key`],
1230/// [`QueryIter::sort_unstable_by_key`], and [`QueryIter::sort_by_cached_key`] methods.
1231pub struct QuerySortedIter<'w, 's, D: QueryData, F: QueryFilter, I>
1232where
1233 I: Iterator<Item = Entity>,
1234{
1235 entity_iter: I,
1236 entities: &'w Entities,
1237 tables: &'w Tables,
1238 archetypes: &'w Archetypes,
1239 fetch: D::Fetch<'w>,
1240 query_state: &'s QueryState<D, F>,
1241}
1242
1243impl<'w, 's, D: QueryData, F: QueryFilter, I: Iterator> QuerySortedIter<'w, 's, D, F, I>
1244where
1245 I: Iterator<Item = Entity>,
1246{
1247 /// # Safety
1248 /// - `world` must have permission to access any of the components registered in `query_state`.
1249 /// - `world` must be the same one used to initialize `query_state`.
1250 /// - `entity_list` must only contain unique entities or be empty.
1251 pub(crate) unsafe fn new<EntityList: IntoIterator<IntoIter = I>>(
1252 world: UnsafeWorldCell<'w>,
1253 query_state: &'s QueryState<D, F>,
1254 entity_list: EntityList,
1255 last_run: Tick,
1256 this_run: Tick,
1257 ) -> QuerySortedIter<'w, 's, D, F, I> {
1258 let fetch = D::init_fetch(world, &query_state.fetch_state, last_run, this_run);
1259 QuerySortedIter {
1260 query_state,
1261 entities: world.entities(),
1262 archetypes: world.archetypes(),
1263 // SAFETY: We only access table data that has been registered in `query_state`.
1264 // This means `world` has permission to access the data we use.
1265 tables: &world.storages().tables,
1266 fetch,
1267 entity_iter: entity_list.into_iter(),
1268 }
1269 }
1270
1271 /// Get the next result from the query.
1272 ///
1273 /// If the [`QueryData`] does not implement [`IterQueryData`],
1274 /// then it is not sound to yield multiple items concurrently
1275 /// and the resulting [`QuerySortedIter`] will not implement [`Iterator`].
1276 /// In that case, this method can be used to iterate over the items
1277 /// while ensuring only one is alive at a time.
1278 ///
1279 /// Most queries do implement [`IterQueryData`],
1280 /// and can use the ordinary [`Iterator::next`]
1281 /// method or a `for` loop.
1282 ///
1283 /// # Example
1284 ///
1285 /// ```
1286 /// # use bevy_ecs::prelude::*;
1287 /// # #[derive(Component, Ord, PartialOrd, Eq, PartialEq)]
1288 /// # struct C;
1289 /// fn system(mut query: Query<&mut C>) {
1290 /// let mut iter = query.iter_mut().sort::<&C>();
1291 /// while let Some(mut c) = iter.fetch_next() {
1292 /// //
1293 /// }
1294 /// }
1295 /// # bevy_ecs::system::assert_is_system(system);
1296 /// ```
1297 pub fn fetch_next(&mut self) -> Option<D::Item<'_, 's>> {
1298 while let Some(entity) = self.entity_iter.next() {
1299 // SAFETY:
1300 // - `entity` is passed from `entity_iter` the first time.
1301 // - `self` is mutably borrowed, so there are no other items alive for any entity.
1302 if let Some(item) = unsafe { self.fetch_next_impl(entity) } {
1303 return Some(D::shrink(item));
1304 }
1305 }
1306 None
1307 }
1308
1309 /// Get the next result from the back of the query.
1310 ///
1311 /// If the [`QueryData`] does not implement [`IterQueryData`],
1312 /// then it is not sound to yield multiple items concurrently
1313 /// and the resulting [`QuerySortedIter`] will not implement [`Iterator`].
1314 /// In that case, this method can be used to iterate over the items
1315 /// while ensuring only one is alive at a time.
1316 ///
1317 /// Most queries do implement [`IterQueryData`],
1318 /// and can use the ordinary [`Iterator::next`]
1319 /// method or a `for` loop.
1320 pub fn fetch_next_back(&mut self) -> Option<D::Item<'_, 's>>
1321 where
1322 I: DoubleEndedIterator,
1323 {
1324 while let Some(entity) = self.entity_iter.next_back() {
1325 // SAFETY:
1326 // - `entity` is passed from `entity_iter` the first time.
1327 // - `self` is mutably borrowed, so there are no other items alive for any entity.
1328 if let Some(item) = unsafe { self.fetch_next_impl(entity) } {
1329 return Some(D::shrink(item));
1330 }
1331 }
1332 None
1333 }
1334
1335 /// # Safety
1336 ///
1337 /// - `entity` must stem from `self.entity_iter`
1338 /// - If `D` does not impl `ReadOnlyQueryData`, then there must not be any other `Item`s alive for the current entity
1339 /// - If `D` does not impl `IterQueryData`, then there must not be any other `Item`s alive for *any* entity
1340 #[inline(always)]
1341 unsafe fn fetch_next_impl(&mut self, entity: Entity) -> Option<D::Item<'w, 's>> {
1342 let (location, archetype, table);
1343 // SAFETY:
1344 // `tables` and `archetypes` belong to the same world that the [`QueryIter`]
1345 // was initialized for.
1346 unsafe {
1347 location = self.entities.get_spawned(entity).debug_checked_unwrap();
1348 archetype = self
1349 .archetypes
1350 .get(location.archetype_id)
1351 .debug_checked_unwrap();
1352 table = self.tables.get(location.table_id).debug_checked_unwrap();
1353 }
1354
1355 // SAFETY: `archetype` is from the world that `fetch` was created for,
1356 // `fetch_state` is the state that `fetch` was initialized with
1357 unsafe {
1358 D::set_archetype(
1359 &mut self.fetch,
1360 &self.query_state.fetch_state,
1361 archetype,
1362 table,
1363 );
1364 }
1365
1366 // The entity list has already been filtered by the query lens, so we forego filtering here.
1367 // SAFETY:
1368 // - set_archetype was called prior, `location.archetype_row` is an archetype index in range of the current archetype
1369 // - Caller ensures there are no conflicting items alive
1370 unsafe {
1371 D::fetch(
1372 &self.query_state.fetch_state,
1373 &mut self.fetch,
1374 entity,
1375 location.table_row,
1376 )
1377 }
1378 }
1379}
1380
1381impl<'w, 's, D: IterQueryData, F: QueryFilter, I: Iterator> Iterator
1382 for QuerySortedIter<'w, 's, D, F, I>
1383where
1384 I: Iterator<Item = Entity>,
1385{
1386 type Item = D::Item<'w, 's>;
1387
1388 #[inline(always)]
1389 fn next(&mut self) -> Option<Self::Item> {
1390 while let Some(entity) = self.entity_iter.next() {
1391 // SAFETY:
1392 // - `entity` is passed from `entity_iter` the first time.
1393 // - `D: IterQueryData`
1394 if let Some(item) = unsafe { self.fetch_next_impl(entity) } {
1395 return Some(item);
1396 }
1397 }
1398 None
1399 }
1400
1401 fn size_hint(&self) -> (usize, Option<usize>) {
1402 let (min_size, max_size) = self.entity_iter.size_hint();
1403 let archetype_query = D::IS_ARCHETYPAL;
1404 let min_size = if archetype_query { min_size } else { 0 };
1405 (min_size, max_size)
1406 }
1407}
1408
1409impl<'w, 's, D: IterQueryData, F: QueryFilter, I: Iterator> DoubleEndedIterator
1410 for QuerySortedIter<'w, 's, D, F, I>
1411where
1412 I: DoubleEndedIterator<Item = Entity>,
1413{
1414 #[inline(always)]
1415 fn next_back(&mut self) -> Option<Self::Item> {
1416 while let Some(entity) = self.entity_iter.next_back() {
1417 // SAFETY:
1418 // - `entity` is passed from `entity_iter` the first time.
1419 // - `D: IterQueryData`
1420 if let Some(item) = unsafe { self.fetch_next_impl(entity) } {
1421 return Some(item);
1422 }
1423 }
1424 None
1425 }
1426}
1427
1428impl<
1429 D: ArchetypeQueryData + IterQueryData,
1430 F: QueryFilter,
1431 I: ExactSizeIterator<Item = Entity>,
1432 > ExactSizeIterator for QuerySortedIter<'_, '_, D, F, I>
1433{
1434}
1435
1436// This is correct as [`QuerySortedIter`] returns `None` once exhausted if `entity_iter` does.
1437impl<'w, 's, D: IterQueryData, F: QueryFilter, I: Iterator> FusedIterator
1438 for QuerySortedIter<'w, 's, D, F, I>
1439where
1440 I: FusedIterator<Item = Entity>,
1441{
1442}
1443
1444// SAFETY:
1445// `I` stems from a collected and sorted `EntitySetIterator` ([`QueryIter`]).
1446// Fetching unique entities maintains uniqueness.
1447unsafe impl<'w, 's, F: QueryFilter, I: Iterator<Item = Entity>> EntitySetIterator
1448 for QuerySortedIter<'w, 's, Entity, F, I>
1449{
1450}
1451
1452impl<'w, 's, D: QueryData, F: QueryFilter, I: Iterator<Item = Entity>> Debug
1453 for QuerySortedIter<'w, 's, D, F, I>
1454{
1455 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1456 f.debug_struct("QuerySortedIter").finish()
1457 }
1458}
1459
1460/// An [`Iterator`] over the query items generated from an iterator of [`Entity`]s.
1461///
1462/// Items are returned in the order of the provided iterator.
1463/// In case of a nonexisting entity or mismatched component, a [`QueryEntityError`] is generated instead.
1464///
1465/// This struct is created by the [`Query::iter_many`](crate::system::Query::iter_many) and [`Query::iter_many_mut`](crate::system::Query::iter_many_mut) methods.
1466pub struct QueryManyIter<'w, 's, D: QueryData, F: QueryFilter, I: Iterator<Item: EntityEquivalent>>
1467{
1468 world: UnsafeWorldCell<'w>,
1469 entity_iter: I,
1470 entities: &'w Entities,
1471 tables: &'w Tables,
1472 archetypes: &'w Archetypes,
1473 fetch: D::Fetch<'w>,
1474 filter: F::Fetch<'w>,
1475 query_state: &'s QueryState<D, F>,
1476}
1477
1478impl<'w, 's, D: QueryData, F: QueryFilter, I: Iterator<Item: EntityEquivalent>>
1479 QueryManyIter<'w, 's, D, F, I>
1480{
1481 /// # Safety
1482 /// - `world` must have permission to access any of the components registered in `query_state`.
1483 /// - `world` must be the same one used to initialize `query_state`.
1484 pub(crate) unsafe fn new<EntityList: IntoIterator<IntoIter = I>>(
1485 world: UnsafeWorldCell<'w>,
1486 query_state: &'s QueryState<D, F>,
1487 entity_list: EntityList,
1488 last_run: Tick,
1489 this_run: Tick,
1490 ) -> QueryManyIter<'w, 's, D, F, I> {
1491 let fetch = D::init_fetch(world, &query_state.fetch_state, last_run, this_run);
1492 let filter = F::init_fetch(world, &query_state.filter_state, last_run, this_run);
1493 QueryManyIter {
1494 world,
1495 query_state,
1496 entities: world.entities(),
1497 archetypes: world.archetypes(),
1498 // SAFETY: We only access table data that has been registered in `query_state`.
1499 // This means `world` has permission to access the data we use.
1500 tables: &world.storages().tables,
1501 fetch,
1502 filter,
1503 entity_iter: entity_list.into_iter(),
1504 }
1505 }
1506
1507 /// # Safety
1508 ///
1509 /// - All arguments must stem from the same valid `QueryManyIter`.
1510 /// - If `D` does not impl `ReadOnlyQueryData`, then there must not be any other `Item`s alive for the current entity
1511 /// - If `D` does not impl `IterQueryData`, then there must not be any other `Item`s alive for *any* entity
1512 #[inline(always)]
1513 unsafe fn fetch_next_aliased_unchecked_internal(
1514 entity_borrow: impl EntityEquivalent,
1515 entities: &'w Entities,
1516 tables: &'w Tables,
1517 archetypes: &'w Archetypes,
1518 fetch: &mut D::Fetch<'w>,
1519 filter: &mut F::Fetch<'w>,
1520 query_state: &'s QueryState<D, F>,
1521 ) -> Result<D::Item<'w, 's>, QueryEntityError> {
1522 let entity = entity_borrow.entity();
1523 let location = entities.get_spawned(entity)?;
1524
1525 if !query_state
1526 .matched_archetypes
1527 .contains(location.archetype_id.index())
1528 {
1529 return Err(QueryEntityError::QueryDoesNotMatch(
1530 entity,
1531 location.archetype_id,
1532 ));
1533 }
1534
1535 let archetype = archetypes.get(location.archetype_id).debug_checked_unwrap();
1536 let table = tables.get(location.table_id).debug_checked_unwrap();
1537
1538 // SAFETY: `archetype` is from the world that `fetch/filter` were created for,
1539 // `fetch_state`/`filter_state` are the states that `fetch/filter` were initialized with
1540 unsafe {
1541 D::set_archetype(fetch, &query_state.fetch_state, archetype, table);
1542 }
1543 // SAFETY: `table` is from the world that `fetch/filter` were created for,
1544 // `fetch_state`/`filter_state` are the states that `fetch/filter` were initialized with
1545 unsafe {
1546 F::set_archetype(filter, &query_state.filter_state, archetype, table);
1547 }
1548
1549 // SAFETY: set_archetype was called prior.
1550 // `location.archetype_row` is an archetype index row in range of the current archetype, because if it was not, the match above would have `continue`d
1551 if unsafe {
1552 F::filter_fetch(
1553 &query_state.filter_state,
1554 filter,
1555 entity,
1556 location.table_row,
1557 )
1558 } && let Some(item) =
1559 // SAFETY:
1560 // - set_archetype was called prior, `location.archetype_row` is an archetype index in range of the current archetype
1561 // - Caller ensures there are no conflicting items alive
1562 unsafe {
1563 D::fetch(&query_state.fetch_state, fetch, entity, location.table_row)
1564 }
1565 {
1566 Ok(item)
1567 } else {
1568 Err(QueryEntityError::QueryDoesNotMatch(
1569 entity,
1570 location.archetype_id,
1571 ))
1572 }
1573 }
1574
1575 /// # Safety
1576 ///
1577 /// - If `D` does not impl `ReadOnlyQueryData`, then there must not be any other `Item`s alive for the current entity
1578 /// - If `D` does not impl `IterQueryData`, then there must not be any other `Item`s alive for *any* entity
1579 #[inline(always)]
1580 unsafe fn fetch_next_aliased_unchecked(
1581 &mut self,
1582 ) -> Option<Result<D::Item<'w, 's>, QueryEntityError>> {
1583 // SAFETY:
1584 // All arguments stem from self.
1585 // The caller has to prevent aliasing.
1586 Some(unsafe {
1587 Self::fetch_next_aliased_unchecked_internal(
1588 self.entity_iter.next()?,
1589 self.entities,
1590 self.tables,
1591 self.archetypes,
1592 &mut self.fetch,
1593 &mut self.filter,
1594 self.query_state,
1595 )
1596 })
1597 }
1598
1599 /// Get the next result from the query
1600 #[inline(always)]
1601 pub fn fetch_next(&mut self) -> Option<Result<D::Item<'_, 's>, QueryEntityError>> {
1602 // SAFETY:
1603 // We are limiting the returned reference to self,
1604 // making sure this method cannot be called multiple times without getting rid
1605 // of any previously returned unique references first, thus preventing aliasing.
1606 unsafe {
1607 self.fetch_next_aliased_unchecked()
1608 .map(|result| result.map(D::shrink))
1609 }
1610 }
1611
1612 /// Creates an iterator which calls [`Result::unwrap`] on each element.
1613 #[inline(always)]
1614 pub fn unwrapped(self) -> QueryManyUnwrappedIter<Self> {
1615 QueryManyUnwrappedIter(self)
1616 }
1617
1618 /// Creates an iterator which skips entities that don't match the query.
1619 #[inline(always)]
1620 pub fn matched(self) -> QueryManyMatchedIter<Self> {
1621 QueryManyMatchedIter(self)
1622 }
1623
1624 /// Sorts all query items into a new iterator, using the query lens as a key.
1625 ///
1626 /// This sort is stable (i.e., does not reorder equal elements).
1627 ///
1628 /// This uses [`slice::sort`] internally.
1629 ///
1630 /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
1631 /// This includes the allowed parameter type changes listed under [allowed transmutes].
1632 /// However, the lens uses the filter of the original query when present.
1633 ///
1634 /// The lens needs to be a [`SingleEntityQueryData`] because the current implementation
1635 /// of query transmutes does not support nested queries.
1636 /// This restriction may be lifted in the future.
1637 ///
1638 /// The sort is not cached across system runs.
1639 ///
1640 /// [allowed transmutes]: crate::system::Query#allowed-transmutes
1641 ///
1642 /// Unlike the sort methods on [`QueryIter`], this does NOT panic if `next`/`fetch_next` has been
1643 /// called on [`QueryManyIter`] before.
1644 ///
1645 /// # Examples
1646 /// ```rust
1647 /// # use bevy_ecs::prelude::*;
1648 /// # use std::{ops::{Deref, DerefMut}, iter::Sum};
1649 /// #
1650 /// # #[derive(Component)]
1651 /// # struct PartMarker;
1652 /// #
1653 /// # #[derive(Component, PartialEq, Eq, PartialOrd, Ord)]
1654 /// # struct PartIndex(usize);
1655 /// #
1656 /// # #[derive(Component, Clone, Copy)]
1657 /// # struct PartValue(usize);
1658 /// #
1659 /// # impl Deref for PartValue {
1660 /// # type Target = usize;
1661 /// #
1662 /// # fn deref(&self) -> &Self::Target {
1663 /// # &self.0
1664 /// # }
1665 /// # }
1666 /// #
1667 /// # impl DerefMut for PartValue {
1668 /// # fn deref_mut(&mut self) -> &mut Self::Target {
1669 /// # &mut self.0
1670 /// # }
1671 /// # }
1672 /// #
1673 /// # #[derive(Component, Debug, PartialEq, Eq, PartialOrd, Ord)]
1674 /// # struct Length(usize);
1675 /// #
1676 /// # #[derive(Component, Debug, PartialEq, Eq, PartialOrd, Ord)]
1677 /// # struct Width(usize);
1678 /// #
1679 /// # #[derive(Component, Debug, PartialEq, Eq, PartialOrd, Ord)]
1680 /// # struct Height(usize);
1681 /// #
1682 /// # #[derive(Component, PartialEq, Eq, PartialOrd, Ord)]
1683 /// # struct ParentEntity(Entity);
1684 /// #
1685 /// # let mut world = World::new();
1686 /// // We can ensure that a query always returns in the same order.
1687 /// fn system_1(query: Query<(Entity, &PartIndex)>) {
1688 /// # let entity_list: Vec<Entity> = Vec::new();
1689 /// let parts: Result<Vec<(Entity, &PartIndex)>, _> = query.iter_many(entity_list).sort::<&PartIndex>().collect();
1690 /// }
1691 ///
1692 /// // We can freely rearrange query components in the key.
1693 /// fn system_2(query: Query<(&Length, &Width, &Height), With<PartMarker>>) {
1694 /// # let entity_list: Vec<Entity> = Vec::new();
1695 /// for (length, width, height) in query.iter_many(entity_list).sort::<(&Height, &Length, &Width)>().flat_map(Result::ok) {
1696 /// println!("height: {height:?}, width: {width:?}, length: {length:?}")
1697 /// }
1698 /// }
1699 ///
1700 /// // You can use `fetch_next_back` to obtain mutable references in reverse order.
1701 /// fn system_3(
1702 /// mut query: Query<&mut PartValue>,
1703 /// ) {
1704 /// # let entity_list: Vec<Entity> = Vec::new();
1705 /// // We need to collect the internal iterator before iterating mutably
1706 /// let mut parent_query_iter = query.iter_many_mut(entity_list)
1707 /// .sort::<Entity>();
1708 ///
1709 /// let mut scratch_value = 0;
1710 /// while let Some(mut part_value) = parent_query_iter.fetch_next_back().map(Result::unwrap)
1711 /// {
1712 /// // some order-dependent operation, here bitwise XOR
1713 /// **part_value ^= scratch_value;
1714 /// scratch_value = **part_value;
1715 /// }
1716 /// }
1717 /// #
1718 /// # let mut schedule = Schedule::default();
1719 /// # schedule.add_systems((system_1, system_2, system_3));
1720 /// # schedule.run(&mut world);
1721 /// ```
1722 pub fn sort<L: ReadOnlyQueryData + SingleEntityQueryData + 'w>(
1723 self,
1724 ) -> QuerySortedManyIter<
1725 'w,
1726 's,
1727 D,
1728 F,
1729 impl ExactSizeIterator<Item = Result<Entity, QueryEntityError>>
1730 + DoubleEndedIterator
1731 + FusedIterator
1732 + 'w,
1733 >
1734 where
1735 for<'lw, 'ls> L::Item<'lw, 'ls>: Ord,
1736 {
1737 self.sort_impl::<L>(|keyed_query| {
1738 keyed_query.sort();
1739 })
1740 }
1741
1742 /// Sorts all query items into a new iterator, using the query lens as a key.
1743 ///
1744 /// This sort is unstable (i.e., may reorder equal elements).
1745 ///
1746 /// This uses [`slice::sort_unstable`] internally.
1747 ///
1748 /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
1749 /// This includes the allowed parameter type changes listed under [allowed transmutes]..
1750 /// However, the lens uses the filter of the original query when present.
1751 ///
1752 /// The lens needs to be a [`SingleEntityQueryData`] because the current implementation
1753 /// of query transmutes does not support nested queries.
1754 /// This restriction may be lifted in the future.
1755 ///
1756 /// The sort is not cached across system runs.
1757 ///
1758 /// [allowed transmutes]: crate::system::Query#allowed-transmutes
1759 ///
1760 /// Unlike the sort methods on [`QueryIter`], this does NOT panic if `next`/`fetch_next` has been
1761 /// called on [`QueryManyIter`] before.
1762 ///
1763 /// # Example
1764 /// ```
1765 /// # use bevy_ecs::prelude::*;
1766 /// #
1767 /// # let mut world = World::new();
1768 /// #
1769 /// # #[derive(Component)]
1770 /// # struct PartMarker;
1771 /// #
1772 /// # let entity_list: Vec<Entity> = Vec::new();
1773 /// #[derive(Component, PartialEq, Eq, PartialOrd, Ord)]
1774 /// enum Flying {
1775 /// Enabled,
1776 /// Disabled
1777 /// };
1778 ///
1779 /// // We perform an unstable sort by a Component with few values.
1780 /// fn system_1(query: Query<&Flying, With<PartMarker>>) {
1781 /// # let entity_list: Vec<Entity> = Vec::new();
1782 /// let part_values: Vec<&Flying> = query.iter_many(entity_list).sort_unstable::<&Flying>().map(Result::unwrap).collect();
1783 /// }
1784 /// #
1785 /// # let mut schedule = Schedule::default();
1786 /// # schedule.add_systems((system_1));
1787 /// # schedule.run(&mut world);
1788 /// ```
1789 pub fn sort_unstable<L: ReadOnlyQueryData + SingleEntityQueryData + 'w>(
1790 self,
1791 ) -> QuerySortedManyIter<
1792 'w,
1793 's,
1794 D,
1795 F,
1796 impl ExactSizeIterator<Item = Result<Entity, QueryEntityError>>
1797 + DoubleEndedIterator
1798 + FusedIterator
1799 + 'w,
1800 >
1801 where
1802 for<'lw, 'ls> L::Item<'lw, 'ls>: Ord,
1803 {
1804 self.sort_impl::<L>(|keyed_query| keyed_query.sort_unstable())
1805 }
1806
1807 /// Sorts all query items into a new iterator with a comparator function over the query lens.
1808 ///
1809 /// This sort is stable (i.e., does not reorder equal elements).
1810 ///
1811 /// This uses [`slice::sort_by`] internally.
1812 ///
1813 /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
1814 /// This includes the allowed parameter type changes listed under [allowed transmutes].
1815 /// However, the lens uses the filter of the original query when present.
1816 ///
1817 /// The lens needs to be a [`SingleEntityQueryData`] because the current implementation
1818 /// of query transmutes does not support nested queries.
1819 /// This restriction may be lifted in the future.
1820 ///
1821 /// The sort is not cached across system runs.
1822 ///
1823 /// [allowed transmutes]: crate::system::Query#allowed-transmutes
1824 ///
1825 /// Unlike the sort methods on [`QueryIter`], this does NOT panic if `next`/`fetch_next` has been
1826 /// called on [`QueryManyIter`] before.
1827 ///
1828 /// # Example
1829 /// ```
1830 /// # use bevy_ecs::prelude::*;
1831 /// # use std::ops::Deref;
1832 /// #
1833 /// # impl Deref for PartValue {
1834 /// # type Target = f32;
1835 /// #
1836 /// # fn deref(&self) -> &Self::Target {
1837 /// # &self.0
1838 /// # }
1839 /// # }
1840 /// #
1841 /// # let mut world = World::new();
1842 /// # let entity_list: Vec<Entity> = Vec::new();
1843 /// #
1844 /// #[derive(Component)]
1845 /// struct PartValue(f32);
1846 ///
1847 /// // We can use a cmp function on components do not implement Ord.
1848 /// fn system_1(query: Query<&PartValue>) {
1849 /// # let entity_list: Vec<Entity> = Vec::new();
1850 /// // Sort part values according to `f32::total_comp`.
1851 /// let part_values: Vec<&PartValue> = query
1852 /// .iter_many(entity_list)
1853 /// .sort_by::<&PartValue>(|value_1, value_2| value_1.total_cmp(*value_2))
1854 /// .map(Result::unwrap)
1855 /// .collect();
1856 /// }
1857 /// #
1858 /// # let mut schedule = Schedule::default();
1859 /// # schedule.add_systems((system_1));
1860 /// # schedule.run(&mut world);
1861 /// ```
1862 pub fn sort_by<L: ReadOnlyQueryData + SingleEntityQueryData + 'w>(
1863 self,
1864 mut compare: impl FnMut(&L::Item<'_, '_>, &L::Item<'_, '_>) -> Ordering,
1865 ) -> QuerySortedManyIter<
1866 'w,
1867 's,
1868 D,
1869 F,
1870 impl ExactSizeIterator<Item = Result<Entity, QueryEntityError>>
1871 + DoubleEndedIterator
1872 + FusedIterator
1873 + 'w,
1874 > {
1875 self.sort_impl::<L>(move |keyed_query| {
1876 keyed_query.sort_by(|result_1, result_2| match (result_1, result_2) {
1877 (Ok((key_1, _)), Ok((key_2, _))) => compare(key_1, key_2),
1878 (Ok(_), Err(_)) => Ordering::Greater,
1879 (Err(_), Ok(_)) => Ordering::Less,
1880 (Err(_), Err(_)) => Ordering::Equal,
1881 });
1882 })
1883 }
1884
1885 /// Sorts all query items into a new iterator with a comparator function over the query lens.
1886 ///
1887 /// This sort is unstable (i.e., may reorder equal elements).
1888 ///
1889 /// This uses [`slice::sort_unstable_by`] internally.
1890 ///
1891 /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
1892 /// This includes the allowed parameter type changes listed under [allowed transmutes].
1893 /// However, the lens uses the filter of the original query when present.
1894 ///
1895 /// The lens needs to be a [`SingleEntityQueryData`] because the current implementation
1896 /// of query transmutes does not support nested queries.
1897 /// This restriction may be lifted in the future.
1898 ///
1899 /// The sort is not cached across system runs.
1900 ///
1901 /// [allowed transmutes]: crate::system::Query#allowed-transmutes
1902 ///
1903 /// Unlike the sort methods on [`QueryIter`], this does NOT panic if `next`/`fetch_next` has been
1904 /// called on [`QueryManyIter`] before.
1905 pub fn sort_unstable_by<L: ReadOnlyQueryData + SingleEntityQueryData + 'w>(
1906 self,
1907 mut compare: impl FnMut(&L::Item<'_, '_>, &L::Item<'_, '_>) -> Ordering,
1908 ) -> QuerySortedManyIter<
1909 'w,
1910 's,
1911 D,
1912 F,
1913 impl ExactSizeIterator<Item = Result<Entity, QueryEntityError>>
1914 + DoubleEndedIterator
1915 + FusedIterator
1916 + 'w,
1917 > {
1918 self.sort_impl::<L>(move |keyed_query| {
1919 keyed_query.sort_unstable_by(|result_1, result_2| match (result_1, result_2) {
1920 (Ok((key_1, _)), Ok((key_2, _))) => compare(key_1, key_2),
1921 (Ok(_), Err(_)) => Ordering::Greater,
1922 (Err(_), Ok(_)) => Ordering::Less,
1923 (Err(_), Err(_)) => Ordering::Equal,
1924 });
1925 })
1926 }
1927
1928 /// Sorts all query items into a new iterator with a key extraction function over the query lens.
1929 ///
1930 /// This sort is stable (i.e., does not reorder equal elements).
1931 ///
1932 /// This uses [`slice::sort_by_key`] internally.
1933 ///
1934 /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
1935 /// This includes the allowed parameter type changes listed under [allowed transmutes].
1936 /// However, the lens uses the filter of the original query when present.
1937 ///
1938 /// The lens needs to be a [`SingleEntityQueryData`] because the current implementation
1939 /// of query transmutes does not support nested queries.
1940 /// This restriction may be lifted in the future.
1941 ///
1942 /// The sort is not cached across system runs.
1943 ///
1944 /// [allowed transmutes]: crate::system::Query#allowed-transmutes
1945 ///
1946 /// Unlike the sort methods on [`QueryIter`], this does NOT panic if `next`/`fetch_next` has been
1947 /// called on [`QueryManyIter`] before.
1948 ///
1949 /// # Example
1950 /// ```
1951 /// # use bevy_ecs::prelude::*;
1952 /// # use std::ops::Deref;
1953 /// #
1954 /// # #[derive(Component)]
1955 /// # struct PartMarker;
1956 /// #
1957 /// # impl Deref for PartValue {
1958 /// # type Target = f32;
1959 /// #
1960 /// # fn deref(&self) -> &Self::Target {
1961 /// # &self.0
1962 /// # }
1963 /// # }
1964 /// #
1965 /// # let mut world = World::new();
1966 /// # let entity_list: Vec<Entity> = Vec::new();
1967 /// #
1968 /// #[derive(Component)]
1969 /// struct AvailableMarker;
1970 ///
1971 /// #[derive(Component, PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
1972 /// enum Rarity {
1973 /// Common,
1974 /// Rare,
1975 /// Epic,
1976 /// Legendary
1977 /// };
1978 ///
1979 /// #[derive(Component)]
1980 /// struct PartValue(f32);
1981 ///
1982 /// // We can sort with the internals of components that do not implement Ord.
1983 /// fn system_1(query: Query<(Entity, &PartValue)>) {
1984 /// # let entity_list: Vec<Entity> = Vec::new();
1985 /// // Sort by the sines of the part values.
1986 /// let parts: Result<Vec<(Entity, &PartValue)>, _> = query
1987 /// .iter_many(entity_list)
1988 /// .sort_by_key::<&PartValue, _>(|value| value.sin() as usize)
1989 /// .collect();
1990 /// }
1991 ///
1992 /// // We can define our own custom comparison functions over an EntityRef.
1993 /// fn system_2(query: Query<EntityRef, With<PartMarker>>) {
1994 /// # let entity_list: Vec<Entity> = Vec::new();
1995 /// // Sort by whether parts are available and their rarity.
1996 /// // We want the available legendaries to come first, so we reverse the iterator.
1997 /// let parts: Vec<EntityRef> = query.iter_many(entity_list)
1998 /// .sort_by_key::<EntityRef, _>(|entity_ref| {
1999 /// (
2000 /// entity_ref.contains::<AvailableMarker>(),
2001 // entity_ref.get::<Rarity>().copied()
2002 /// )
2003 /// })
2004 /// .rev()
2005 /// .flat_map(Result::ok)
2006 /// .collect();
2007 /// }
2008 /// # let mut schedule = Schedule::default();
2009 /// # schedule.add_systems((system_1, system_2));
2010 /// # schedule.run(&mut world);
2011 /// ```
2012 pub fn sort_by_key<L: ReadOnlyQueryData + SingleEntityQueryData + 'w, K>(
2013 self,
2014 mut f: impl FnMut(&L::Item<'_, '_>) -> K,
2015 ) -> QuerySortedManyIter<
2016 'w,
2017 's,
2018 D,
2019 F,
2020 impl ExactSizeIterator<Item = Result<Entity, QueryEntityError>>
2021 + DoubleEndedIterator
2022 + FusedIterator
2023 + 'w,
2024 >
2025 where
2026 K: Ord,
2027 {
2028 self.sort_impl::<L>(move |keyed_query| {
2029 keyed_query.sort_by_key(|result| result.as_ref().ok().map(|(key, _)| f(key)));
2030 })
2031 }
2032
2033 /// Sorts all query items into a new iterator with a key extraction function over the query lens.
2034 ///
2035 /// This sort is unstable (i.e., may reorder equal elements).
2036 ///
2037 /// This uses [`slice::sort_unstable_by_key`] internally.
2038 ///
2039 /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
2040 /// This includes the allowed parameter type changes listed under [allowed transmutes].
2041 /// However, the lens uses the filter of the original query when present.
2042 ///
2043 /// The lens needs to be a [`SingleEntityQueryData`] because the current implementation
2044 /// of query transmutes does not support nested queries.
2045 /// This restriction may be lifted in the future.
2046 ///
2047 /// The sort is not cached across system runs.
2048 ///
2049 /// [allowed transmutes]: crate::system::Query#allowed-transmutes
2050 ///
2051 /// Unlike the sort methods on [`QueryIter`], this does NOT panic if `next`/`fetch_next` has been
2052 /// called on [`QueryManyIter`] before.
2053 pub fn sort_unstable_by_key<L: ReadOnlyQueryData + SingleEntityQueryData + 'w, K>(
2054 self,
2055 mut f: impl FnMut(&L::Item<'_, '_>) -> K,
2056 ) -> QuerySortedManyIter<
2057 'w,
2058 's,
2059 D,
2060 F,
2061 impl ExactSizeIterator<Item = Result<Entity, QueryEntityError>>
2062 + DoubleEndedIterator
2063 + FusedIterator
2064 + 'w,
2065 >
2066 where
2067 K: Ord,
2068 {
2069 self.sort_impl::<L>(move |keyed_query| {
2070 keyed_query.sort_unstable_by_key(|result| result.as_ref().ok().map(|(key, _)| f(key)));
2071 })
2072 }
2073
2074 /// Sort all query items into a new iterator with a key extraction function over the query lens.
2075 ///
2076 /// This sort is stable (i.e., does not reorder equal elements).
2077 ///
2078 /// This uses [`slice::sort_by_cached_key`] internally.
2079 ///
2080 /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
2081 /// This includes the allowed parameter type changes listed under [allowed transmutes].
2082 /// However, the lens uses the filter of the original query when present.
2083 ///
2084 /// The lens needs to be a [`SingleEntityQueryData`] because the current implementation
2085 /// of query transmutes does not support nested queries.
2086 /// This restriction may be lifted in the future.
2087 ///
2088 /// The sort is not cached across system runs.
2089 ///
2090 /// [allowed transmutes]: crate::system::Query#allowed-transmutes
2091 ///
2092 /// Unlike the sort methods on [`QueryIter`], this does NOT panic if `next`/`fetch_next` has been
2093 /// called on [`QueryManyIter`] before.
2094 pub fn sort_by_cached_key<L: ReadOnlyQueryData + SingleEntityQueryData + 'w, K>(
2095 self,
2096 mut f: impl FnMut(&L::Item<'_, '_>) -> K,
2097 ) -> QuerySortedManyIter<
2098 'w,
2099 's,
2100 D,
2101 F,
2102 impl ExactSizeIterator<Item = Result<Entity, QueryEntityError>>
2103 + DoubleEndedIterator
2104 + FusedIterator
2105 + 'w,
2106 >
2107 where
2108 K: Ord,
2109 {
2110 self.sort_impl::<L>(move |keyed_query| {
2111 keyed_query.sort_by_cached_key(|result| result.as_ref().ok().map(|(key, _)| f(key)));
2112 })
2113 }
2114
2115 /// Shared implementation for the various `sort` methods.
2116 /// This uses the lens to collect the items for sorting, but delegates the actual sorting to the provided closure.
2117 ///
2118 /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
2119 /// This includes the allowed parameter type changes listed under [allowed transmutes].
2120 /// However, the lens uses the filter of the original query when present.
2121 ///
2122 /// The lens needs to be a [`SingleEntityQueryData`] because the current implementation
2123 /// of query transmutes does not support nested queries.
2124 /// This restriction may be lifted in the future.
2125 ///
2126 /// The sort is not cached across system runs.
2127 ///
2128 /// [allowed transmutes]: crate::system::Query#allowed-transmutes
2129 ///
2130 /// Unlike the sort methods on [`QueryIter`], this does NOT panic if `next`/`fetch_next` has been
2131 /// called on [`QueryManyIter`] before.
2132 fn sort_impl<L: ReadOnlyQueryData + SingleEntityQueryData + 'w>(
2133 self,
2134 sort: impl FnOnce(
2135 &mut Vec<Result<(L::Item<'_, '_>, NeutralOrd<Entity>), NeutralOrd<QueryEntityError>>>,
2136 ),
2137 ) -> QuerySortedManyIter<
2138 'w,
2139 's,
2140 D,
2141 F,
2142 impl ExactSizeIterator<Item = Result<Entity, QueryEntityError>>
2143 + DoubleEndedIterator
2144 + FusedIterator
2145 + 'w,
2146 > {
2147 let world = self.world;
2148
2149 let query_lens_state = self.query_state.transmute_filtered::<(L, Entity), F>(world);
2150
2151 // SAFETY:
2152 // `self.world` has permission to access the required components.
2153 // The original query iter has not been iterated on, so no items are aliased from it.
2154 // `QueryIter::new` ensures `world` is the same one used to initialize `query_state`.
2155 let query_lens = unsafe { query_lens_state.query_unchecked_manual(world) }
2156 .iter_many_inner(self.entity_iter);
2157 let mut keyed_query: Vec<_> = query_lens
2158 .map(|result| match result {
2159 Ok((key, entity)) => Ok((key, NeutralOrd(entity))),
2160 Err(e) => Err(NeutralOrd(e)),
2161 })
2162 .collect();
2163 sort(&mut keyed_query);
2164 // Re-collect into a `Vec` to eagerly drop the lens items.
2165 // They must be dropped before `fetch_next` is called since they may alias
2166 // with other data items if there are duplicate entities in `entity_iter`.
2167 let entity_iter = keyed_query
2168 .into_iter()
2169 .map(|result| result.map(|(.., entity)| entity.0).map_err(|error| error.0))
2170 .collect::<Vec<_>>()
2171 .into_iter();
2172 // SAFETY:
2173 // `self.world` has permission to access the required components.
2174 // Each lens query item is dropped before the respective actual query item is accessed.
2175 unsafe {
2176 QuerySortedManyIter::new(
2177 world,
2178 self.query_state,
2179 entity_iter,
2180 world.last_change_tick(),
2181 world.change_tick(),
2182 )
2183 }
2184 }
2185}
2186
2187impl<'w, 's, D: QueryData, F: QueryFilter, I: DoubleEndedIterator<Item: EntityEquivalent>>
2188 QueryManyIter<'w, 's, D, F, I>
2189{
2190 /// # Safety
2191 ///
2192 /// - If `D` does not impl `ReadOnlyQueryData`, then there must not be any other `Item`s alive for the current entity
2193 /// - If `D` does not impl `IterQueryData`, then there must not be any other `Item`s alive for *any* entity
2194 #[inline(always)]
2195 unsafe fn fetch_next_back_aliased_unchecked(
2196 &mut self,
2197 ) -> Option<Result<D::Item<'w, 's>, QueryEntityError>> {
2198 // SAFETY:
2199 // All arguments stem from self.
2200 // The caller has to prevent aliasing.
2201 Some(unsafe {
2202 Self::fetch_next_aliased_unchecked_internal(
2203 self.entity_iter.next_back()?,
2204 self.entities,
2205 self.tables,
2206 self.archetypes,
2207 &mut self.fetch,
2208 &mut self.filter,
2209 self.query_state,
2210 )
2211 })
2212 }
2213
2214 /// Get the next result from the back of the query
2215 #[inline(always)]
2216 pub fn fetch_next_back(&mut self) -> Option<Result<D::Item<'_, 's>, QueryEntityError>> {
2217 // SAFETY:
2218 // We are limiting the returned reference to self,
2219 // making sure this method cannot be called multiple times without getting rid
2220 // of any previously returned unique references first, thus preventing aliasing.
2221 unsafe {
2222 self.fetch_next_back_aliased_unchecked()
2223 .map(|result| result.map(D::shrink))
2224 }
2225 }
2226}
2227
2228impl<'w, 's, D: ReadOnlyQueryData, F: QueryFilter, I: Iterator<Item: EntityEquivalent>> Iterator
2229 for QueryManyIter<'w, 's, D, F, I>
2230{
2231 type Item = Result<D::Item<'w, 's>, QueryEntityError>;
2232
2233 #[inline(always)]
2234 fn next(&mut self) -> Option<Self::Item> {
2235 // SAFETY:
2236 // It is safe to alias for ReadOnlyWorldQuery.
2237 unsafe { self.fetch_next_aliased_unchecked() }
2238 }
2239
2240 fn size_hint(&self) -> (usize, Option<usize>) {
2241 self.entity_iter.size_hint()
2242 }
2243}
2244
2245impl<
2246 'w,
2247 's,
2248 D: ReadOnlyQueryData,
2249 F: QueryFilter,
2250 I: DoubleEndedIterator<Item: EntityEquivalent>,
2251 > DoubleEndedIterator for QueryManyIter<'w, 's, D, F, I>
2252{
2253 #[inline(always)]
2254 fn next_back(&mut self) -> Option<Self::Item> {
2255 // SAFETY:
2256 // It is safe to alias for ReadOnlyWorldQuery.
2257 unsafe { self.fetch_next_back_aliased_unchecked() }
2258 }
2259}
2260
2261// This is correct as [`QueryManyIter`] always returns `None` once exhausted.
2262impl<'w, 's, D: ReadOnlyQueryData, F: QueryFilter, I: Iterator<Item: EntityEquivalent>>
2263 FusedIterator for QueryManyIter<'w, 's, D, F, I>
2264{
2265}
2266
2267impl<'w, 's, D: QueryData, F: QueryFilter, I: Iterator<Item: EntityEquivalent>> Debug
2268 for QueryManyIter<'w, 's, D, F, I>
2269{
2270 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
2271 f.debug_struct("QueryManyIter").finish()
2272 }
2273}
2274
2275/// An [`Iterator`] over the query items generated from an iterator of [`Entity`]s.
2276///
2277/// Items are returned in the order of the provided iterator.
2278/// In case of a nonexisting entity or mismatched component, iteration will panic.
2279///
2280/// This struct is created by the [`QueryManyIter::unwrapped`] and [`QueryManyUniqueIter::unwrapped`] methods.
2281pub struct QueryManyUnwrappedIter<I>(I);
2282
2283impl<'w, 's, D: QueryData, F: QueryFilter, I: Iterator<Item: EntityEquivalent>>
2284 QueryManyUnwrappedIter<QueryManyIter<'w, 's, D, F, I>>
2285{
2286 /// Get the next result from the query
2287 #[inline(always)]
2288 pub fn fetch_next(&mut self) -> Option<D::Item<'_, 's>> {
2289 self.0.fetch_next().map(Result::unwrap)
2290 }
2291}
2292
2293impl<'w, 's, D: QueryData, F: QueryFilter, I: DoubleEndedIterator<Item: EntityEquivalent>>
2294 QueryManyUnwrappedIter<QueryManyIter<'w, 's, D, F, I>>
2295{
2296 /// Get the next result from the back of the query
2297 #[inline(always)]
2298 pub fn fetch_next_back(&mut self) -> Option<D::Item<'_, 's>> {
2299 self.0.fetch_next_back().map(Result::unwrap)
2300 }
2301}
2302
2303impl<T, E: Debug, I: Iterator<Item = Result<T, E>>> Iterator for QueryManyUnwrappedIter<I> {
2304 type Item = T;
2305
2306 #[inline(always)]
2307 fn next(&mut self) -> Option<Self::Item> {
2308 self.0.next().map(Result::unwrap)
2309 }
2310
2311 fn size_hint(&self) -> (usize, Option<usize>) {
2312 self.0.size_hint()
2313 }
2314}
2315
2316impl<T, E: Debug, I: DoubleEndedIterator<Item = Result<T, E>>> DoubleEndedIterator
2317 for QueryManyUnwrappedIter<I>
2318{
2319 #[inline(always)]
2320 fn next_back(&mut self) -> Option<Self::Item> {
2321 self.0.next_back().map(Result::unwrap)
2322 }
2323}
2324
2325impl<T, E: Debug, I: FusedIterator<Item = Result<T, E>>> FusedIterator
2326 for QueryManyUnwrappedIter<I>
2327{
2328}
2329
2330// SAFETY: Fetching unique entities maintains uniqueness.
2331unsafe impl<'w, 's, F: QueryFilter, I: EntitySetIterator> EntitySetIterator
2332 for QueryManyUnwrappedIter<QueryManyIter<'w, 's, Entity, F, I>>
2333{
2334}
2335
2336// SAFETY: Fetching unique entities maintains uniqueness.
2337unsafe impl<'w, 's, F: QueryFilter, I: EntitySetIterator> EntitySetIterator
2338 for QueryManyUnwrappedIter<QueryManyUniqueIter<'w, 's, Entity, F, I>>
2339{
2340}
2341
2342impl<I> Debug for QueryManyUnwrappedIter<I> {
2343 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
2344 f.debug_struct("QueryManyUnwrappedIter").finish()
2345 }
2346}
2347
2348/// An [`Iterator`] over the query items generated from an iterator of [`Entity`]s.
2349///
2350/// Items are returned in the order of the provided iterator.
2351/// Entities that don't match the query are skipped.
2352///
2353/// This struct is created by the [`QueryManyIter::matched`] and [`QueryManyUniqueIter::matched`] methods.
2354pub struct QueryManyMatchedIter<I>(I);
2355
2356impl<'w, 's, D: QueryData, F: QueryFilter, I: Iterator<Item: EntityEquivalent>>
2357 QueryManyMatchedIter<QueryManyIter<'w, 's, D, F, I>>
2358{
2359 /// Get the next result from the query
2360 #[inline(always)]
2361 pub fn fetch_next(&mut self) -> Option<D::Item<'_, 's>> {
2362 loop {
2363 // SAFETY:
2364 // We are limiting the returned reference to self,
2365 // making sure this method cannot be called multiple times without getting rid
2366 // of any previously returned unique references first, thus preventing aliasing.
2367 let next_option_result = unsafe {
2368 self.0
2369 .fetch_next_aliased_unchecked()
2370 .map(|result| result.map(D::shrink))
2371 };
2372 if let Ok(next) = next_option_result? {
2373 return Some(next);
2374 }
2375 }
2376 }
2377}
2378
2379impl<'w, 's, D: QueryData, F: QueryFilter, I: DoubleEndedIterator<Item: EntityEquivalent>>
2380 QueryManyMatchedIter<QueryManyIter<'w, 's, D, F, I>>
2381{
2382 /// Get the next result from the back of the query
2383 #[inline(always)]
2384 pub fn fetch_next_back(&mut self) -> Option<D::Item<'_, 's>> {
2385 loop {
2386 // SAFETY:
2387 // We are limiting the returned reference to self,
2388 // making sure this method cannot be called multiple times without getting rid
2389 // of any previously returned unique references first, thus preventing aliasing.
2390 let next_option_result = unsafe {
2391 self.0
2392 .fetch_next_back_aliased_unchecked()
2393 .map(|result| result.map(D::shrink))
2394 };
2395 if let Ok(next) = next_option_result? {
2396 return Some(next);
2397 }
2398 }
2399 }
2400}
2401
2402impl<T, E: Debug, I: Iterator<Item = Result<T, E>>> Iterator for QueryManyMatchedIter<I> {
2403 type Item = T;
2404
2405 #[inline(always)]
2406 fn next(&mut self) -> Option<Self::Item> {
2407 loop {
2408 if let Ok(next) = self.0.next()? {
2409 return Some(next);
2410 }
2411 }
2412 }
2413
2414 fn size_hint(&self) -> (usize, Option<usize>) {
2415 let (_, max) = self.0.size_hint();
2416 (0, max)
2417 }
2418}
2419
2420impl<T, E: Debug, I: DoubleEndedIterator<Item = Result<T, E>>> DoubleEndedIterator
2421 for QueryManyMatchedIter<I>
2422{
2423 #[inline(always)]
2424 fn next_back(&mut self) -> Option<Self::Item> {
2425 loop {
2426 if let Ok(next) = self.0.next_back()? {
2427 return Some(next);
2428 }
2429 }
2430 }
2431}
2432
2433impl<T, E: Debug, I: FusedIterator<Item = Result<T, E>>> FusedIterator for QueryManyMatchedIter<I> {}
2434
2435// SAFETY: Fetching unique entities maintains uniqueness.
2436unsafe impl<'w, 's, F: QueryFilter, I: EntitySetIterator> EntitySetIterator
2437 for QueryManyMatchedIter<QueryManyIter<'w, 's, Entity, F, I>>
2438{
2439}
2440
2441// SAFETY: Fetching unique entities maintains uniqueness.
2442unsafe impl<'w, 's, F: QueryFilter, I: EntitySetIterator> EntitySetIterator
2443 for QueryManyMatchedIter<QueryManyUniqueIter<'w, 's, Entity, F, I>>
2444{
2445}
2446
2447impl<I> Debug for QueryManyMatchedIter<I> {
2448 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
2449 f.debug_struct("QueryManyMatchedIter").finish()
2450 }
2451}
2452
2453/// An [`Iterator`] over the query items generated from an iterator of unique [`Entity`]s.
2454///
2455/// Items are returned in the order of the provided iterator.
2456/// Entities that don't match the query are skipped.
2457///
2458/// In contrast with [`QueryManyIter`], this allows for mutable iteration without a [`fetch_next`] method.
2459///
2460/// This struct is created by the [`iter_many_unique`] and [`iter_many_unique_mut`] methods on [`Query`].
2461///
2462/// [`fetch_next`]: QueryManyIter::fetch_next
2463/// [`iter_many_unique`]: crate::system::Query::iter_many
2464/// [`iter_many_unique_mut`]: crate::system::Query::iter_many_mut
2465/// [`Query`]: crate::system::Query
2466pub struct QueryManyUniqueIter<'w, 's, D: IterQueryData, F: QueryFilter, I: EntitySetIterator>(
2467 QueryManyIter<'w, 's, D, F, I>,
2468);
2469
2470impl<'w, 's, D: IterQueryData, F: QueryFilter, I: EntitySetIterator>
2471 QueryManyUniqueIter<'w, 's, D, F, I>
2472{
2473 /// # Safety
2474 /// - `world` must have permission to access any of the components registered in `query_state`.
2475 /// - `world` must be the same one used to initialize `query_state`.
2476 pub(crate) unsafe fn new<EntityList: EntitySet<IntoIter = I>>(
2477 world: UnsafeWorldCell<'w>,
2478 query_state: &'s QueryState<D, F>,
2479 entity_list: EntityList,
2480 last_run: Tick,
2481 this_run: Tick,
2482 ) -> QueryManyUniqueIter<'w, 's, D, F, I> {
2483 QueryManyUniqueIter(QueryManyIter::new(
2484 world,
2485 query_state,
2486 entity_list,
2487 last_run,
2488 this_run,
2489 ))
2490 }
2491
2492 /// Creates an iterator which calls [`Result::unwrap`] on each element.
2493 #[inline(always)]
2494 pub fn unwrapped(self) -> QueryManyUnwrappedIter<Self> {
2495 QueryManyUnwrappedIter(self)
2496 }
2497
2498 /// Creates an iterator which skips entities that don't match the query.
2499 #[inline(always)]
2500 pub fn matched(self) -> QueryManyMatchedIter<Self> {
2501 QueryManyMatchedIter(self)
2502 }
2503}
2504
2505impl<'w, 's, D: IterQueryData, F: QueryFilter, I: EntitySetIterator> Iterator
2506 for QueryManyUniqueIter<'w, 's, D, F, I>
2507{
2508 type Item = Result<D::Item<'w, 's>, QueryEntityError>;
2509
2510 #[inline(always)]
2511 fn next(&mut self) -> Option<Self::Item> {
2512 // SAFETY:
2513 // - Entities are guaranteed to be unique, thus do not alias.
2514 // - `D: IterQueryData`
2515 unsafe { self.0.fetch_next_aliased_unchecked() }
2516 }
2517
2518 fn size_hint(&self) -> (usize, Option<usize>) {
2519 let (_, max_size) = self.0.entity_iter.size_hint();
2520 (0, max_size)
2521 }
2522}
2523
2524// This is correct as [`QueryManyIter`] always returns `None` once exhausted.
2525impl<'w, 's, D: IterQueryData, F: QueryFilter, I: EntitySetIterator> FusedIterator
2526 for QueryManyUniqueIter<'w, 's, D, F, I>
2527{
2528}
2529
2530impl<'w, 's, D: IterQueryData, F: QueryFilter, I: EntitySetIterator> Debug
2531 for QueryManyUniqueIter<'w, 's, D, F, I>
2532{
2533 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
2534 f.debug_struct("QueryManyUniqueIter").finish()
2535 }
2536}
2537
2538/// An [`Iterator`] over sorted query results of a [`QueryManyIter`].
2539///
2540/// This struct is created by the [`sort`](QueryManyIter), [`sort_unstable`](QueryManyIter),
2541/// [`sort_by`](QueryManyIter), [`sort_unstable_by`](QueryManyIter), [`sort_by_key`](QueryManyIter),
2542/// [`sort_unstable_by_key`](QueryManyIter), and [`sort_by_cached_key`](QueryManyIter) methods of [`QueryManyIter`].
2543pub struct QuerySortedManyIter<
2544 'w,
2545 's,
2546 D: QueryData,
2547 F: QueryFilter,
2548 I: Iterator<Item = Result<Entity, QueryEntityError>>,
2549> {
2550 entity_iter: I,
2551 entities: &'w Entities,
2552 tables: &'w Tables,
2553 archetypes: &'w Archetypes,
2554 fetch: D::Fetch<'w>,
2555 query_state: &'s QueryState<D, F>,
2556}
2557
2558impl<
2559 'w,
2560 's,
2561 D: QueryData,
2562 F: QueryFilter,
2563 I: Iterator<Item = Result<Entity, QueryEntityError>>,
2564 > QuerySortedManyIter<'w, 's, D, F, I>
2565{
2566 /// # Safety
2567 /// - `world` must have permission to access any of the components registered in `query_state`.
2568 /// - `world` must be the same one used to initialize `query_state`.
2569 /// - `entity_list` must only contain unique entities or be empty.
2570 pub(crate) unsafe fn new<EntityList: IntoIterator<IntoIter = I>>(
2571 world: UnsafeWorldCell<'w>,
2572 query_state: &'s QueryState<D, F>,
2573 entity_list: EntityList,
2574 last_run: Tick,
2575 this_run: Tick,
2576 ) -> QuerySortedManyIter<'w, 's, D, F, I> {
2577 let fetch = D::init_fetch(world, &query_state.fetch_state, last_run, this_run);
2578 QuerySortedManyIter {
2579 query_state,
2580 entities: world.entities(),
2581 archetypes: world.archetypes(),
2582 // SAFETY: We only access table data that has been registered in `query_state`.
2583 // This means `world` has permission to access the data we use.
2584 tables: &world.storages().tables,
2585 fetch,
2586 entity_iter: entity_list.into_iter(),
2587 }
2588 }
2589
2590 /// # Safety
2591 ///
2592 /// - `entity_result` must stem from `self.entity_iter`
2593 /// - If `D` does not impl `ReadOnlyQueryData`, then there must not be any other `Item`s alive for the current entity
2594 /// - If `D` does not impl `IterQueryData`, then there must not be any other `Item`s alive for *any* entity
2595 #[inline(always)]
2596 unsafe fn fetch_next_aliased_unchecked(
2597 &mut self,
2598 entity_result: Result<Entity, QueryEntityError>,
2599 ) -> Result<D::Item<'w, 's>, QueryEntityError> {
2600 let entity = entity_result?;
2601 let (location, archetype, table);
2602 // SAFETY:
2603 // `tables` and `archetypes` belong to the same world that the [`QueryIter`]
2604 // was initialized for.
2605 unsafe {
2606 location = self.entities.get_spawned(entity).debug_checked_unwrap();
2607 archetype = self
2608 .archetypes
2609 .get(location.archetype_id)
2610 .debug_checked_unwrap();
2611 table = self.tables.get(location.table_id).debug_checked_unwrap();
2612 }
2613
2614 // SAFETY: `archetype` is from the world that `fetch` was created for,
2615 // `fetch_state` is the state that `fetch` was initialized with
2616 unsafe {
2617 D::set_archetype(
2618 &mut self.fetch,
2619 &self.query_state.fetch_state,
2620 archetype,
2621 table,
2622 );
2623 }
2624
2625 // The entity list has already been filtered by the query lens, so we forego filtering here.
2626 // SAFETY:
2627 // - set_archetype was called prior, `location.archetype_row` is an archetype index in range of the current archetype
2628 // - Caller ensures there are no conflicting items alive
2629 unsafe {
2630 D::fetch(
2631 &self.query_state.fetch_state,
2632 &mut self.fetch,
2633 entity,
2634 location.table_row,
2635 )
2636 }
2637 .ok_or(QueryEntityError::QueryDoesNotMatch(entity, archetype.id()))
2638 }
2639
2640 /// Get the next result from the query
2641 #[inline(always)]
2642 pub fn fetch_next(&mut self) -> Option<Result<D::Item<'_, 's>, QueryEntityError>> {
2643 let entity_result = self.entity_iter.next()?;
2644 // SAFETY:
2645 // We are limiting the returned reference to self,
2646 // making sure this method cannot be called multiple times without getting rid
2647 // of any previously returned unique references first, thus preventing aliasing.
2648 // `entity_result` is passed from `entity_iter` the first time.
2649 let next = unsafe { self.fetch_next_aliased_unchecked(entity_result) };
2650 Some(next.map(D::shrink))
2651 }
2652}
2653
2654impl<
2655 'w,
2656 's,
2657 D: QueryData,
2658 F: QueryFilter,
2659 I: DoubleEndedIterator<Item = Result<Entity, QueryEntityError>>,
2660 > QuerySortedManyIter<'w, 's, D, F, I>
2661{
2662 /// Get the next result from the query
2663 #[inline(always)]
2664 pub fn fetch_next_back(&mut self) -> Option<Result<D::Item<'_, 's>, QueryEntityError>> {
2665 let entity_result = self.entity_iter.next_back()?;
2666 // SAFETY:
2667 // We are limiting the returned reference to self,
2668 // making sure this method cannot be called multiple times without getting rid
2669 // of any previously returned unique references first, thus preventing aliasing.
2670 // `entity_result` is passed from `entity_iter` the first time.
2671 let next = unsafe { self.fetch_next_aliased_unchecked(entity_result) };
2672 Some(next.map(D::shrink))
2673 }
2674}
2675
2676impl<
2677 'w,
2678 's,
2679 D: ReadOnlyQueryData,
2680 F: QueryFilter,
2681 I: Iterator<Item = Result<Entity, QueryEntityError>>,
2682 > Iterator for QuerySortedManyIter<'w, 's, D, F, I>
2683{
2684 type Item = Result<D::Item<'w, 's>, QueryEntityError>;
2685
2686 #[inline(always)]
2687 fn next(&mut self) -> Option<Self::Item> {
2688 let entity_result = self.entity_iter.next()?;
2689 // SAFETY:
2690 // We have collected the entity_iter once to drop all internal lens query item
2691 // references.
2692 // We are limiting the returned reference to self,
2693 // making sure this method cannot be called multiple times without getting rid
2694 // of any previously returned unique references first, thus preventing aliasing.
2695 // `entity_result` is passed from `entity_iter` the first time.
2696 let next = unsafe { self.fetch_next_aliased_unchecked(entity_result) };
2697 Some(next.map(D::shrink))
2698 }
2699
2700 fn size_hint(&self) -> (usize, Option<usize>) {
2701 self.entity_iter.size_hint()
2702 }
2703}
2704
2705impl<
2706 'w,
2707 's,
2708 D: ReadOnlyQueryData,
2709 F: QueryFilter,
2710 I: DoubleEndedIterator<Item = Result<Entity, QueryEntityError>>,
2711 > DoubleEndedIterator for QuerySortedManyIter<'w, 's, D, F, I>
2712{
2713 #[inline(always)]
2714 fn next_back(&mut self) -> Option<Self::Item> {
2715 let entity_result = self.entity_iter.next_back()?;
2716 // SAFETY:
2717 // We have collected the entity_iter once to drop all internal lens query item
2718 // references.
2719 // We are limiting the returned reference to self,
2720 // making sure this method cannot be called multiple times without getting rid
2721 // of any previously returned unique references first, thus preventing aliasing.
2722 // `entity_result` is passed from `entity_iter` the first time.
2723 let next = unsafe { self.fetch_next_aliased_unchecked(entity_result) };
2724 Some(next.map(D::shrink))
2725 }
2726}
2727
2728impl<
2729 D: ReadOnlyQueryData,
2730 F: QueryFilter,
2731 I: ExactSizeIterator<Item = Result<Entity, QueryEntityError>>,
2732 > ExactSizeIterator for QuerySortedManyIter<'_, '_, D, F, I>
2733{
2734}
2735
2736impl<
2737 'w,
2738 's,
2739 D: QueryData,
2740 F: QueryFilter,
2741 I: Iterator<Item = Result<Entity, QueryEntityError>>,
2742 > Debug for QuerySortedManyIter<'w, 's, D, F, I>
2743{
2744 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
2745 f.debug_struct("QuerySortedManyIter").finish()
2746 }
2747}
2748
2749/// An iterator over `K`-sized combinations of query items without repetition.
2750///
2751/// A combination is an arrangement of a collection of items where order does not matter.
2752///
2753/// `K` is the number of items that make up each subset, and the number of items returned by the iterator.
2754/// `N` is the number of total entities output by the query.
2755///
2756/// For example, given the list [1, 2, 3, 4], where `K` is 2, the combinations without repeats are
2757/// [1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4].
2758/// And in this case, `N` would be defined as 4 since the size of the input list is 4.
2759///
2760/// The number of combinations depend on how `K` relates to the number of entities matching the [`Query`]:
2761/// - if `K = N`, only one combination exists,
2762/// - if `K < N`, there are <sub>N</sub>C<sub>K</sub> combinations (see the [performance section] of `Query`),
2763/// - if `K > N`, there are no combinations.
2764///
2765/// The output combination is not guaranteed to have any order of iteration.
2766///
2767/// # Usage
2768///
2769/// This type is returned by calling [`Query::iter_combinations`] or [`Query::iter_combinations_mut`].
2770///
2771/// It implements [`Iterator`] only if it iterates over read-only query items ([learn more]).
2772///
2773/// In the case of mutable query items, it can be iterated by calling [`fetch_next`] in a `while let` loop.
2774///
2775/// # Examples
2776///
2777/// The following example shows how to traverse the iterator when the query items are read-only.
2778///
2779/// ```
2780/// # use bevy_ecs::prelude::*;
2781/// # #[derive(Component)]
2782/// # struct ComponentA;
2783/// #
2784/// fn some_system(query: Query<&ComponentA>) {
2785/// for [a1, a2] in query.iter_combinations() {
2786/// // ...
2787/// }
2788/// }
2789/// ```
2790///
2791/// The following example shows how `fetch_next` should be called with a `while let` loop to traverse the iterator when the query items are mutable.
2792///
2793/// ```
2794/// # use bevy_ecs::prelude::*;
2795/// # #[derive(Component)]
2796/// # struct ComponentA;
2797/// #
2798/// fn some_system(mut query: Query<&mut ComponentA>) {
2799/// let mut combinations = query.iter_combinations_mut();
2800/// while let Some([a1, a2]) = combinations.fetch_next() {
2801/// // ...
2802/// }
2803/// }
2804/// ```
2805///
2806/// [`fetch_next`]: Self::fetch_next
2807/// [learn more]: Self#impl-Iterator
2808/// [performance section]: crate::system::Query#performance
2809/// [`Query`]: crate::system::Query
2810/// [`Query::iter_combinations`]: crate::system::Query::iter_combinations
2811/// [`Query::iter_combinations_mut`]: crate::system::Query::iter_combinations_mut
2812pub struct QueryCombinationIter<'w, 's, D: IterQueryData, F: QueryFilter, const K: usize> {
2813 tables: &'w Tables,
2814 archetypes: &'w Archetypes,
2815 query_state: &'s QueryState<D, F>,
2816 cursors: [QueryIterationCursor<'w, 's, D, F>; K],
2817}
2818
2819impl<'w, 's, D: IterQueryData, F: QueryFilter, const K: usize>
2820 QueryCombinationIter<'w, 's, D, F, K>
2821{
2822 /// # Safety
2823 /// - `world` must have permission to access any of the components registered in `query_state`.
2824 /// - `world` must be the same one used to initialize `query_state`.
2825 pub(crate) unsafe fn new(
2826 world: UnsafeWorldCell<'w>,
2827 query_state: &'s QueryState<D, F>,
2828 last_run: Tick,
2829 this_run: Tick,
2830 ) -> Self {
2831 assert!(K != 0, "K should not equal to zero");
2832 // Initialize array with cursors.
2833 // There is no FromIterator on arrays, so instead initialize it manually with MaybeUninit
2834
2835 let mut array: MaybeUninit<[QueryIterationCursor<'w, 's, D, F>; K]> = MaybeUninit::uninit();
2836 let ptr = array
2837 .as_mut_ptr()
2838 .cast::<QueryIterationCursor<'w, 's, D, F>>();
2839 ptr.write(QueryIterationCursor::init(
2840 world,
2841 query_state,
2842 last_run,
2843 this_run,
2844 ));
2845 for slot in (1..K).map(|offset| ptr.add(offset)) {
2846 slot.write(QueryIterationCursor::init_empty(
2847 world,
2848 query_state,
2849 last_run,
2850 this_run,
2851 ));
2852 }
2853
2854 QueryCombinationIter {
2855 query_state,
2856 // SAFETY: We only access table data that has been registered in `query_state`.
2857 tables: unsafe { &world.storages().tables },
2858 archetypes: world.archetypes(),
2859 cursors: array.assume_init(),
2860 }
2861 }
2862
2863 /// # Safety
2864 /// The lifetime here is not restrictive enough for Fetch with &mut access,
2865 /// as calling `fetch_next_aliased_unchecked` multiple times can produce multiple
2866 /// references to the same component, leading to unique reference aliasing.
2867 /// .
2868 /// It is always safe for shared access.
2869 #[inline]
2870 unsafe fn fetch_next_aliased_unchecked(&mut self) -> Option<[D::Item<'w, 's>; K]> {
2871 // PERF: can speed up the following code using `cursor.remaining()` instead of `next_item.is_none()`
2872 // when D::IS_ARCHETYPAL && F::IS_ARCHETYPAL
2873 //
2874 // let `i` be the index of `c`, the last cursor in `self.cursors` that
2875 // returns `K-i` or more elements.
2876 // Make cursor in index `j` for all `j` in `[i, K)` a copy of `c` advanced `j-i+1` times.
2877 // If no such `c` exists, return `None`
2878 'outer: for i in (0..K).rev() {
2879 match self.cursors[i].next(self.tables, self.archetypes, self.query_state) {
2880 Some(_) => {
2881 for j in (i + 1)..K {
2882 self.cursors[j] = self.cursors[j - 1].clone();
2883 match self.cursors[j].next(self.tables, self.archetypes, self.query_state) {
2884 Some(_) => {}
2885 None if i > 0 => continue 'outer,
2886 None => return None,
2887 }
2888 }
2889 break;
2890 }
2891 None if i > 0 => continue,
2892 None => return None,
2893 }
2894 }
2895
2896 let mut values = MaybeUninit::<[D::Item<'w, 's>; K]>::uninit();
2897
2898 let ptr = values.as_mut_ptr().cast::<D::Item<'w, 's>>();
2899 for (offset, cursor) in self.cursors.iter_mut().enumerate() {
2900 ptr.add(offset)
2901 .write(cursor.peek_last(self.query_state).unwrap());
2902 }
2903
2904 Some(values.assume_init())
2905 }
2906
2907 /// Get next combination of queried components
2908 #[inline]
2909 pub fn fetch_next(&mut self) -> Option<[D::Item<'_, 's>; K]> {
2910 // SAFETY: we are limiting the returned reference to self,
2911 // making sure this method cannot be called multiple times without getting rid
2912 // of any previously returned unique references first, thus preventing aliasing.
2913 unsafe {
2914 self.fetch_next_aliased_unchecked()
2915 .map(|array| array.map(D::shrink))
2916 }
2917 }
2918}
2919
2920// Iterator type is intentionally implemented only for read-only access.
2921// Doing so for mutable references would be unsound, because calling `next`
2922// multiple times would allow multiple owned references to the same data to exist.
2923impl<'w, 's, D: ReadOnlyQueryData, F: QueryFilter, const K: usize> Iterator
2924 for QueryCombinationIter<'w, 's, D, F, K>
2925{
2926 type Item = [D::Item<'w, 's>; K];
2927
2928 #[inline]
2929 fn next(&mut self) -> Option<Self::Item> {
2930 // Safety: it is safe to alias for ReadOnlyWorldQuery
2931 unsafe { QueryCombinationIter::fetch_next_aliased_unchecked(self) }
2932 }
2933
2934 fn size_hint(&self) -> (usize, Option<usize>) {
2935 // binomial coefficient: (n ; k) = n! / k!(n-k)! = (n*n-1*...*n-k+1) / k!
2936 // See https://en.wikipedia.org/wiki/Binomial_coefficient
2937 // See https://blog.plover.com/math/choose.html for implementation
2938 // It was chosen to reduce overflow potential.
2939 fn choose(n: usize, k: usize) -> Option<usize> {
2940 if k > n || n == 0 {
2941 return Some(0);
2942 }
2943 let k = k.min(n - k);
2944 let ks = 1..=k;
2945 let ns = (n - k + 1..=n).rev();
2946 ks.zip(ns)
2947 .try_fold(1_usize, |acc, (k, n)| Some(acc.checked_mul(n)? / k))
2948 }
2949 // sum_i=0..k choose(cursors[i].remaining, k-i)
2950 let max_combinations = self
2951 .cursors
2952 .iter()
2953 .enumerate()
2954 .try_fold(0, |acc, (i, cursor)| {
2955 let n = cursor.max_remaining(self.tables, self.archetypes);
2956 Some(acc + choose(n as usize, K - i)?)
2957 });
2958
2959 let archetype_query = D::IS_ARCHETYPAL && F::IS_ARCHETYPAL;
2960 let known_max = max_combinations.unwrap_or(usize::MAX);
2961 let min_combinations = if archetype_query { known_max } else { 0 };
2962 (min_combinations, max_combinations)
2963 }
2964}
2965
2966impl<'w, 's, D: ArchetypeQueryData + IterQueryData, F: ArchetypeFilter> ExactSizeIterator
2967 for QueryIter<'w, 's, D, F>
2968{
2969 fn len(&self) -> usize {
2970 self.size_hint().0
2971 }
2972}
2973
2974// This is correct as [`QueryCombinationIter`] always returns `None` once exhausted.
2975impl<'w, 's, D: ReadOnlyQueryData, F: QueryFilter, const K: usize> FusedIterator
2976 for QueryCombinationIter<'w, 's, D, F, K>
2977{
2978}
2979
2980impl<'w, 's, D: IterQueryData, F: QueryFilter, const K: usize> Debug
2981 for QueryCombinationIter<'w, 's, D, F, K>
2982{
2983 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
2984 f.debug_struct("QueryCombinationIter").finish()
2985 }
2986}
2987
2988struct QueryIterationCursor<'w, 's, D: QueryData, F: QueryFilter> {
2989 // whether the query iteration is dense or not. Mirrors QueryState's `is_dense` field.
2990 is_dense: bool,
2991 storage_id_iter: core::slice::Iter<'s, StorageId>,
2992 table_entities: &'w [Entity],
2993 archetype_entities: &'w [ArchetypeEntity],
2994 fetch: D::Fetch<'w>,
2995 filter: F::Fetch<'w>,
2996 // length of the table or length of the archetype, depending on whether both `D`'s and `F`'s fetches are dense
2997 current_len: u32,
2998 // either table row or archetype index, depending on whether both `D`'s and `F`'s fetches are dense
2999 current_row: u32,
3000}
3001
3002impl<D: QueryData, F: QueryFilter> Clone for QueryIterationCursor<'_, '_, D, F> {
3003 fn clone(&self) -> Self {
3004 Self {
3005 is_dense: self.is_dense,
3006 storage_id_iter: self.storage_id_iter.clone(),
3007 table_entities: self.table_entities,
3008 archetype_entities: self.archetype_entities,
3009 fetch: self.fetch.clone(),
3010 filter: self.filter.clone(),
3011 current_len: self.current_len,
3012 current_row: self.current_row,
3013 }
3014 }
3015}
3016
3017impl<'w, 's, D: QueryData, F: QueryFilter> QueryIterationCursor<'w, 's, D, F> {
3018 /// # Safety
3019 /// - `world` must have permission to access any of the components registered in `query_state`.
3020 /// - `world` must be the same one used to initialize `query_state`.
3021 unsafe fn init_empty(
3022 world: UnsafeWorldCell<'w>,
3023 query_state: &'s QueryState<D, F>,
3024 last_run: Tick,
3025 this_run: Tick,
3026 ) -> Self {
3027 QueryIterationCursor {
3028 storage_id_iter: [].iter(),
3029 ..Self::init(world, query_state, last_run, this_run)
3030 }
3031 }
3032
3033 /// # Safety
3034 /// - `world` must have permission to access any of the components registered in `query_state`.
3035 /// - `world` must be the same one used to initialize `query_state`.
3036 unsafe fn init(
3037 world: UnsafeWorldCell<'w>,
3038 query_state: &'s QueryState<D, F>,
3039 last_run: Tick,
3040 this_run: Tick,
3041 ) -> Self {
3042 let fetch = D::init_fetch(world, &query_state.fetch_state, last_run, this_run);
3043 let filter = F::init_fetch(world, &query_state.filter_state, last_run, this_run);
3044 QueryIterationCursor {
3045 fetch,
3046 filter,
3047 table_entities: &[],
3048 archetype_entities: &[],
3049 storage_id_iter: query_state.matched_storage_ids.iter(),
3050 is_dense: query_state.is_dense,
3051 current_len: 0,
3052 current_row: 0,
3053 }
3054 }
3055
3056 fn reborrow(&mut self) -> QueryIterationCursor<'_, 's, D, F> {
3057 QueryIterationCursor {
3058 is_dense: self.is_dense,
3059 fetch: D::shrink_fetch(self.fetch.clone()),
3060 filter: F::shrink_fetch(self.filter.clone()),
3061 table_entities: self.table_entities,
3062 archetype_entities: self.archetype_entities,
3063 storage_id_iter: self.storage_id_iter.clone(),
3064 current_len: self.current_len,
3065 current_row: self.current_row,
3066 }
3067 }
3068
3069 /// Retrieve item returned from most recent `next` call again.
3070 ///
3071 /// # Safety
3072 /// The result of `next` and any previous calls to `peek_last` with this row must have been
3073 /// dropped to prevent aliasing mutable references.
3074 #[inline]
3075 unsafe fn peek_last(&mut self, query_state: &'s QueryState<D, F>) -> Option<D::Item<'w, 's>>
3076 where
3077 D: IterQueryData,
3078 {
3079 if self.current_row > 0 {
3080 let index = self.current_row - 1;
3081 if self.is_dense {
3082 // SAFETY: This must have been called previously in `next` as `current_row > 0`
3083 let entity = unsafe { self.table_entities.get_unchecked(index as usize) };
3084 // SAFETY:
3085 // - `set_table` must have been called previously either in `next` or before it.
3086 // - `*entity` and `index` are in the current table.
3087 // - `D: IterQueryData`
3088 unsafe {
3089 D::fetch(
3090 &query_state.fetch_state,
3091 &mut self.fetch,
3092 *entity,
3093 // SAFETY: This is from an exclusive range, so it can't be max.
3094 TableRow::new(NonMaxU32::new_unchecked(index)),
3095 )
3096 }
3097 } else {
3098 // SAFETY: This must have been called previously in `next` as `current_row > 0`
3099 let archetype_entity =
3100 unsafe { self.archetype_entities.get_unchecked(index as usize) };
3101 // SAFETY:
3102 // - `set_archetype` must have been called previously either in `next` or before it.
3103 // - `archetype_entity.id()` and `archetype_entity.table_row()` are in the current archetype.
3104 // - `D: IterQueryData`
3105 unsafe {
3106 D::fetch(
3107 &query_state.fetch_state,
3108 &mut self.fetch,
3109 archetype_entity.id(),
3110 archetype_entity.table_row(),
3111 )
3112 }
3113 }
3114 } else {
3115 None
3116 }
3117 }
3118
3119 /// How many values will this cursor return at most?
3120 ///
3121 /// Note that if `D::IS_ARCHETYPAL && F::IS_ARCHETYPAL`, the return value
3122 /// will be **the exact count of remaining values**.
3123 fn max_remaining(&self, tables: &'w Tables, archetypes: &'w Archetypes) -> u32 {
3124 let ids = self.storage_id_iter.clone();
3125 let remaining_matched: u32 = if self.is_dense {
3126 // SAFETY: The if check ensures that storage_id_iter stores TableIds
3127 unsafe { ids.map(|id| tables[id.table_id].entity_count()).sum() }
3128 } else {
3129 // SAFETY: The if check ensures that storage_id_iter stores ArchetypeIds
3130 unsafe { ids.map(|id| archetypes[id.archetype_id].len()).sum() }
3131 };
3132 remaining_matched + self.current_len - self.current_row
3133 }
3134
3135 // NOTE: If you are changing query iteration code, remember to update the following places, where relevant:
3136 // QueryIter, QueryIterationCursor, QuerySortedIter, QueryManyIter, QuerySortedManyIter, QueryCombinationIter,
3137 // QueryState::par_fold_init_unchecked_manual, QueryState::par_many_fold_init_unchecked_manual,
3138 // QueryState::par_many_unique_fold_init_unchecked_manual, QueryContiguousIter::next
3139 /// # Safety
3140 /// - `tables` and `archetypes` must belong to the same world that the [`QueryIterationCursor`]
3141 /// was initialized for.
3142 /// - `query_state` must be the same [`QueryState`] that was passed to `init` or `init_empty`.
3143 /// - If `D` does not impl `ReadOnlyQueryData`, then there must not be any other `Item`s alive for the current entity
3144 /// - If `D` does not impl `IterQueryData`, then there must not be any other `Item`s alive for *any* entity
3145 #[inline(always)]
3146 unsafe fn next(
3147 &mut self,
3148 tables: &'w Tables,
3149 archetypes: &'w Archetypes,
3150 query_state: &'s QueryState<D, F>,
3151 ) -> Option<D::Item<'w, 's>> {
3152 if self.is_dense {
3153 // NOTE: if you are changing this branch you would probably have to change
3154 // QueryContiguousIter::next as well
3155 loop {
3156 // we are on the beginning of the query, or finished processing a table, so skip to the next
3157 if self.current_row == self.current_len {
3158 let table_id = self.storage_id_iter.next()?.table_id;
3159 let table = tables.get(table_id).debug_checked_unwrap();
3160 if table.is_empty() {
3161 continue;
3162 }
3163 // SAFETY: `table` is from the world that `fetch/filter` were created for,
3164 // `fetch_state`/`filter_state` are the states that `fetch/filter` were initialized with
3165 unsafe {
3166 D::set_table(&mut self.fetch, &query_state.fetch_state, table);
3167 F::set_table(&mut self.filter, &query_state.filter_state, table);
3168 }
3169 self.table_entities = table.entities();
3170 self.current_len = table.entity_count();
3171 self.current_row = 0;
3172 }
3173
3174 // SAFETY: set_table was called prior.
3175 // `current_row` is a table row in range of the current table, because if it was not, then the above would have been executed.
3176 let entity =
3177 unsafe { self.table_entities.get_unchecked(self.current_row as usize) };
3178 // SAFETY: The row is less than the u32 len, so it must not be max.
3179 let row = unsafe { TableRow::new(NonMaxU32::new_unchecked(self.current_row)) };
3180 self.current_row += 1;
3181
3182 if !F::filter_fetch(&query_state.filter_state, &mut self.filter, *entity, row) {
3183 continue;
3184 }
3185
3186 // SAFETY:
3187 // - set_table was called prior.
3188 // - `current_row` must be a table row in range of the current table,
3189 // because if it was not, then the above would have been executed.
3190 // - fetch is only called once for each `entity`.
3191 // - caller ensures no conflicting `Item`s are alive
3192 let item =
3193 unsafe { D::fetch(&query_state.fetch_state, &mut self.fetch, *entity, row) };
3194 if let Some(item) = item {
3195 return Some(item);
3196 }
3197 }
3198 } else {
3199 loop {
3200 if self.current_row == self.current_len {
3201 let archetype_id = self.storage_id_iter.next()?.archetype_id;
3202 let archetype = archetypes.get(archetype_id).debug_checked_unwrap();
3203 if archetype.is_empty() {
3204 continue;
3205 }
3206 let table = tables.get(archetype.table_id()).debug_checked_unwrap();
3207 // SAFETY: `archetype` and `tables` are from the world that `fetch/filter` were created for,
3208 // `fetch_state`/`filter_state` are the states that `fetch/filter` were initialized with
3209 unsafe {
3210 D::set_archetype(
3211 &mut self.fetch,
3212 &query_state.fetch_state,
3213 archetype,
3214 table,
3215 );
3216 F::set_archetype(
3217 &mut self.filter,
3218 &query_state.filter_state,
3219 archetype,
3220 table,
3221 );
3222 }
3223 self.archetype_entities = archetype.entities();
3224 self.current_len = archetype.len();
3225 self.current_row = 0;
3226 }
3227
3228 // SAFETY: set_archetype was called prior.
3229 // `current_row` is an archetype index row in range of the current archetype, because if it was not, then the if above would have been executed.
3230 let archetype_entity = unsafe {
3231 self.archetype_entities
3232 .get_unchecked(self.current_row as usize)
3233 };
3234 self.current_row += 1;
3235
3236 if !F::filter_fetch(
3237 &query_state.filter_state,
3238 &mut self.filter,
3239 archetype_entity.id(),
3240 archetype_entity.table_row(),
3241 ) {
3242 continue;
3243 }
3244
3245 // SAFETY:
3246 // - set_archetype was called prior.
3247 // - `current_row` must be an archetype index row in range of the current archetype,
3248 // because if it was not, then the if above would have been executed.
3249 // - fetch is only called once for each `archetype_entity`.
3250 // - caller ensures no conflicting `Item`s are alive
3251 let item = unsafe {
3252 D::fetch(
3253 &query_state.fetch_state,
3254 &mut self.fetch,
3255 archetype_entity.id(),
3256 archetype_entity.table_row(),
3257 )
3258 };
3259 if let Some(item) = item {
3260 return Some(item);
3261 }
3262 }
3263 }
3264 }
3265}
3266
3267// A wrapper struct that gives its data a neutral ordering.
3268#[derive(Copy, Clone)]
3269struct NeutralOrd<T>(T);
3270
3271impl<T> PartialEq for NeutralOrd<T> {
3272 fn eq(&self, _other: &Self) -> bool {
3273 true
3274 }
3275}
3276
3277impl<T> Eq for NeutralOrd<T> {}
3278
3279#[expect(
3280 clippy::non_canonical_partial_ord_impl,
3281 reason = "`PartialOrd` and `Ord` on this struct must only ever return `Ordering::Equal`, so we prefer clarity"
3282)]
3283impl<T> PartialOrd for NeutralOrd<T> {
3284 fn partial_cmp(&self, _other: &Self) -> Option<Ordering> {
3285 Some(Ordering::Equal)
3286 }
3287}
3288
3289impl<T> Ord for NeutralOrd<T> {
3290 fn cmp(&self, _other: &Self) -> Ordering {
3291 Ordering::Equal
3292 }
3293}
3294
3295#[cfg(test)]
3296#[expect(clippy::print_stdout, reason = "Allowed in tests.")]
3297mod tests {
3298 use alloc::vec::Vec;
3299 use std::println;
3300
3301 use crate::component::Component;
3302 use crate::entity::Entity;
3303 use crate::prelude::{With, World};
3304
3305 #[derive(Component, Debug, PartialEq, PartialOrd, Clone, Copy)]
3306 struct A(f32);
3307 #[derive(Component, Debug, Eq, PartialEq, Clone, Copy)]
3308 #[component(storage = "SparseSet")]
3309 struct Sparse(usize);
3310
3311 #[derive(Component)]
3312 struct Marker;
3313
3314 #[test]
3315 #[cfg_attr(miri, ignore = "This test takes ~70s on CI")]
3316 fn query_iter_sorts() {
3317 let mut world = World::new();
3318 for i in 0..100 {
3319 world.spawn((A(i as f32), Marker));
3320 world.spawn((A(i as f32), Sparse(i), Marker));
3321 world.spawn((Sparse(i), Marker));
3322 }
3323
3324 let mut query = world.query_filtered::<Entity, With<Marker>>();
3325
3326 let sort = query.iter(&world).sort::<Entity>().collect::<Vec<_>>();
3327 assert_eq!(sort.len(), 300);
3328
3329 let sort_unstable = query
3330 .iter(&world)
3331 .sort_unstable::<Entity>()
3332 .collect::<Vec<_>>();
3333
3334 let sort_by = query
3335 .iter(&world)
3336 .sort_by::<Entity>(Ord::cmp)
3337 .collect::<Vec<_>>();
3338
3339 let sort_unstable_by = query
3340 .iter(&world)
3341 .sort_unstable_by::<Entity>(Ord::cmp)
3342 .collect::<Vec<_>>();
3343
3344 let sort_by_key = query
3345 .iter(&world)
3346 .sort_by_key::<Entity, _>(|&e| e)
3347 .collect::<Vec<_>>();
3348
3349 let sort_unstable_by_key = query
3350 .iter(&world)
3351 .sort_unstable_by_key::<Entity, _>(|&e| e)
3352 .collect::<Vec<_>>();
3353
3354 let sort_by_cached_key = query
3355 .iter(&world)
3356 .sort_by_cached_key::<Entity, _>(|&e| e)
3357 .collect::<Vec<_>>();
3358
3359 let mut sort_v2 = query.iter(&world).collect::<Vec<_>>();
3360 sort_v2.sort();
3361
3362 let mut sort_unstable_v2 = query.iter(&world).collect::<Vec<_>>();
3363 sort_unstable_v2.sort_unstable();
3364
3365 let mut sort_by_v2 = query.iter(&world).collect::<Vec<_>>();
3366 sort_by_v2.sort_by(Ord::cmp);
3367
3368 let mut sort_unstable_by_v2 = query.iter(&world).collect::<Vec<_>>();
3369 sort_unstable_by_v2.sort_unstable_by(Ord::cmp);
3370
3371 let mut sort_by_key_v2 = query.iter(&world).collect::<Vec<_>>();
3372 sort_by_key_v2.sort_by_key(|&e| e);
3373
3374 let mut sort_unstable_by_key_v2 = query.iter(&world).collect::<Vec<_>>();
3375 sort_unstable_by_key_v2.sort_unstable_by_key(|&e| e);
3376
3377 let mut sort_by_cached_key_v2 = query.iter(&world).collect::<Vec<_>>();
3378 sort_by_cached_key_v2.sort_by_cached_key(|&e| e);
3379
3380 assert_eq!(sort, sort_v2);
3381 assert_eq!(sort_unstable, sort_unstable_v2);
3382 assert_eq!(sort_by, sort_by_v2);
3383 assert_eq!(sort_unstable_by, sort_unstable_by_v2);
3384 assert_eq!(sort_by_key, sort_by_key_v2);
3385 assert_eq!(sort_unstable_by_key, sort_unstable_by_key_v2);
3386 assert_eq!(sort_by_cached_key, sort_by_cached_key_v2);
3387 }
3388
3389 #[test]
3390 #[should_panic]
3391 fn query_iter_sort_after_next() {
3392 let mut world = World::new();
3393 world.spawn((A(0.),));
3394 world.spawn((A(1.1),));
3395 world.spawn((A(2.22),));
3396
3397 {
3398 let mut query = world.query::<&A>();
3399 let mut iter = query.iter(&world);
3400 println!(
3401 "archetype_entities: {} table_entities: {} current_len: {} current_row: {}",
3402 iter.cursor.archetype_entities.len(),
3403 iter.cursor.table_entities.len(),
3404 iter.cursor.current_len,
3405 iter.cursor.current_row
3406 );
3407 _ = iter.next();
3408 println!(
3409 "archetype_entities: {} table_entities: {} current_len: {} current_row: {}",
3410 iter.cursor.archetype_entities.len(),
3411 iter.cursor.table_entities.len(),
3412 iter.cursor.current_len,
3413 iter.cursor.current_row
3414 );
3415 println!("{}", iter.sort::<Entity>().len());
3416 }
3417 }
3418
3419 #[test]
3420 #[should_panic]
3421 fn query_iter_sort_after_next_dense() {
3422 let mut world = World::new();
3423 world.spawn((Sparse(11),));
3424 world.spawn((Sparse(22),));
3425 world.spawn((Sparse(33),));
3426
3427 {
3428 let mut query = world.query::<&Sparse>();
3429 let mut iter = query.iter(&world);
3430 println!(
3431 "before_next_call: archetype_entities: {} table_entities: {} current_len: {} current_row: {}",
3432 iter.cursor.archetype_entities.len(),
3433 iter.cursor.table_entities.len(),
3434 iter.cursor.current_len,
3435 iter.cursor.current_row
3436 );
3437 _ = iter.next();
3438 println!(
3439 "after_next_call: archetype_entities: {} table_entities: {} current_len: {} current_row: {}",
3440 iter.cursor.archetype_entities.len(),
3441 iter.cursor.table_entities.len(),
3442 iter.cursor.current_len,
3443 iter.cursor.current_row
3444 );
3445 println!("{}", iter.sort::<Entity>().len());
3446 }
3447 }
3448
3449 #[test]
3450 fn empty_query_iter_sort_after_next_does_not_panic() {
3451 let mut world = World::new();
3452 {
3453 let mut query = world.query::<(&A, &Sparse)>();
3454 let mut iter = query.iter(&world);
3455 println!(
3456 "before_next_call: archetype_entities: {} table_entities: {} current_len: {} current_row: {}",
3457 iter.cursor.archetype_entities.len(),
3458 iter.cursor.table_entities.len(),
3459 iter.cursor.current_len,
3460 iter.cursor.current_row
3461 );
3462 _ = iter.next();
3463 println!(
3464 "after_next_call: archetype_entities: {} table_entities: {} current_len: {} current_row: {}",
3465 iter.cursor.archetype_entities.len(),
3466 iter.cursor.table_entities.len(),
3467 iter.cursor.current_len,
3468 iter.cursor.current_row
3469 );
3470 println!("{}", iter.sort::<Entity>().len());
3471 }
3472 }
3473
3474 #[test]
3475 fn query_iter_cursor_state_non_empty_after_next() {
3476 let mut world = World::new();
3477 world.spawn((A(0.), Sparse(11)));
3478 world.spawn((A(1.1), Sparse(22)));
3479 world.spawn((A(2.22), Sparse(33)));
3480 {
3481 let mut query = world.query::<(&A, &Sparse)>();
3482 let mut iter = query.iter(&world);
3483 println!(
3484 "before_next_call: archetype_entities: {} table_entities: {} current_len: {} current_row: {}",
3485 iter.cursor.archetype_entities.len(),
3486 iter.cursor.table_entities.len(),
3487 iter.cursor.current_len,
3488 iter.cursor.current_row
3489 );
3490 assert!(iter.cursor.table_entities.len() | iter.cursor.archetype_entities.len() == 0);
3491 _ = iter.next();
3492 println!(
3493 "after_next_call: archetype_entities: {} table_entities: {} current_len: {} current_row: {}",
3494 iter.cursor.archetype_entities.len(),
3495 iter.cursor.table_entities.len(),
3496 iter.cursor.current_len,
3497 iter.cursor.current_row
3498 );
3499 assert!(
3500 (
3501 iter.cursor.table_entities.len(),
3502 iter.cursor.archetype_entities.len()
3503 ) != (0, 0)
3504 );
3505 }
3506 }
3507
3508 #[test]
3509 fn query_iter_many_sorts() {
3510 let mut world = World::new();
3511
3512 let entity_list: &Vec<_> = &world
3513 .spawn_batch([A(0.), A(1.), A(2.), A(3.), A(4.)])
3514 .collect();
3515
3516 let mut query = world.query::<Entity>();
3517
3518 let sort = query
3519 .iter_many(&world, entity_list)
3520 .sort::<Entity>()
3521 .map(Result::unwrap)
3522 .collect::<Vec<_>>();
3523
3524 let sort_unstable = query
3525 .iter_many(&world, entity_list)
3526 .sort_unstable::<Entity>()
3527 .map(Result::unwrap)
3528 .collect::<Vec<_>>();
3529
3530 let sort_by = query
3531 .iter_many(&world, entity_list)
3532 .sort_by::<Entity>(Ord::cmp)
3533 .map(Result::unwrap)
3534 .collect::<Vec<_>>();
3535
3536 let sort_unstable_by = query
3537 .iter_many(&world, entity_list)
3538 .sort_unstable_by::<Entity>(Ord::cmp)
3539 .map(Result::unwrap)
3540 .collect::<Vec<_>>();
3541
3542 let sort_by_key = query
3543 .iter_many(&world, entity_list)
3544 .sort_by_key::<Entity, _>(|&e| e)
3545 .map(Result::unwrap)
3546 .collect::<Vec<_>>();
3547
3548 let sort_unstable_by_key = query
3549 .iter_many(&world, entity_list)
3550 .sort_unstable_by_key::<Entity, _>(|&e| e)
3551 .map(Result::unwrap)
3552 .collect::<Vec<_>>();
3553
3554 let sort_by_cached_key = query
3555 .iter_many(&world, entity_list)
3556 .sort_by_cached_key::<Entity, _>(|&e| e)
3557 .map(Result::unwrap)
3558 .collect::<Vec<_>>();
3559
3560 let mut sort_v2 = query
3561 .iter_many(&world, entity_list)
3562 .unwrapped()
3563 .collect::<Vec<_>>();
3564 sort_v2.sort();
3565
3566 let mut sort_unstable_v2 = query
3567 .iter_many(&world, entity_list)
3568 .unwrapped()
3569 .collect::<Vec<_>>();
3570 sort_unstable_v2.sort_unstable();
3571
3572 let mut sort_by_v2 = query
3573 .iter_many(&world, entity_list)
3574 .unwrapped()
3575 .collect::<Vec<_>>();
3576 sort_by_v2.sort_by(Ord::cmp);
3577
3578 let mut sort_unstable_by_v2 = query
3579 .iter_many(&world, entity_list)
3580 .unwrapped()
3581 .collect::<Vec<_>>();
3582 sort_unstable_by_v2.sort_unstable_by(Ord::cmp);
3583
3584 let mut sort_by_key_v2 = query
3585 .iter_many(&world, entity_list)
3586 .unwrapped()
3587 .collect::<Vec<_>>();
3588 sort_by_key_v2.sort_by_key(|&e| e);
3589
3590 let mut sort_unstable_by_key_v2 = query
3591 .iter_many(&world, entity_list)
3592 .unwrapped()
3593 .collect::<Vec<_>>();
3594 sort_unstable_by_key_v2.sort_unstable_by_key(|&e| e);
3595
3596 let mut sort_by_cached_key_v2 = query
3597 .iter_many(&world, entity_list)
3598 .unwrapped()
3599 .collect::<Vec<_>>();
3600 sort_by_cached_key_v2.sort_by_cached_key(|&e| e);
3601
3602 assert_eq!(sort, sort_v2);
3603 assert_eq!(sort_unstable, sort_unstable_v2);
3604 assert_eq!(sort_by, sort_by_v2);
3605 assert_eq!(sort_unstable_by, sort_unstable_by_v2);
3606 assert_eq!(sort_by_key, sort_by_key_v2);
3607 assert_eq!(sort_unstable_by_key, sort_unstable_by_key_v2);
3608 assert_eq!(sort_by_cached_key, sort_by_cached_key_v2);
3609 }
3610
3611 #[test]
3612 fn query_iter_many_sort_doesnt_panic_after_next() {
3613 let mut world = World::new();
3614
3615 let entity_list: &Vec<_> = &world
3616 .spawn_batch([A(0.), A(1.), A(2.), A(3.), A(4.)])
3617 .collect();
3618
3619 let mut query = world.query::<Entity>();
3620 let mut iter = query.iter_many(&world, entity_list);
3621
3622 _ = iter.next();
3623
3624 iter.sort::<Entity>();
3625
3626 let mut query_2 = world.query::<&mut A>();
3627 let mut iter_2 = query_2.iter_many_mut(&mut world, entity_list);
3628
3629 _ = iter_2.fetch_next();
3630
3631 iter_2.sort::<Entity>();
3632 }
3633
3634 // This test should be run with miri to check for UB caused by aliasing.
3635 // The lens items created during the sort must not be live at the same time as the mutable references returned from the iterator.
3636 #[test]
3637 fn query_iter_many_sorts_duplicate_entities_no_ub() {
3638 #[derive(Component, Ord, PartialOrd, Eq, PartialEq)]
3639 struct C(usize);
3640
3641 let mut world = World::new();
3642 let id = world.spawn(C(10)).id();
3643 let mut query_state = world.query::<&mut C>();
3644
3645 {
3646 let mut query = query_state.iter_many_mut(&mut world, [id, id]).sort::<&C>();
3647 while query.fetch_next().is_some() {}
3648 }
3649 {
3650 let mut query = query_state
3651 .iter_many_mut(&mut world, [id, id])
3652 .sort_unstable::<&C>();
3653 while query.fetch_next().is_some() {}
3654 }
3655 {
3656 let mut query = query_state
3657 .iter_many_mut(&mut world, [id, id])
3658 .sort_by::<&C>(|l, r| Ord::cmp(l, r));
3659 while query.fetch_next().is_some() {}
3660 }
3661 {
3662 let mut query = query_state
3663 .iter_many_mut(&mut world, [id, id])
3664 .sort_unstable_by::<&C>(|l, r| Ord::cmp(l, r));
3665 while query.fetch_next().is_some() {}
3666 }
3667 {
3668 let mut query = query_state
3669 .iter_many_mut(&mut world, [id, id])
3670 .sort_by_key::<&C, _>(|d| d.0);
3671 while query.fetch_next().is_some() {}
3672 }
3673 {
3674 let mut query = query_state
3675 .iter_many_mut(&mut world, [id, id])
3676 .sort_unstable_by_key::<&C, _>(|d| d.0);
3677 while query.fetch_next().is_some() {}
3678 }
3679 {
3680 let mut query = query_state
3681 .iter_many_mut(&mut world, [id, id])
3682 .sort_by_cached_key::<&C, _>(|d| d.0);
3683 while query.fetch_next().is_some() {}
3684 }
3685 }
3686}