Skip to content

Commit

Permalink
Implement IntoIterator for &Extract<P> (#6025)
Browse files Browse the repository at this point in the history
# 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 <justthecooldude@gmail.com>
  • Loading branch information
tim-blackbird and tim-blackbird committed Sep 20, 2022
1 parent 7d5a7cc commit 2b80a3f
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/core_2d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub fn extract_core_2d_camera_phases(
mut commands: Commands,
cameras_2d: Extract<Query<(Entity, &Camera), With<Camera2d>>>,
) {
for (entity, camera) in cameras_2d.iter() {
for (entity, camera) in &cameras_2d {
if camera.is_active {
commands
.get_or_spawn(entity)
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/core_3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ pub fn extract_core_3d_camera_phases(
mut commands: Commands,
cameras_3d: Extract<Query<(Entity, &Camera), With<Camera3d>>>,
) {
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::<Opaque3d>::default(),
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ pub fn extract_clusters(
mut commands: Commands,
views: Extract<Query<(Entity, &Clusters), With<Camera>>>,
) {
for (entity, clusters) in views.iter() {
for (entity, clusters) in &views {
commands.get_or_spawn(entity).insert_bundle((
ExtractedClustersPointLights {
data: clusters.lights.clone(),
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/wireframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Plugin for WireframePlugin {
}

fn extract_wireframes(mut commands: Commands, query: Extract<Query<Entity, With<Wireframe>>>) {
for entity in query.iter() {
for entity in &query {
commands.get_or_spawn(entity).insert(Wireframe);
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_render/src/extract_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ impl<T: Asset> ExtractComponent for Handle<T> {
fn extract_components<C: ExtractComponent>(
mut commands: Commands,
mut previous_len: Local<usize>,
mut query: Extract<Query<(Entity, C::Query), C::Filter>>,
query: Extract<Query<(Entity, C::Query), C::Filter>>,
) {
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();
Expand All @@ -197,10 +197,10 @@ fn extract_components<C: ExtractComponent>(
fn extract_visible_components<C: ExtractComponent>(
mut commands: Commands,
mut previous_len: Local<usize>,
mut query: Extract<Query<(Entity, &ComputedVisibility, C::Query), C::Filter>>,
query: Extract<Query<(Entity, &ComputedVisibility, C::Query), C::Filter>>,
) {
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),)));
}
Expand Down
17 changes: 15 additions & 2 deletions crates/bevy_render/src/extract_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy_ecs::{
prelude::*,
system::{
ReadOnlySystemParamFetch, ResState, SystemMeta, SystemParam, SystemParamFetch,
SystemParamState, SystemState,
SystemParamItem, SystemParamState, SystemState,
},
};
use std::ops::{Deref, DerefMut};
Expand Down Expand Up @@ -34,7 +34,7 @@ use std::ops::{Deref, DerefMut};
/// # #[derive(Component)]
/// # struct Cloud;
/// fn extract_clouds(mut commands: Commands, clouds: Extract<Query<Entity, With<Cloud>>>) {
/// for cloud in clouds.iter() {
/// for cloud in &clouds {
/// commands.get_or_spawn(cloud).insert(Cloud);
/// }
/// }
Expand Down Expand Up @@ -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()
}
}
2 changes: 1 addition & 1 deletion crates/bevy_sprite/src/mesh2d/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub fn extract_mesh2d(
query: Extract<Query<(Entity, &ComputedVisibility, &GlobalTransform, &Mesh2dHandle)>>,
) {
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;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ pub fn extract_default_ui_camera_view<T: Component>(
mut commands: Commands,
query: Extract<Query<(Entity, &Camera, Option<&UiCameraConfig>), With<T>>>,
) {
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;
Expand Down
2 changes: 1 addition & 1 deletion examples/2d/mesh2d_manual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ pub fn extract_colored_mesh2d(
query: Extract<Query<(Entity, &ComputedVisibility), With<ColoredMesh2d>>>,
) {
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;
}
Expand Down

0 comments on commit 2b80a3f

Please sign in to comment.