Skip to content

Commit b9fa5ec

Browse files
Create an extension trait and iterator
1 parent 6e3ab7d commit b9fa5ec

File tree

2 files changed

+62
-31
lines changed

2 files changed

+62
-31
lines changed

crates/bevy_ecs/src/query/iter.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate::{
22
archetype::{ArchetypeId, Archetypes},
3+
entity::Entity,
34
query::{Fetch, FilterFetch, QueryState, ReadOnlyFetch, WorldQuery},
45
storage::{TableId, Tables},
6+
system::Query,
57
world::World,
68
};
79
use std::mem::MaybeUninit;
@@ -550,3 +552,63 @@ where
550552
}
551553
}
552554
}
555+
556+
pub struct WithQuery<'w, I, Q, F>
557+
where
558+
Q: WorldQuery,
559+
Q::Fetch: ReadOnlyFetch,
560+
F: WorldQuery,
561+
F::Fetch: FilterFetch,
562+
{
563+
iter: I,
564+
query: &'w Query<'w, Q, F>,
565+
}
566+
567+
impl<'w, I, Q, F> Iterator for WithQuery<'w, I, Q, F>
568+
where
569+
I: Iterator<Item = Entity>,
570+
Q: WorldQuery,
571+
Q::Fetch: ReadOnlyFetch,
572+
F: WorldQuery,
573+
F::Fetch: FilterFetch,
574+
{
575+
type Item = <<Q as WorldQuery>::Fetch as Fetch<'w>>::Item;
576+
577+
fn next(&mut self) -> Option<Self::Item> {
578+
// if the entity iterator is done, this iterator is done
579+
let mut entity = self.iter.next()?;
580+
loop {
581+
match self.query.get(entity).ok() {
582+
Some(item) => return Some(item),
583+
None => {
584+
entity = self.iter.next()?;
585+
}
586+
}
587+
}
588+
}
589+
}
590+
591+
pub trait WithQueryExt: Sized {
592+
fn with_query<'w, Q, F>(self, query: &'w Query<'w, Q, F>) -> WithQuery<'w, Self, Q, F>
593+
where
594+
Q: WorldQuery,
595+
Q::Fetch: ReadOnlyFetch,
596+
F: WorldQuery,
597+
F::Fetch: FilterFetch;
598+
}
599+
600+
impl<I> WithQueryExt for I
601+
where
602+
I: Iterator<Item = Entity> + Sized,
603+
{
604+
fn with_query<'w, Q, F>(self, query: &'w Query<'w, Q, F>) -> WithQuery<'w, Self, Q, F>
605+
where
606+
Self: Sized,
607+
Q: WorldQuery,
608+
Q::Fetch: ReadOnlyFetch,
609+
F: WorldQuery,
610+
F::Fetch: FilterFetch,
611+
{
612+
WithQuery { iter: self, query }
613+
}
614+
}

crates/bevy_transform/src/components/children.rs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use bevy_ecs::{
22
entity::{Entity, EntityMap, MapEntities, MapEntitiesError},
3-
prelude::Query,
4-
query::{Fetch, FilterFetch, ReadOnlyFetch, WorldQuery},
53
reflect::{ReflectComponent, ReflectMapEntities},
64
};
75
use bevy_reflect::Reflect;
@@ -31,35 +29,6 @@ impl Children {
3129
pub fn swap(&mut self, a_index: usize, b_index: usize) {
3230
self.0.swap(a_index, b_index);
3331
}
34-
35-
/// Returns an iterator over the query result for each child
36-
///
37-
/// Example:
38-
/// ```
39-
/// fn example_system(
40-
/// parents: Query<&Children, With<Sprite>>,
41-
/// transforms: Query<&Transform, With<Sprite>>,
42-
/// ) {
43-
/// for children in parents.iter() {
44-
/// for transform in children.with_query(transforms) {
45-
/// println!("transform: {:?}", transform);
46-
/// }
47-
/// }
48-
/// }
49-
/// ```
50-
pub fn with_query<'a, Q, F>(
51-
&'a self,
52-
q: &'a Query<Q, F>,
53-
) -> impl Iterator<Item = <<Q as WorldQuery>::Fetch as Fetch<'a>>::Item>
54-
where
55-
Q: WorldQuery,
56-
Q::Fetch: ReadOnlyFetch,
57-
F: WorldQuery,
58-
F::Fetch: FilterFetch,
59-
{
60-
self.iter()
61-
.filter_map(move |child_id| q.get(*child_id).ok())
62-
}
6332
}
6433

6534
impl Deref for Children {

0 commit comments

Comments
 (0)