Skip to content

Commit 2b80a3f

Browse files
committed
Implement IntoIterator for &Extract<P> (#6025)
# Objective Implement `IntoIterator` for `&Extract<P>` if the system parameter it wraps implements `IntoIterator`. Enables the use of `IntoIterator` with an extracted query. Co-authored-by: devil-ira <[email protected]>
1 parent 7d5a7cc commit 2b80a3f

File tree

10 files changed

+27
-14
lines changed

10 files changed

+27
-14
lines changed

crates/bevy_core_pipeline/src/core_2d/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub fn extract_core_2d_camera_phases(
128128
mut commands: Commands,
129129
cameras_2d: Extract<Query<(Entity, &Camera), With<Camera2d>>>,
130130
) {
131-
for (entity, camera) in cameras_2d.iter() {
131+
for (entity, camera) in &cameras_2d {
132132
if camera.is_active {
133133
commands
134134
.get_or_spawn(entity)

crates/bevy_core_pipeline/src/core_3d/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ pub fn extract_core_3d_camera_phases(
211211
mut commands: Commands,
212212
cameras_3d: Extract<Query<(Entity, &Camera), With<Camera3d>>>,
213213
) {
214-
for (entity, camera) in cameras_3d.iter() {
214+
for (entity, camera) in &cameras_3d {
215215
if camera.is_active {
216216
commands.get_or_spawn(entity).insert_bundle((
217217
RenderPhase::<Opaque3d>::default(),

crates/bevy_pbr/src/render/light.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ pub fn extract_clusters(
393393
mut commands: Commands,
394394
views: Extract<Query<(Entity, &Clusters), With<Camera>>>,
395395
) {
396-
for (entity, clusters) in views.iter() {
396+
for (entity, clusters) in &views {
397397
commands.get_or_spawn(entity).insert_bundle((
398398
ExtractedClustersPointLights {
399399
data: clusters.lights.clone(),

crates/bevy_pbr/src/render/mesh.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ pub fn extract_skinned_meshes(
229229
let mut joints = Vec::with_capacity(*previous_joint_len);
230230
let mut last_start = 0;
231231

232-
for (entity, computed_visibility, skin) in query.iter() {
232+
for (entity, computed_visibility, skin) in &query {
233233
if !computed_visibility.is_visible() {
234234
continue;
235235
}

crates/bevy_pbr/src/wireframe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl Plugin for WireframePlugin {
5252
}
5353

5454
fn extract_wireframes(mut commands: Commands, query: Extract<Query<Entity, With<Wireframe>>>) {
55-
for entity in query.iter() {
55+
for entity in &query {
5656
commands.get_or_spawn(entity).insert(Wireframe);
5757
}
5858
}

crates/bevy_render/src/extract_component.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ impl<T: Asset> ExtractComponent for Handle<T> {
183183
fn extract_components<C: ExtractComponent>(
184184
mut commands: Commands,
185185
mut previous_len: Local<usize>,
186-
mut query: Extract<Query<(Entity, C::Query), C::Filter>>,
186+
query: Extract<Query<(Entity, C::Query), C::Filter>>,
187187
) {
188188
let mut values = Vec::with_capacity(*previous_len);
189-
for (entity, query_item) in query.iter_mut() {
189+
for (entity, query_item) in &query {
190190
values.push((entity, (C::extract_component(query_item),)));
191191
}
192192
*previous_len = values.len();
@@ -197,10 +197,10 @@ fn extract_components<C: ExtractComponent>(
197197
fn extract_visible_components<C: ExtractComponent>(
198198
mut commands: Commands,
199199
mut previous_len: Local<usize>,
200-
mut query: Extract<Query<(Entity, &ComputedVisibility, C::Query), C::Filter>>,
200+
query: Extract<Query<(Entity, &ComputedVisibility, C::Query), C::Filter>>,
201201
) {
202202
let mut values = Vec::with_capacity(*previous_len);
203-
for (entity, computed_visibility, query_item) in query.iter_mut() {
203+
for (entity, computed_visibility, query_item) in &query {
204204
if computed_visibility.is_visible() {
205205
values.push((entity, (C::extract_component(query_item),)));
206206
}

crates/bevy_render/src/extract_param.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use bevy_ecs::{
33
prelude::*,
44
system::{
55
ReadOnlySystemParamFetch, ResState, SystemMeta, SystemParam, SystemParamFetch,
6-
SystemParamState, SystemState,
6+
SystemParamItem, SystemParamState, SystemState,
77
},
88
};
99
use std::ops::{Deref, DerefMut};
@@ -34,7 +34,7 @@ use std::ops::{Deref, DerefMut};
3434
/// # #[derive(Component)]
3535
/// # struct Cloud;
3636
/// fn extract_clouds(mut commands: Commands, clouds: Extract<Query<Entity, With<Cloud>>>) {
37-
/// for cloud in clouds.iter() {
37+
/// for cloud in &clouds {
3838
/// commands.get_or_spawn(cloud).insert(Cloud);
3939
/// }
4040
/// }
@@ -118,3 +118,16 @@ where
118118
&mut self.item
119119
}
120120
}
121+
122+
impl<'a, 'w, 's, P: SystemParam> IntoIterator for &'a Extract<'w, 's, P>
123+
where
124+
P::Fetch: ReadOnlySystemParamFetch,
125+
&'a SystemParamItem<'w, 's, P>: IntoIterator,
126+
{
127+
type Item = <&'a SystemParamItem<'w, 's, P> as IntoIterator>::Item;
128+
type IntoIter = <&'a SystemParamItem<'w, 's, P> as IntoIterator>::IntoIter;
129+
130+
fn into_iter(self) -> Self::IntoIter {
131+
(&self.item).into_iter()
132+
}
133+
}

crates/bevy_sprite/src/mesh2d/mesh.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub fn extract_mesh2d(
127127
query: Extract<Query<(Entity, &ComputedVisibility, &GlobalTransform, &Mesh2dHandle)>>,
128128
) {
129129
let mut values = Vec::with_capacity(*previous_len);
130-
for (entity, computed_visibility, transform, handle) in query.iter() {
130+
for (entity, computed_visibility, transform, handle) in &query {
131131
if !computed_visibility.is_visible() {
132132
continue;
133133
}

crates/bevy_ui/src/render/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ pub fn extract_default_ui_camera_view<T: Component>(
233233
mut commands: Commands,
234234
query: Extract<Query<(Entity, &Camera, Option<&UiCameraConfig>), With<T>>>,
235235
) {
236-
for (entity, camera, camera_ui) in query.iter() {
236+
for (entity, camera, camera_ui) in &query {
237237
// ignore cameras with disabled ui
238238
if matches!(camera_ui, Some(&UiCameraConfig { show_ui: false, .. })) {
239239
continue;

examples/2d/mesh2d_manual.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ pub fn extract_colored_mesh2d(
294294
query: Extract<Query<(Entity, &ComputedVisibility), With<ColoredMesh2d>>>,
295295
) {
296296
let mut values = Vec::with_capacity(*previous_len);
297-
for (entity, computed_visibility) in query.iter() {
297+
for (entity, computed_visibility) in &query {
298298
if !computed_visibility.is_visible() {
299299
continue;
300300
}

0 commit comments

Comments
 (0)