Skip to content

Commit 42d8b65

Browse files
committed
replace MapEntities impls with IntoIterator
1 parent e988ae1 commit 42d8b65

File tree

11 files changed

+131
-43
lines changed

11 files changed

+131
-43
lines changed

crates/bevy_animation/src/lib.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::{
1919
collections::BTreeMap,
2020
fmt::Debug,
2121
hash::{Hash, Hasher},
22-
iter,
22+
iter, option,
2323
};
2424

2525
use bevy_app::{App, Plugin, PostUpdate};
@@ -1291,9 +1291,19 @@ impl From<&Name> for AnimationTargetId {
12911291
}
12921292
}
12931293

1294-
impl MapEntities for AnimationTarget {
1295-
fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M) {
1296-
self.player = entity_mapper.map_entity(self.player);
1294+
impl<'a> IntoIterator for &'a mut AnimationTarget {
1295+
type IntoIter = option::IntoIter<&'a mut Entity>;
1296+
type Item = <Self::IntoIter as Iterator>::Item;
1297+
fn into_iter(self) -> Self::IntoIter {
1298+
Some(&mut self.player).into_iter()
1299+
}
1300+
}
1301+
1302+
impl<'a> IntoIterator for &'a AnimationTarget {
1303+
type IntoIter = option::IntoIter<Entity>;
1304+
type Item = <Self::IntoIter as Iterator>::Item;
1305+
fn into_iter(self) -> Self::IntoIter {
1306+
Some(self.player).into_iter()
12971307
}
12981308
}
12991309

crates/bevy_hierarchy/src/components/children.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@ 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 MapEntitiesMut for Children {
32-
fn map_entities_mut<F: FnMut(&mut Entity)>(&mut self, mut f: F) {
33-
for entity in &mut self.0 {
34-
f(entity);
35-
}
36-
}
37-
}
38-
3931
// TODO: We need to impl either FromWorld or Default so Children can be registered as Reflect.
4032
// This is because Reflect deserialize by creating an instance and apply a patch on top.
4133
// However Children should only ever be set with a real user-defined entities. Its worth looking
@@ -152,6 +144,17 @@ impl Deref for Children {
152144
}
153145
}
154146

147+
impl<'a> IntoIterator for &'a mut Children {
148+
type Item = <Self::IntoIter as Iterator>::Item;
149+
150+
type IntoIter = slice::IterMut<'a, Entity>;
151+
152+
#[inline(always)]
153+
fn into_iter(self) -> Self::IntoIter {
154+
self.0.iter_mut()
155+
}
156+
}
157+
155158
impl<'a> IntoIterator for &'a Children {
156159
type Item = <Self::IntoIter as Iterator>::Item;
157160

@@ -162,3 +165,15 @@ impl<'a> IntoIterator for &'a Children {
162165
self.0.iter().copied()
163166
}
164167
}
168+
169+
#[cfg(test)]
170+
mod test {
171+
use super::*;
172+
173+
fn assert_impls_map_entities_mut<M: MapEntitiesMut>() {}
174+
175+
#[test]
176+
fn children_impls_map_entities_mut() {
177+
assert_impls_map_entities_mut::<Children>();
178+
}
179+
}

crates/bevy_render/src/mesh/mesh/skinning.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@ pub struct SkinnedMesh {
1616
pub joints: Vec<Entity>,
1717
}
1818

