From 2b80a3f279d1273fbcface9e075637fe681a38d2 Mon Sep 17 00:00:00 2001 From: ira Date: Tue, 20 Sep 2022 00:29:10 +0000 Subject: [PATCH] Implement `IntoIterator` for `&Extract

` (#6025) # Objective Implement `IntoIterator` for `&Extract

` if the system parameter it wraps implements `IntoIterator`. Enables the use of `IntoIterator` with an extracted query. Co-authored-by: devil-ira --- crates/bevy_core_pipeline/src/core_2d/mod.rs | 2 +- crates/bevy_core_pipeline/src/core_3d/mod.rs | 2 +- crates/bevy_pbr/src/render/light.rs | 2 +- crates/bevy_pbr/src/render/mesh.rs | 2 +- crates/bevy_pbr/src/wireframe.rs | 2 +- crates/bevy_render/src/extract_component.rs | 8 ++++---- crates/bevy_render/src/extract_param.rs | 17 +++++++++++++++-- crates/bevy_sprite/src/mesh2d/mesh.rs | 2 +- crates/bevy_ui/src/render/mod.rs | 2 +- examples/2d/mesh2d_manual.rs | 2 +- 10 files changed, 27 insertions(+), 14 deletions(-) diff --git a/crates/bevy_core_pipeline/src/core_2d/mod.rs b/crates/bevy_core_pipeline/src/core_2d/mod.rs index 6db7ef7665893..cc0ddc184746b 100644 --- a/crates/bevy_core_pipeline/src/core_2d/mod.rs +++ b/crates/bevy_core_pipeline/src/core_2d/mod.rs @@ -128,7 +128,7 @@ pub fn extract_core_2d_camera_phases( mut commands: Commands, cameras_2d: Extract>>, ) { - for (entity, camera) in cameras_2d.iter() { + for (entity, camera) in &cameras_2d { if camera.is_active { commands .get_or_spawn(entity) diff --git a/crates/bevy_core_pipeline/src/core_3d/mod.rs b/crates/bevy_core_pipeline/src/core_3d/mod.rs index a78528b46f0f5..9fa91d0a55d29 100644 --- a/crates/bevy_core_pipeline/src/core_3d/mod.rs +++ b/crates/bevy_core_pipeline/src/core_3d/mod.rs @@ -211,7 +211,7 @@ pub fn extract_core_3d_camera_phases( mut commands: Commands, cameras_3d: Extract>>, ) { - for (entity, camera) in cameras_3d.iter() { + for (entity, camera) in &cameras_3d { if camera.is_active { commands.get_or_spawn(entity).insert_bundle(( RenderPhase::::default(), diff --git a/crates/bevy_pbr/src/render/light.rs b/crates/bevy_pbr/src/render/light.rs index 9a68bc4be8656..10b811a3aad0d 100644 --- a/crates/bevy_pbr/src/render/light.rs +++ b/crates/bevy_pbr/src/render/light.rs @@ -393,7 +393,7 @@ pub fn extract_clusters( mut commands: Commands, views: Extract>>, ) { - for (entity, clusters) in views.iter() { + for (entity, clusters) in &views { commands.get_or_spawn(entity).insert_bundle(( ExtractedClustersPointLights { data: clusters.lights.clone(), diff --git a/crates/bevy_pbr/src/render/mesh.rs b/crates/bevy_pbr/src/render/mesh.rs index f5e9a9ae8519b..539af6393bfa9 100644 --- a/crates/bevy_pbr/src/render/mesh.rs +++ b/crates/bevy_pbr/src/render/mesh.rs @@ -229,7 +229,7 @@ pub fn extract_skinned_meshes( let mut joints = Vec::with_capacity(*previous_joint_len); let mut last_start = 0; - for (entity, computed_visibility, skin) in query.iter() { + for (entity, computed_visibility, skin) in &query { if !computed_visibility.is_visible() { continue; } diff --git a/crates/bevy_pbr/src/wireframe.rs b/crates/bevy_pbr/src/wireframe.rs index 6d08adb3025ad..0872241c3a0ba 100644 --- a/crates/bevy_pbr/src/wireframe.rs +++ b/crates/bevy_pbr/src/wireframe.rs @@ -52,7 +52,7 @@ impl Plugin for WireframePlugin { } fn extract_wireframes(mut commands: Commands, query: Extract>>) { - for entity in query.iter() { + for entity in &query { commands.get_or_spawn(entity).insert(Wireframe); } } diff --git a/crates/bevy_render/src/extract_component.rs b/crates/bevy_render/src/extract_component.rs index f57c07b6a2c00..38a9b597fb762 100644 --- a/crates/bevy_render/src/extract_component.rs +++ b/crates/bevy_render/src/extract_component.rs @@ -183,10 +183,10 @@ impl ExtractComponent for Handle { fn extract_components( mut commands: Commands, mut previous_len: Local, - mut query: Extract>, + query: Extract>, ) { let mut values = Vec::with_capacity(*previous_len); - for (entity, query_item) in query.iter_mut() { + for (entity, query_item) in &query { values.push((entity, (C::extract_component(query_item),))); } *previous_len = values.len(); @@ -197,10 +197,10 @@ fn extract_components( fn extract_visible_components( mut commands: Commands, mut previous_len: Local, - mut query: Extract>, + query: Extract>, ) { let mut values = Vec::with_capacity(*previous_len); - for (entity, computed_visibility, query_item) in query.iter_mut() { + for (entity, computed_visibility, query_item) in &query { if computed_visibility.is_visible() { values.push((entity, (C::extract_component(query_item),))); } diff --git a/crates/bevy_render/src/extract_param.rs b/crates/bevy_render/src/extract_param.rs index 37e76a20e2bed..767c0a74a3ae1 100644 --- a/crates/bevy_render/src/extract_param.rs +++ b/crates/bevy_render/src/extract_param.rs @@ -3,7 +3,7 @@ use bevy_ecs::{ prelude::*, system::{ ReadOnlySystemParamFetch, ResState, SystemMeta, SystemParam, SystemParamFetch, - SystemParamState, SystemState, + SystemParamItem, SystemParamState, SystemState, }, }; use std::ops::{Deref, DerefMut}; @@ -34,7 +34,7 @@ use std::ops::{Deref, DerefMut}; /// # #[derive(Component)] /// # struct Cloud; /// fn extract_clouds(mut commands: Commands, clouds: Extract>>) { -/// for cloud in clouds.iter() { +/// for cloud in &clouds { /// commands.get_or_spawn(cloud).insert(Cloud); /// } /// } @@ -118,3 +118,16 @@ where &mut self.item } } + +impl<'a, 'w, 's, P: SystemParam> IntoIterator for &'a Extract<'w, 's, P> +where + P::Fetch: ReadOnlySystemParamFetch, + &'a SystemParamItem<'w, 's, P>: IntoIterator, +{ + type Item = <&'a SystemParamItem<'w, 's, P> as IntoIterator>::Item; + type IntoIter = <&'a SystemParamItem<'w, 's, P> as IntoIterator>::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + (&self.item).into_iter() + } +} diff --git a/crates/bevy_sprite/src/mesh2d/mesh.rs b/crates/bevy_sprite/src/mesh2d/mesh.rs index 54927a0077031..b2248be595d77 100644 --- a/crates/bevy_sprite/src/mesh2d/mesh.rs +++ b/crates/bevy_sprite/src/mesh2d/mesh.rs @@ -127,7 +127,7 @@ pub fn extract_mesh2d( query: Extract>, ) { let mut values = Vec::with_capacity(*previous_len); - for (entity, computed_visibility, transform, handle) in query.iter() { + for (entity, computed_visibility, transform, handle) in &query { if !computed_visibility.is_visible() { continue; } diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index cb7c24182fe9d..34a2502089f85 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -233,7 +233,7 @@ pub fn extract_default_ui_camera_view( mut commands: Commands, query: Extract), With>>, ) { - for (entity, camera, camera_ui) in query.iter() { + for (entity, camera, camera_ui) in &query { // ignore cameras with disabled ui if matches!(camera_ui, Some(&UiCameraConfig { show_ui: false, .. })) { continue; diff --git a/examples/2d/mesh2d_manual.rs b/examples/2d/mesh2d_manual.rs index cdd31c8f93e42..a83e6cfd2fd0f 100644 --- a/examples/2d/mesh2d_manual.rs +++ b/examples/2d/mesh2d_manual.rs @@ -294,7 +294,7 @@ pub fn extract_colored_mesh2d( query: Extract>>, ) { let mut values = Vec::with_capacity(*previous_len); - for (entity, computed_visibility) in query.iter() { + for (entity, computed_visibility) in &query { if !computed_visibility.is_visible() { continue; }