From 4847f7e3adc835053a8907dd578c342b4bd395e2 Mon Sep 17 00:00:00 2001 From: ira Date: Mon, 11 Jul 2022 15:28:50 +0000 Subject: [PATCH] Update codebase to use `IntoIterator` where possible. (#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 --- .../bevy_ecs/iteration/iter_simple_system.rs | 2 +- benches/benches/bevy_ecs/world/commands.rs | 4 ++-- benches/benches/bevy_reflect/struct.rs | 2 +- crates/bevy_animation/src/lib.rs | 2 +- crates/bevy_asset/src/asset_server.rs | 2 +- crates/bevy_core_pipeline/src/core_3d/mod.rs | 2 +- crates/bevy_ecs/README.md | 10 +++++----- crates/bevy_ecs/examples/change_detection.rs | 8 ++++---- crates/bevy_ecs/macros/src/fetch.rs | 2 +- crates/bevy_ecs/src/entity/mod.rs | 2 +- crates/bevy_ecs/src/query/fetch.rs | 16 ++++++++-------- crates/bevy_ecs/src/query/filter.rs | 10 +++++----- crates/bevy_ecs/src/query/state.rs | 4 ++-- .../bevy_ecs/src/schedule/executor_parallel.rs | 2 +- crates/bevy_ecs/src/storage/table.rs | 2 +- .../src/system/commands/parallel_scope.rs | 2 +- crates/bevy_ecs/src/system/exclusive_system.rs | 2 +- crates/bevy_ecs/src/system/function_system.rs | 2 +- crates/bevy_ecs/src/system/mod.rs | 6 +++--- crates/bevy_ecs/src/system/query.rs | 14 +++++++------- crates/bevy_hierarchy/src/components/children.rs | 2 +- crates/bevy_pbr/src/light.rs | 16 ++++++++-------- crates/bevy_pbr/src/material.rs | 2 +- crates/bevy_pbr/src/render/light.rs | 6 +++--- crates/bevy_pbr/src/render/mesh.rs | 2 +- crates/bevy_pbr/src/wireframe.rs | 4 ++-- .../bevy_reflect_derive/src/field_attributes.rs | 2 +- crates/bevy_render/src/camera/camera.rs | 4 ++-- crates/bevy_render/src/mesh/mesh/mod.rs | 2 +- crates/bevy_render/src/render_phase/mod.rs | 4 ++-- crates/bevy_render/src/view/mod.rs | 4 ++-- crates/bevy_render/src/view/visibility/mod.rs | 6 +++--- crates/bevy_scene/src/bundle.rs | 4 ++-- crates/bevy_scene/src/scene_spawner.rs | 2 +- crates/bevy_scene/src/serde.rs | 2 +- crates/bevy_sprite/src/mesh2d/material.rs | 2 +- crates/bevy_sprite/src/mesh2d/mesh.rs | 2 +- crates/bevy_sprite/src/render/mod.rs | 4 ++-- crates/bevy_text/src/text2d.rs | 2 +- crates/bevy_transform/src/systems.rs | 4 ++-- crates/bevy_ui/src/flex/mod.rs | 10 +++++----- crates/bevy_ui/src/render/mod.rs | 4 ++-- crates/bevy_ui/src/update.rs | 4 ++-- crates/bevy_ui/src/widget/image.rs | 2 +- crates/bevy_window/src/window.rs | 2 +- examples/2d/mesh2d_manual.rs | 2 +- examples/2d/move_sprite.rs | 2 +- examples/2d/rotation.rs | 4 ++-- examples/2d/sprite_sheet.rs | 2 +- examples/2d/text2d.rs | 6 +++--- examples/3d/lighting.rs | 4 ++-- examples/3d/load_gltf.rs | 2 +- examples/3d/parenting.rs | 2 +- examples/3d/render_to_texture.rs | 4 ++-- examples/3d/shadow_biases.rs | 10 +++++----- examples/3d/shadow_caster_receiver.rs | 4 ++-- examples/3d/shapes.rs | 2 +- examples/3d/update_gltf_scene.rs | 2 +- examples/animation/custom_skinned_mesh.rs | 2 +- examples/animation/gltf_skinned_mesh.rs | 2 +- examples/async_tasks/async_compute.rs | 2 +- .../external_source_external_thread.rs | 2 +- examples/ecs/component_change_detection.rs | 6 +++--- examples/ecs/custom_query_param.rs | 8 ++++---- examples/ecs/ecs_guide.rs | 4 ++-- examples/ecs/generic_system.rs | 4 ++-- examples/ecs/hierarchy.rs | 4 ++-- examples/ecs/iter_combinations.rs | 2 +- examples/ecs/state.rs | 6 +++--- examples/ecs/timers.rs | 2 +- examples/games/alien_cake_addict.rs | 2 +- examples/games/breakout.rs | 4 ++-- examples/games/contributors.rs | 6 +++--- examples/games/game_menu.rs | 8 ++++---- examples/ios/src/lib.rs | 2 +- examples/scene/scene.rs | 2 +- examples/shader/animate_shader.rs | 4 ++-- examples/shader/post_processing.rs | 2 +- examples/shader/shader_instancing.rs | 6 +++--- examples/stress_tests/bevymark.rs | 4 ++-- examples/stress_tests/many_foxes.rs | 6 +++--- examples/stress_tests/transform_hierarchy.rs | 2 +- examples/tools/scene_viewer.rs | 6 +++--- examples/transforms/3d_rotation.rs | 2 +- .../transforms/global_vs_local_translation.rs | 8 ++++---- examples/transforms/scale.rs | 4 ++-- examples/transforms/transform.rs | 10 +++++----- examples/transforms/translation.rs | 2 +- examples/ui/button.rs | 2 +- examples/ui/font_atlas_debug.rs | 2 +- examples/ui/text.rs | 4 ++-- examples/ui/text_debug.rs | 2 +- examples/ui/ui.rs | 2 +- examples/window/low_power.rs | 2 +- tests/how_to_test_systems.rs | 4 ++-- 95 files changed, 191 insertions(+), 191 deletions(-) diff --git a/benches/benches/bevy_ecs/iteration/iter_simple_system.rs b/benches/benches/bevy_ecs/iteration/iter_simple_system.rs index 039de135c2..f120b0015f 100644 --- a/benches/benches/bevy_ecs/iteration/iter_simple_system.rs +++ b/benches/benches/bevy_ecs/iteration/iter_simple_system.rs @@ -30,7 +30,7 @@ impl Benchmark { } fn query_system(mut query: Query<(&Velocity, &mut Position)>) { - for (velocity, mut position) in query.iter_mut() { + for (velocity, mut position) in &mut query { position.0 += velocity.0; } } diff --git a/benches/benches/bevy_ecs/world/commands.rs b/benches/benches/bevy_ecs/world/commands.rs index a35e20033b..7688ce1d87 100644 --- a/benches/benches/bevy_ecs/world/commands.rs +++ b/benches/benches/bevy_ecs/world/commands.rs @@ -92,7 +92,7 @@ pub fn insert_commands(criterion: &mut Criterion) { bencher.iter(|| { let mut commands = Commands::new(&mut command_queue, &world); - for entity in entities.iter() { + for entity in &entities { commands .entity(*entity) .insert_bundle((Matrix::default(), Vec3::default())); @@ -112,7 +112,7 @@ pub fn insert_commands(criterion: &mut Criterion) { bencher.iter(|| { let mut commands = Commands::new(&mut command_queue, &world); let mut values = Vec::with_capacity(entity_count); - for entity in entities.iter() { + for entity in &entities { values.push((*entity, (Matrix::default(), Vec3::default()))); } commands.insert_or_spawn_batch(values); diff --git a/benches/benches/bevy_reflect/struct.rs b/benches/benches/bevy_reflect/struct.rs index a879828ab4..613bd13d19 100644 --- a/benches/benches/bevy_reflect/struct.rs +++ b/benches/benches/bevy_reflect/struct.rs @@ -43,7 +43,7 @@ fn concrete_struct_field(criterion: &mut Criterion) { .collect::>(); bencher.iter(|| { - for name in field_names.iter() { + for name in &field_names { s.field(black_box(name)); } }); diff --git a/crates/bevy_animation/src/lib.rs b/crates/bevy_animation/src/lib.rs index 92ecf16c25..ee70d801ef 100644 --- a/crates/bevy_animation/src/lib.rs +++ b/crates/bevy_animation/src/lib.rs @@ -183,7 +183,7 @@ pub fn animation_player( mut transforms: Query<&mut Transform>, children: Query<&Children>, ) { - for (entity, mut player) in animation_players.iter_mut() { + for (entity, mut player) in &mut animation_players { if let Some(animation_clip) = animations.get(&player.animation_clip) { // Continue if paused unless the `AnimationPlayer` was changed // This allow the animation to still be updated if the player.elapsed field was manually updated in pause diff --git a/crates/bevy_asset/src/asset_server.rs b/crates/bevy_asset/src/asset_server.rs index da1fc3b0cb..e9e8576352 100644 --- a/crates/bevy_asset/src/asset_server.rs +++ b/crates/bevy_asset/src/asset_server.rs @@ -110,7 +110,7 @@ impl AssetServer { { let mut loaders = self.server.loaders.write(); let loader_index = loaders.len(); - for extension in loader.extensions().iter() { + for extension in loader.extensions() { self.server .extension_to_loader_index .write() diff --git a/crates/bevy_core_pipeline/src/core_3d/mod.rs b/crates/bevy_core_pipeline/src/core_3d/mod.rs index 9470121bc4..c69e8104dc 100644 --- a/crates/bevy_core_pipeline/src/core_3d/mod.rs +++ b/crates/bevy_core_pipeline/src/core_3d/mod.rs @@ -236,7 +236,7 @@ pub fn prepare_core_3d_depth_textures( >, ) { let mut textures = HashMap::default(); - for (entity, camera) in views_3d.iter() { + for (entity, camera) in &views_3d { if let Some(physical_target_size) = camera.physical_target_size { let cached_texture = textures .entry(camera.target.clone()) diff --git a/crates/bevy_ecs/README.md b/crates/bevy_ecs/README.md index f967a75218..7c8f8f0f30 100644 --- a/crates/bevy_ecs/README.md +++ b/crates/bevy_ecs/README.md @@ -78,7 +78,7 @@ use bevy_ecs::prelude::*; struct Position { x: f32, y: f32 } fn print_position(query: Query<(Entity, &Position)>) { - for (entity, position) in query.iter() { + for (entity, position) in &query { println!("Entity {:?} is at position: x {}, y {}", entity, position.x, position.y); } } @@ -130,7 +130,7 @@ struct Velocity { x: f32, y: f32 } // This system moves each entity with a Position and Velocity component fn movement(mut query: Query<(&mut Position, &Velocity)>) { - for (mut position, velocity) in query.iter_mut() { + for (mut position, velocity) in &mut query { position.x += velocity.x; position.y += velocity.y; } @@ -176,7 +176,7 @@ struct Alive; // Gets the Position component of all Entities with Player component and without the Alive // component. fn system(query: Query<&Position, (With, Without)>) { - for position in query.iter() { + for position in &query { } } ``` @@ -197,13 +197,13 @@ struct Velocity { x: f32, y: f32 } // Gets the Position component of all Entities whose Velocity has changed since the last run of the System fn system_changed(query: Query<&Position, Changed>) { - for position in query.iter() { + for position in &query { } } // Gets the Position component of all Entities that had a Velocity component added since the last run of the System fn system_added(query: Query<&Position, Added>) { - for position in query.iter() { + for position in &query { } } ``` diff --git a/crates/bevy_ecs/examples/change_detection.rs b/crates/bevy_ecs/examples/change_detection.rs index 1a3e135b65..8477a5164f 100644 --- a/crates/bevy_ecs/examples/change_detection.rs +++ b/crates/bevy_ecs/examples/change_detection.rs @@ -79,24 +79,24 @@ fn print_changed_entities( entity_with_added_component: Query>, entity_with_mutated_component: Query<(Entity, &Age), Changed>, ) { - for entity in entity_with_added_component.iter() { + for entity in &entity_with_added_component { println!(" {:?} has it's first birthday!", entity); } - for (entity, value) in entity_with_mutated_component.iter() { + for (entity, value) in &entity_with_mutated_component { println!(" {:?} is now {:?} frames old", entity, value); } } // This system iterates over all entities and increases their age in every frame fn age_all_entities(mut entities: Query<&mut Age>) { - for mut age in entities.iter_mut() { + for mut age in &mut entities { age.frames += 1; } } // This system iterates over all entities in every frame and despawns entities older than 2 frames fn remove_old_entities(mut commands: Commands, entities: Query<(Entity, &Age)>) { - for (entity, age) in entities.iter() { + for (entity, age) in &entities { if age.frames > 2 { println!(" despawning {:?} due to age > 2", entity); commands.entity(entity).despawn(); diff --git a/crates/bevy_ecs/macros/src/fetch.rs b/crates/bevy_ecs/macros/src/fetch.rs index 2bbb08cc98..0b44e3c33c 100644 --- a/crates/bevy_ecs/macros/src/fetch.rs +++ b/crates/bevy_ecs/macros/src/fetch.rs @@ -134,7 +134,7 @@ pub fn derive_world_query_impl(ast: DeriveInput) -> TokenStream { let mut field_idents = Vec::new(); let mut field_types = Vec::new(); - for field in fields.iter() { + for field in fields { let WorldQueryFieldInfo { is_ignored, attrs } = read_world_query_field_info(field); let field_ident = field.ident.as_ref().unwrap().clone(); diff --git a/crates/bevy_ecs/src/entity/mod.rs b/crates/bevy_ecs/src/entity/mod.rs index 2c10a87eb3..e022483fa8 100644 --- a/crates/bevy_ecs/src/entity/mod.rs +++ b/crates/bevy_ecs/src/entity/mod.rs @@ -84,7 +84,7 @@ use std::{ /// # struct Expired; /// # /// fn dispose_expired_food(mut commands: Commands, query: Query>) { -/// for food_entity in query.iter() { +/// for food_entity in &query { /// commands.entity(food_entity).despawn(); /// } /// } diff --git a/crates/bevy_ecs/src/query/fetch.rs b/crates/bevy_ecs/src/query/fetch.rs index 33b3f47529..d2f0541ce2 100644 --- a/crates/bevy_ecs/src/query/fetch.rs +++ b/crates/bevy_ecs/src/query/fetch.rs @@ -79,7 +79,7 @@ use std::{cell::UnsafeCell, marker::PhantomData}; /// } /// /// fn my_system(query: Query) { -/// for q in query.iter() { +/// for q in &query { /// // Note the type of the returned item. /// let q: MyQueryItem<'_> = q; /// q.foo; @@ -130,11 +130,11 @@ use std::{cell::UnsafeCell, marker::PhantomData}; /// /// fn my_system(mut health_query: Query) { /// // Iterator's item is `HealthQueryReadOnlyItem`. -/// for health in health_query.iter() { +/// for health in &health_query { /// println!("Total: {}", health.total()); /// } /// // Iterator's item is `HealthQueryItem`. -/// for mut health in health_query.iter_mut() { +/// for mut health in &mut health_query { /// health.damage(1.0); /// println!("Total (mut): {}", health.total()); /// } @@ -158,7 +158,7 @@ use std::{cell::UnsafeCell, marker::PhantomData}; /// } /// /// fn my_system(mut my_query: Query<(FooReadOnly, FooReadOnly)>) { -/// for (i1, i2) in my_query.iter_mut() { +/// for (i1, i2) in &mut my_query { /// let _: FooReadOnlyItem<'_> = i1; /// let _: FooReadOnlyItem<'_> = i2; /// } @@ -254,7 +254,7 @@ use std::{cell::UnsafeCell, marker::PhantomData}; /// /// // You can also compose derived queries with regular ones in tuples. /// fn my_system(query: Query<(&Foo, MyQuery, FooQuery)>) { -/// for (foo, my_query, foo_query) in query.iter() { +/// for (foo, my_query, foo_query) in &query { /// foo; my_query; foo_query; /// } /// } @@ -279,7 +279,7 @@ use std::{cell::UnsafeCell, marker::PhantomData}; /// } /// /// fn my_system(query: Query) { -/// for _ in query.iter() {} +/// for _ in &query {} /// } /// /// # bevy_ecs::system::assert_is_system(my_system); @@ -311,7 +311,7 @@ use std::{cell::UnsafeCell, marker::PhantomData}; /// } /// /// fn my_system(query: Query>) { -/// for _ in query.iter() {} +/// for _ in &query {} /// } /// /// # bevy_ecs::system::assert_is_system(my_system); @@ -1087,7 +1087,7 @@ unsafe impl<'w, T: Fetch<'w>> Fetch<'w> for OptionFetch { /// # struct Transform {}; /// # /// fn print_moving_objects_system(query: Query<(&Name, ChangeTrackers)>) { -/// for (name, tracker) in query.iter() { +/// for (name, tracker) in &query { /// if tracker.is_changed() { /// println!("Entity moved: {:?}", name); /// } else { diff --git a/crates/bevy_ecs/src/query/filter.rs b/crates/bevy_ecs/src/query/filter.rs index 919b7dd1ec..23392fd49d 100644 --- a/crates/bevy_ecs/src/query/filter.rs +++ b/crates/bevy_ecs/src/query/filter.rs @@ -36,7 +36,7 @@ use super::ReadOnlyWorldQuery; /// # struct Name { name: &'static str }; /// # /// fn compliment_entity_system(query: Query<&Name, With>) { -/// for name in query.iter() { +/// for name in &query { /// println!("{} is looking lovely today!", name.name); /// } /// } @@ -177,7 +177,7 @@ impl Copy for WithFetch {} /// # struct Name { name: &'static str }; /// # /// fn no_permit_system(query: Query<&Name, Without>) { -/// for name in query.iter() { +/// for name in &query{ /// println!("{} has no permit!", name.name); /// } /// } @@ -324,7 +324,7 @@ impl Copy for WithoutFetch {} /// # struct Style {}; /// # /// fn print_cool_entity_system(query: Query, Changed