Skip to content

Commit e988ae1

Browse files
committed
add IterEntities traits and rebase MapEntities on them
1 parent 16b341b commit e988ae1

File tree

10 files changed

+68
-35
lines changed

10 files changed

+68
-35
lines changed

crates/bevy_ecs/src/entity/map_entities.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,46 @@ pub trait MapEntitiesMut: MapEntities {
4747

4848
impl<T> MapEntities for T
4949
where
50-
for<'a> &'a T: IntoIterator<Item = Entity>,
50+
T: IterEntities,
5151
{
5252
fn map_entities<F: FnMut(Entity)>(&self, f: F) {
53-
self.into_iter().for_each(f);
53+
self.iter_entities().for_each(f)
5454
}
5555
}
5656

5757
impl<T> MapEntitiesMut for T
5858
where
59-
for<'a> &'a mut T: IntoIterator<Item = &'a mut Entity>,
60-
T: MapEntities,
59+
T: MapEntities + IterEntitiesMut,
6160
{
6261
fn map_entities_mut<F: FnMut(&mut Entity)>(&mut self, f: F) {
63-
self.into_iter().for_each(f);
62+
self.iter_entities_mut().for_each(f)
63+
}
64+
}
65+
66+
pub trait IterEntities {
67+
fn iter_entities(&self) -> impl Iterator<Item = Entity>;
68+
}
69+
70+
impl<T> IterEntities for T
71+
where
72+
for<'a> &'a T: IntoIterator<Item = Entity>,
73+
{
74+
fn iter_entities(&self) -> impl Iterator<Item = Entity> {
75+
self.into_iter()
76+
}
77+
}
78+
79+
pub trait IterEntitiesMut: IterEntities {
80+
fn iter_entities_mut(&mut self) -> impl Iterator<Item = &mut Entity>;
81+
}
82+
83+
impl<T> IterEntitiesMut for T
84+
where
85+
for<'a> &'a mut T: IntoIterator<Item = &'a mut Entity>,
86+
T: IterEntities,
87+
{
88+
fn iter_entities_mut(&mut self) -> impl Iterator<Item = &mut Entity> {
89+
self.into_iter()
6490
}
6591
}
6692

crates/bevy_hierarchy/src/components/children.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use bevy_ecs::reflect::{ReflectComponent, ReflectFromWorld, ReflectMapEntities};
33
use bevy_ecs::{
44
component::Component,
5-
entity::{Entity, EntityMapper, MapEntities},
5+
entity::{Entity, MapEntitiesMut},
66
prelude::FromWorld,
77
world::World,
88
};
@@ -28,10 +28,10 @@ use std::ops::Deref;
2828
#[cfg_attr(feature = "reflect", reflect(Component, MapEntities, Debug, FromWorld))]
2929
pub struct Children(pub(crate) SmallVec<[Entity; 8]>);
3030

31-
impl MapEntities for Children {
32-
fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M) {
31+
impl MapEntitiesMut for Children {
32+
fn map_entities_mut<F: FnMut(&mut Entity)>(&mut self, mut f: F) {
3333
for entity in &mut self.0 {
34-
*entity = entity_mapper.map_entity(*entity);
34+
f(entity);
3535
}
3636
}
3737
}
@@ -155,10 +155,10 @@ impl Deref for Children {
155155
impl<'a> IntoIterator for &'a Children {
156156
type Item = <Self::IntoIter as Iterator>::Item;
157157

158-
type IntoIter = slice::Iter<'a, Entity>;
158+
type IntoIter = std::iter::Copied<slice::Iter<'a, Entity>>;
159159

160160
#[inline(always)]
161161
fn into_iter(self) -> Self::IntoIter {
162-
self.0.iter()
162+
self.0.iter().copied()
163163
}
164164
}

crates/bevy_hierarchy/src/components/parent.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
use bevy_ecs::reflect::{ReflectComponent, ReflectFromWorld, ReflectMapEntities};
33
use bevy_ecs::{
44
component::Component,
5-
entity::{Entity, EntityMapper, MapEntities},
5+
entity::Entity,
66
traversal::Traversal,
77
world::{FromWorld, World},
88
};
9-
use std::ops::Deref;
9+
use std::{ops::Deref, option};
1010

1111
/// Holds a reference to the parent entity of this entity.
1212
/// This component should only be present on entities that actually have a parent entity.
@@ -59,9 +59,21 @@ impl FromWorld for Parent {
5959
}
6060
}
6161

62-
impl MapEntities for Parent {
63-
fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M) {
64-
self.0 = entity_mapper.map_entity(self.0);
62+
impl<'a> IntoIterator for &'a Parent {
63+
type Item = Entity;
64+
type IntoIter = option::IntoIter<Entity>;
65+
66+
fn into_iter(self) -> Self::IntoIter {
67+
Some(self.0).into_iter()
68+
}
69+
}
70+
71+
impl<'a> IntoIterator for &'a mut Parent {
72+
type Item = &'a mut Entity;
73+
type IntoIter = option::IntoIter<&'a mut Entity>;
74+
75+
fn into_iter(self) -> Self::IntoIter {
76+
Some(&mut self.0).into_iter()
6577
}
6678
}
6779

crates/bevy_hierarchy/src/query_extension.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,7 @@ where
9292
pub fn new(children_query: &'w Query<'w, 's, D, F>, entity: Entity) -> Self {
9393
DescendantIter {
9494
children_query,
95-
vecdeque: children_query
96-
.get(entity)
97-
.into_iter()
98-
.flatten()
99-
.copied()
100-
.collect(),
95+
vecdeque: children_query.get(entity).into_iter().flatten().collect(),
10196
}
10297
}
10398
}

crates/bevy_ui/src/layout/ui_surface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl UiSurface {
117117
pub fn update_children(&mut self, entity: Entity, children: &Children) {
118118
let mut taffy_children = Vec::with_capacity(children.len());
119119
for child in children {
120-
if let Some(taffy_node) = self.entity_to_taffy.get(child) {
120+
if let Some(taffy_node) = self.entity_to_taffy.get(&child) {
121121
taffy_children.push(*taffy_node);
122122
} else {
123123
warn!(

crates/bevy_ui/src/stack.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ fn insert_context_hierarchy(
108108
cache,
109109
zindex_query,
110110
children_query,
111-
*entity,
111+
entity,
112112
global_context,
113113
Some(&mut new_context),
114114
total_entry_count,

crates/bevy_ui/src/update.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ fn update_children_target_camera(
153153
return;
154154
};
155155

156-
for &child in children {
156+
for child in children {
157157
// Skip if the child has already been updated or update is not needed
158158
if updated_entities.contains(&child)
159159
|| camera_to_set == node_query.get(child).ok().flatten()

examples/ecs/hierarchy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fn rotate(
7272
// To iterate through the entities children, just treat the Children component as a Vec
7373
// Alternatively, you could query entities that have a Parent component
7474
for child in children {
75-
if let Ok(mut transform) = transform_query.get_mut(*child) {
75+
if let Ok(mut transform) = transform_query.get_mut(child) {
7676
transform.rotate_z(PI * time.delta_seconds());
7777
}
7878
}

examples/ui/display_and_visibility.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ fn buttons_handler<T>(
442442
for (target, interaction, children) in visibility_button_query.iter_mut() {
443443
if matches!(interaction, Interaction::Pressed) {
444444
let mut target_value = left_panel_query.get_mut(target.id).unwrap();
445-
for &child in children {
445+
for child in children {
446446
if let Ok(mut text) = text_query.get_mut(child) {
447447
text.sections[0].value = target.update_target(target_value.as_mut());
448448
text.sections[0].style.color = if text.sections[0].value.contains("None")
@@ -466,7 +466,7 @@ fn text_hover(
466466
match interaction {
467467
Interaction::Hovered => {
468468
*color = Color::BLACK.with_alpha(0.6).into();
469-
for &child in children {
469+
for child in children {
470470
if let Ok(mut text) = text_query.get_mut(child) {
471471
// Bypass change detection to avoid recomputation of the text when only changing the color
472472
text.bypass_change_detection().sections[0].style.color = YELLOW.into();
@@ -475,7 +475,7 @@ fn text_hover(
475475
}
476476
_ => {
477477
*color = Color::BLACK.with_alpha(0.5).into();
478-
for &child in children {
478+
for child in children {
479479
if let Ok(mut text) = text_query.get_mut(child) {
480480
text.bypass_change_detection().sections[0].style.color =
481481
if text.sections[0].value.contains("None")

examples/ui/size_constraints.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,9 @@ fn update_buttons(
324324
}
325325
Interaction::Hovered => {
326326
if let Ok(children) = children_query.get(button_id) {
327-
for &child in children {
327+
for child in children {
328328
if let Ok(grand_children) = children_query.get(child) {
329-
for &grandchild in grand_children {
329+
for grandchild in grand_children {
330330
if let Ok(mut text) = text_query.get_mut(grandchild) {
331331
if text.sections[0].style.color != ACTIVE_TEXT_COLOR {
332332
text.sections[0].style.color = HOVERED_TEXT_COLOR;
@@ -339,9 +339,9 @@ fn update_buttons(
339339
}
340340
Interaction::None => {
341341
if let Ok(children) = children_query.get(button_id) {
342-
for &child in children {
342+
for child in children {
343343
if let Ok(grand_children) = children_query.get(child) {
344-
for &grandchild in grand_children {
344+
for grandchild in grand_children {
345345
if let Ok(mut text) = text_query.get_mut(grandchild) {
346346
if text.sections[0].style.color != ACTIVE_TEXT_COLOR {
347347
text.sections[0].style.color = UNHOVERED_TEXT_COLOR;
@@ -383,9 +383,9 @@ fn update_radio_buttons_colors(
383383
};
384384

385385
border_query.get_mut(id).unwrap().0 = border_color;
386-
for &child in children_query.get(id).into_iter().flatten() {
386+
for child in children_query.get(id).into_iter().flatten() {
387387
color_query.get_mut(child).unwrap().0 = inner_color;
388-
for &grandchild in children_query.get(child).into_iter().flatten() {
388+
for grandchild in children_query.get(child).into_iter().flatten() {
389389
if let Ok(mut text) = text_query.get_mut(grandchild) {
390390
text.sections[0].style.color = text_color;
391391
}

0 commit comments

Comments
 (0)