19-
impl MapEntities for SkinnedMesh {
20-
fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M) {
21-
for joint in &mut self.joints {
22-
*joint = entity_mapper.map_entity(*joint);
23-
}
19+
impl<'a> IntoIterator for &'a mut SkinnedMesh {
20+
type IntoIter = std::slice::IterMut<'a, Entity>;
21+
type Item = <Self::IntoIter as Iterator>::Item;
22+
23+
fn into_iter(self) -> Self::IntoIter {
24+
self.joints.iter_mut()
25+
}
26+
}
27+
28+
impl<'a> IntoIterator for &'a SkinnedMesh {
29+
type IntoIter = std::iter::Copied<std::slice::Iter<'a, Entity>>;
30+
type Item = <Self::IntoIter as Iterator>::Item;
31+
32+
fn into_iter(self) -> Self::IntoIter {
33+
self.joints.iter().copied()
2434
}
2535
}
2636

crates/bevy_render/src/view/visibility/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ fn visibility_propagate_system(
370370
inherited_visibility.0 = is_visible;
371371

372372
// Recursively update the visibility of each child.
373-
for &child in children.into_iter().flatten() {
373+
for child in children.into_iter().flatten() {
374374
let _ =
375375
propagate_recursive(is_visible, child, &mut visibility_query, &children_query);
376376
}
@@ -401,7 +401,7 @@ fn propagate_recursive(
401401
inherited_visibility.0 = is_visible;
402402

403403
// Recursively update the visibility of each child.
404-
for &child in children_query.get(entity).ok().into_iter().flatten() {
404+
for child in children_query.get(entity).ok().into_iter().flatten() {
405405
let _ = propagate_recursive(is_visible, child, visibility_query, children_query);
406406
}
407407
}

crates/bevy_scene/src/dynamic_scene.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,11 @@ where
208208

209209
#[cfg(test)]
210210
mod tests {
211+
use std::option;
212+
211213
use bevy_ecs::{
212214
component::Component,
213-
entity::{Entity, EntityHashMap, EntityMapper, MapEntities},
215+
entity::{Entity, EntityHashMap, EntityMapper, IterEntities, IterEntitiesMut, MapEntities},
214216
reflect::{
215217
AppTypeRegistry, ReflectComponent, ReflectMapEntities, ReflectMapEntitiesResource,
216218
ReflectResource,
@@ -231,10 +233,17 @@ mod tests {
231233
entity_b: Entity,
232234
}
233235

234-
impl MapEntities for TestResource {
235-
fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M) {
236-
self.entity_a = entity_mapper.map_entity(self.entity_a);
237-
self.entity_b = entity_mapper.map_entity(self.entity_b);
236+
impl IterEntitiesMut for TestResource {
237+
fn iter_entities_mut(&mut self) -> impl Iterator<Item = &mut Entity> {
238+
Some(&mut self.entity_a)
239+
.into_iter()
240+
.chain(Some(&mut self.entity_b))
241+
}
242+
}
243+
244+
impl IterEntities for TestResource {
245+
fn iter_entities(&self) -> impl Iterator<Item = Entity> {
246+
Some(self.entity_a).into_iter().chain(Some(self.entity_b))
238247
}
239248
}
240249

@@ -366,9 +375,19 @@ mod tests {
366375
#[reflect(Component, MapEntities)]
367376
struct B(pub Entity);
368377

369-
impl MapEntities for B {
370-
fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M) {
371-
self.0 = entity_mapper.map_entity(self.0);
378+
impl<'a> IntoIterator for &'a B {
379+
type IntoIter = option::IntoIter<Entity>;
380+
type Item = <Self::IntoIter as Iterator>::Item;
381+
fn into_iter(self) -> Self::IntoIter {
382+
Some(self.0).into_iter()
383+
}
384+
}
385+
386+
impl<'a> IntoIterator for &'a mut B {
387+
type IntoIter = option::IntoIter<&'a mut Entity>;
388+
type Item = <Self::IntoIter as Iterator>::Item;
389+
fn into_iter(self) -> Self::IntoIter {
390+
Some(&mut self.0).into_iter()
372391
}
373392
}
374393

crates/bevy_scene/src/serde.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ mod tests {
524524
use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize};
525525
use bincode::Options;
526526
use serde::{de::DeserializeSeed, Deserialize, Serialize};
527-
use std::io::BufReader;
527+
use std::{io::BufReader, option};
528528

529529
#[derive(Component, Reflect, Default)]
530530
#[reflect(Component)]
@@ -588,9 +588,19 @@ mod tests {
588588
#[reflect(Component, MapEntities, PartialEq)]
589589
struct MyEntityRef(Entity);
590590

591-
impl MapEntities for MyEntityRef {
592-
fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M) {
593-
self.0 = entity_mapper.map_entity(self.0);
591+
impl<'a> IntoIterator for &'a mut MyEntityRef {
592+
type IntoIter = option::IntoIter<&'a mut Entity>;
593+
type Item = <Self::IntoIter as Iterator>::Item;
594+
fn into_iter(self) -> Self::IntoIter {
595+
Some(&mut self.0).into_iter()
596+
}
597+
}
598+
599+
impl<'a> IntoIterator for &'a MyEntityRef {
600+
type IntoIter = option::IntoIter<Entity>;
601+
type Item = <Self::IntoIter as Iterator>::Item;
602+
fn into_iter(self) -> Self::IntoIter {
603+
Some(self.0).into_iter()
594604
}
595605
}
596606

crates/bevy_ui/src/accessibility.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use bevy_transform::prelude::GlobalTransform;
2222
fn calc_name(texts: &Query<&Text>, children: &Children) -> Option<Box<str>> {
2323
let mut name = None;
2424
for child in children {
25-
if let Ok(text) = texts.get(*child) {
25+
if let Ok(text) = texts.get(child) {
2626
let values = text
2727
.sections
2828
.iter()

crates/bevy_ui/src/layout/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ pub fn ui_layout_system(
403403
}
404404

405405
if let Ok(children) = children_query.get(entity) {
406-
for &child_uinode in children {
406+
for child_uinode in children {
407407
update_uinode_geometry_recursive(
408408
commands,
409409
child_uinode,

crates/bevy_ui/src/update.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ fn update_clipping(
9393
};
9494

9595
if let Ok(children) = children_query.get(entity) {
96-
for &child in children {
96+
for child in children {
9797
update_clipping(commands, children_query, node_query, child, children_clip);
9898
}
9999
}

crates/bevy_window/src/window.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::num::NonZero;
1+
use std::{iter, num::NonZero, option};
22

33
use bevy_ecs::{
44
entity::{Entity, EntityMapper, MapEntities},
@@ -58,14 +58,27 @@ impl WindowRef {
5858
}
5959
}
6060

61-
impl MapEntities for WindowRef {
62-
fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M) {
61+
impl<'a> IntoIterator for &'a WindowRef {
62+
type Item = <Self::IntoIter as Iterator>::Item;
63+
type IntoIter = option::IntoIter<Entity>;
64+
fn into_iter(self) -> Self::IntoIter {
6365
match self {
64-
Self::Entity(entity) => {
65-
*entity = entity_mapper.map_entity(*entity);
66-
}
67-
Self::Primary => {}
68-
};
66+
WindowRef::Primary => None,
67+
WindowRef::Entity(entity) => Some(*entity),
68+
}
69+
.into_iter()
70+
}
71+
}
72+
73+
impl<'a> IntoIterator for &'a mut WindowRef {
74+
type Item = <Self::IntoIter as Iterator>::Item;
75+
type IntoIter = option::IntoIter<&'a mut Entity>;
76+
fn into_iter(self) -> Self::IntoIter {
77+
match self {
78+
WindowRef::Primary => None,
79+
WindowRef::Entity(entity) => Some(entity),
80+
}
81+
.into_iter()
6982
}
7083
}
7184

@@ -1213,8 +1226,19 @@ pub struct ClosingWindow;
12131226

12141227
#[cfg(test)]
12151228
mod tests {
1229+
use bevy_ecs::entity::MapEntitiesMut;
1230+
12161231
use super::*;
12171232

1233+
// Compile-time assertion that a type implementes MapEntitiesMut
1234+
fn impl_map_entities_mut<M: MapEntitiesMut>() {}
1235+
1236+
// Ensure that WidowRef implements MapEntitiesMut
1237+
#[allow(dead_code)]
1238+
fn window_impls_map_entities_mut() {
1239+
impl_map_entities_mut::<WindowRef>();
1240+
}
1241+
12181242
// Checks that `Window::physical_cursor_position` returns the cursor position if it is within
12191243
// the bounds of the window.
12201244
#[test]

0 commit comments

Comments
 (0)