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 <justthecooldude@gmail.com>
This commit is contained in:
ira 2022-09-20 00:29:10 +00:00
parent 7d5a7cc76d
commit 2b80a3f279
10 changed files with 27 additions and 14 deletions

View file

@ -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)

View file

@ -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(),

View file

@ -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(),

View file

@ -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;
}

View file

@ -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);
}
}

View file

@ -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();
@ -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),)));
}

View file

@ -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<Query<Entity, With<Cloud>>>) {
/// 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()
}
}

View file

@ -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;
}

View file

@ -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;

View file

@ -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;
}