Skip to content

Commit 55ad1b3

Browse files
tim-blackbirdItsDoot
authored andcommitted
Update codebase to use IntoIterator where possible. (bevyengine#5269)
Remove unnecessary calls to `iter()`/`iter_mut()`. Mainly updates the use of queries in our code, docs, and examples. ```rust // From for _ in list.iter() { for _ in list.iter_mut() { // To for _ in &list { for _ in &mut list { ``` We already enable the pedantic lint [clippy::explicit_iter_loop](https://rust-lang.github.io/rust-clippy/stable/) inside of Bevy. However, this only warns for a few known types from the standard library. ## Note for reviewers As you can see the additions and deletions are exactly equal. Maybe give it a quick skim to check I didn't sneak in a crypto miner, but you don't have to torture yourself by reading every line. I already experienced enough pain making this PR :) Co-authored-by: devil-ira <[email protected]>
1 parent 1744be3 commit 55ad1b3

File tree

95 files changed

+191
-191
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+191
-191
lines changed

benches/benches/bevy_ecs/iteration/iter_simple_system.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl Benchmark {
3030
}
3131

3232
fn query_system(mut query: Query<(&Velocity, &mut Position)>) {
33-
for (velocity, mut position) in query.iter_mut() {
33+
for (velocity, mut position) in &mut query {
3434
position.0 += velocity.0;
3535
}
3636
}

benches/benches/bevy_ecs/world/commands.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub fn insert_commands(criterion: &mut Criterion) {
9292

9393
bencher.iter(|| {
9494
let mut commands = Commands::new(&mut command_queue, &world);
95-
for entity in entities.iter() {
95+
for entity in &entities {
9696
commands
9797
.entity(*entity)
9898
.insert_bundle((Matrix::default(), Vec3::default()));
@@ -112,7 +112,7 @@ pub fn insert_commands(criterion: &mut Criterion) {
112112
bencher.iter(|| {
113113
let mut commands = Commands::new(&mut command_queue, &world);
114114
let mut values = Vec::with_capacity(entity_count);
115-
for entity in entities.iter() {
115+
for entity in &entities {
116116
values.push((*entity, (Matrix::default(), Vec3::default())));
117117
}
118118
commands.insert_or_spawn_batch(values);

benches/benches/bevy_reflect/struct.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn concrete_struct_field(criterion: &mut Criterion) {
4343
.collect::<Vec<_>>();
4444

4545
bencher.iter(|| {
46-
for name in field_names.iter() {
46+
for name in &field_names {
4747
s.field(black_box(name));
4848
}
4949
});

crates/bevy_animation/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ pub fn animation_player(
183183
mut transforms: Query<&mut Transform>,
184184
children: Query<&Children>,
185185
) {
186-
for (entity, mut player) in animation_players.iter_mut() {
186+
for (entity, mut player) in &mut animation_players {
187187
if let Some(animation_clip) = animations.get(&player.animation_clip) {
188188
// Continue if paused unless the `AnimationPlayer` was changed
189189
// This allow the animation to still be updated if the player.elapsed field was manually updated in pause

crates/bevy_asset/src/asset_server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl AssetServer {
110110
{
111111
let mut loaders = self.server.loaders.write();
112112
let loader_index = loaders.len();
113-
for extension in loader.extensions().iter() {
113+
for extension in loader.extensions() {
114114
self.server
115115
.extension_to_loader_index
116116
.write()

crates/bevy_core_pipeline/src/core_3d/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ pub fn prepare_core_3d_depth_textures(
236236
>,
237237
) {
238238
let mut textures = HashMap::default();
239-
for (entity, camera) in views_3d.iter() {
239+
for (entity, camera) in &views_3d {
240240
if let Some(physical_target_size) = camera.physical_target_size {
241241
let cached_texture = textures
242242
.entry(camera.target.clone())

crates/bevy_ecs/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ use bevy_ecs::prelude::*;
7878
struct Position { x: f32, y: f32 }
7979

8080
fn print_position(query: Query<(Entity, &Position)>) {
81-
for (entity, position) in query.iter() {
81+
for (entity, position) in &query {
8282
println!("Entity {:?} is at position: x {}, y {}", entity, position.x, position.y);
8383
}
8484
}
@@ -130,7 +130,7 @@ struct Velocity { x: f32, y: f32 }
130130

131131
// This system moves each entity with a Position and Velocity component
132132
fn movement(mut query: Query<(&mut Position, &Velocity)>) {
133-
for (mut position, velocity) in query.iter_mut() {
133+
for (mut position, velocity) in &mut query {
134134
position.x += velocity.x;
135135
position.y += velocity.y;
136136
}
@@ -176,7 +176,7 @@ struct Alive;
176176
// Gets the Position component of all Entities with Player component and without the Alive
177177
// component.
178178
fn system(query: Query<&Position, (With<Player>, Without<Alive>)>) {
179-
for position in query.iter() {
179+
for position in &query {
180180
}
181181
}
182182
```
@@ -197,13 +197,13 @@ struct Velocity { x: f32, y: f32 }
197197

198198
// Gets the Position component of all Entities whose Velocity has changed since the last run of the System
199199
fn system_changed(query: Query<&Position, Changed<Velocity>>) {
200-
for position in query.iter() {
200+
for position in &query {
201201
}
202202
}
203203

204204
// Gets the Position component of all Entities that had a Velocity component added since the last run of the System
205205
fn system_added(query: Query<&Position, Added<Velocity>>) {
206-
for position in query.iter() {
206+
for position in &query {
207207
}
208208
}
209209
```

crates/bevy_ecs/examples/change_detection.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,24 @@ fn print_changed_entities(
7979
entity_with_added_component: Query<Entity, Added<Age>>,
8080
entity_with_mutated_component: Query<(Entity, &Age), Changed<Age>>,
8181
) {
82-
for entity in entity_with_added_component.iter() {
82+
for entity in &entity_with_added_component {
8383
println!(" {:?} has it's first birthday!", entity);
8484
}
85-
for (entity, value) in entity_with_mutated_component.iter() {
85+
for (entity, value) in &entity_with_mutated_component {
8686
println!(" {:?} is now {:?} frames old", entity, value);
8787
}
8888
}
8989

9090
// This system iterates over all entities and increases their age in every frame
9191
fn age_all_entities(mut entities: Query<&mut Age>) {
92-
for mut age in entities.iter_mut() {
92+
for mut age in &mut entities {
9393
age.frames += 1;
9494
}
9595
}
9696

9797
// This system iterates over all entities in every frame and despawns entities older than 2 frames
9898
fn remove_old_entities(mut commands: Commands, entities: Query<(Entity, &Age)>) {
99-
for (entity, age) in entities.iter() {
99+
for (entity, age) in &entities {
100100
if age.frames > 2 {
101101
println!(" despawning {:?} due to age > 2", entity);
102102
commands.entity(entity).despawn();

crates/bevy_ecs/macros/src/fetch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ pub fn derive_world_query_impl(ast: DeriveInput) -> TokenStream {
134134
let mut field_idents = Vec::new();
135135
let mut field_types = Vec::new();
136136

137-
for field in fields.iter() {
137+
for field in fields {
138138
let WorldQueryFieldInfo { is_ignored, attrs } = read_world_query_field_info(field);
139139

140140
let field_ident = field.ident.as_ref().unwrap().clone();

crates/bevy_ecs/src/entity/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ use std::{
8484
/// # struct Expired;
8585
/// #
8686
/// fn dispose_expired_food(mut commands: Commands, query: Query<Entity, With<Expired>>) {
87-
/// for food_entity in query.iter() {
87+
/// for food_entity in &query {
8888
/// commands.entity(food_entity).despawn();
8989
/// }
9090
/// }

0 commit comments

Comments
 (0)