From 4e9eb880edd79e43715791803022b50da0b1a307 Mon Sep 17 00:00:00 2001 From: devil-ira Date: Sat, 25 Jun 2022 12:43:50 +0200 Subject: [PATCH 1/6] Implement IntoIterator for ECS wrapper types. --- crates/bevy_ecs/src/change_detection.rs | 52 ++++++++++++++++++++++ crates/bevy_ecs/src/system/system_param.rs | 39 ++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/crates/bevy_ecs/src/change_detection.rs b/crates/bevy_ecs/src/change_detection.rs index 914b1951376bf..5fbd85f8a1ed5 100644 --- a/crates/bevy_ecs/src/change_detection.rs +++ b/crates/bevy_ecs/src/change_detection.rs @@ -246,6 +246,32 @@ pub struct ResMut<'a, T: ?Sized + Resource> { pub(crate) ticks: Ticks<'a>, } +impl<'w, 'a, T: Resource> IntoIterator for &'a ResMut<'w, T> +where + &'a T: IntoIterator, +{ + type Item = <&'a T as IntoIterator>::Item; + + type IntoIter = <&'a T as IntoIterator>::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.value.into_iter() + } +} + +impl<'w, 'a, T: Resource> IntoIterator for &'a mut ResMut<'w, T> +where + &'a mut T: IntoIterator, +{ + type Item = <&'a mut T as IntoIterator>::Item; + + type IntoIter = <&'a mut T as IntoIterator>::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.value.into_iter() + } +} + change_detection_impl!(ResMut<'a, T>, T, Resource); impl_methods!(ResMut<'a, T>, T, Resource); impl_debug!(ResMut<'a, T>, Resource); @@ -299,6 +325,32 @@ pub struct Mut<'a, T: ?Sized> { pub(crate) ticks: Ticks<'a>, } +impl<'w, 'a, T: Resource> IntoIterator for &'a Mut<'w, T> +where + &'a T: IntoIterator, +{ + type Item = <&'a T as IntoIterator>::Item; + + type IntoIter = <&'a T as IntoIterator>::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.value.into_iter() + } +} + +impl<'w, 'a, T: Resource> IntoIterator for &'a mut Mut<'w, T> +where + &'a mut T: IntoIterator, +{ + type Item = <&'a mut T as IntoIterator>::Item; + + type IntoIter = <&'a mut T as IntoIterator>::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.value.into_iter() + } +} + change_detection_impl!(Mut<'a, T>, T,); impl_methods!(Mut<'a, T>, T,); impl_debug!(Mut<'a, T>,); diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index 6f57b2dd0e4a6..73fbf0c24799d 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -344,6 +344,19 @@ impl<'w, T: Resource> From> for Res<'w, T> { } } +impl<'w, 'a, T: Resource> IntoIterator for &'a Res<'w, T> +where + &'a T: IntoIterator, +{ + type Item = <&'a T as IntoIterator>::Item; + + type IntoIter = <&'a T as IntoIterator>::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.value.into_iter() + } +} + /// The [`SystemParamState`] of [`Res`]. #[doc(hidden)] pub struct ResState { @@ -721,6 +734,32 @@ impl<'a, T: FromWorld + Send + Sync + 'static> DerefMut for Local<'a, T> { } } +impl<'w, 'a, T: Resource> IntoIterator for &'a Local<'w, T> +where + &'a T: IntoIterator, +{ + type Item = <&'a T as IntoIterator>::Item; + + type IntoIter = <&'a T as IntoIterator>::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } +} + +impl<'w, 'a, T: Resource> IntoIterator for &'a mut Local<'w, T> +where + &'a mut T: IntoIterator, +{ + type Item = <&'a mut T as IntoIterator>::Item; + + type IntoIter = <&'a mut T as IntoIterator>::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } +} + /// The [`SystemParamState`] of [`Local`]. #[doc(hidden)] pub struct LocalState(pub(crate) SyncCell); From 33077c30e1f2c2f7fa81141103077bccacd7ca69 Mon Sep 17 00:00:00 2001 From: devil-ira Date: Sun, 3 Jul 2022 21:01:57 +0200 Subject: [PATCH 2/6] Use it! --- crates/bevy_pbr/src/light.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_pbr/src/light.rs b/crates/bevy_pbr/src/light.rs index 4d2fc0dca26db..344fff88cb358 100644 --- a/crates/bevy_pbr/src/light.rs +++ b/crates/bevy_pbr/src/light.rs @@ -971,7 +971,7 @@ pub(crate) fn assign_lights_to_clusters( if config.dynamic_resizing() { let mut cluster_index_estimate = 0.0; - for light in lights.iter() { + for light in &lights { let light_sphere = light.sphere(); // Check if the light is within the view frustum @@ -1126,7 +1126,7 @@ pub(crate) fn assign_lights_to_clusters( } let mut update_from_light_intersections = |visible_lights: &mut Vec| { - for light in lights.iter() { + for light in &lights { let light_sphere = light.sphere(); // Check if the light is within the view frustum From db9e405ef8c0cc040146fe6b408f0e8c5b8b0681 Mon Sep 17 00:00:00 2001 From: devil-ira Date: Sun, 3 Jul 2022 22:30:25 +0200 Subject: [PATCH 3/6] Remove type bound on IntoIterator impl for Mut --- crates/bevy_ecs/src/change_detection.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/src/change_detection.rs b/crates/bevy_ecs/src/change_detection.rs index 5fbd85f8a1ed5..b9b2a5bfac358 100644 --- a/crates/bevy_ecs/src/change_detection.rs +++ b/crates/bevy_ecs/src/change_detection.rs @@ -325,7 +325,7 @@ pub struct Mut<'a, T: ?Sized> { pub(crate) ticks: Ticks<'a>, } -impl<'w, 'a, T: Resource> IntoIterator for &'a Mut<'w, T> +impl<'w, 'a, T> IntoIterator for &'a Mut<'w, T> where &'a T: IntoIterator, { @@ -338,7 +338,7 @@ where } } -impl<'w, 'a, T: Resource> IntoIterator for &'a mut Mut<'w, T> +impl<'w, 'a, T> IntoIterator for &'a mut Mut<'w, T> where &'a mut T: IntoIterator, { From 497db127e2bd8d1caf79460ee061d58fdbba6d29 Mon Sep 17 00:00:00 2001 From: devil-ira Date: Fri, 21 Oct 2022 17:31:37 +0200 Subject: [PATCH 4/6] Don't use `Resource` trait bound for `Local` --- crates/bevy_ecs/src/system/system_param.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index 73fbf0c24799d..fff135b03ef4c 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -734,7 +734,7 @@ impl<'a, T: FromWorld + Send + Sync + 'static> DerefMut for Local<'a, T> { } } -impl<'w, 'a, T: Resource> IntoIterator for &'a Local<'w, T> +impl<'w, 'a, T: FromWorld + Send + 'static> IntoIterator for &'a Local<'w, T> where &'a T: IntoIterator, { @@ -747,7 +747,7 @@ where } } -impl<'w, 'a, T: Resource> IntoIterator for &'a mut Local<'w, T> +impl<'w, 'a, T: FromWorld + Send + 'static> IntoIterator for &'a mut Local<'w, T> where &'a mut T: IntoIterator, { From 3d80b9aeff91607e3e0f5a62f37a26550b621ae7 Mon Sep 17 00:00:00 2001 From: devil-ira Date: Fri, 21 Oct 2022 17:40:04 +0200 Subject: [PATCH 5/6] Precious vertical space --- crates/bevy_ecs/src/change_detection.rs | 4 ---- crates/bevy_ecs/src/system/system_param.rs | 3 --- 2 files changed, 7 deletions(-) diff --git a/crates/bevy_ecs/src/change_detection.rs b/crates/bevy_ecs/src/change_detection.rs index b9b2a5bfac358..1924a32a573a9 100644 --- a/crates/bevy_ecs/src/change_detection.rs +++ b/crates/bevy_ecs/src/change_detection.rs @@ -251,7 +251,6 @@ where &'a T: IntoIterator, { type Item = <&'a T as IntoIterator>::Item; - type IntoIter = <&'a T as IntoIterator>::IntoIter; fn into_iter(self) -> Self::IntoIter { @@ -264,7 +263,6 @@ where &'a mut T: IntoIterator, { type Item = <&'a mut T as IntoIterator>::Item; - type IntoIter = <&'a mut T as IntoIterator>::IntoIter; fn into_iter(self) -> Self::IntoIter { @@ -330,7 +328,6 @@ where &'a T: IntoIterator, { type Item = <&'a T as IntoIterator>::Item; - type IntoIter = <&'a T as IntoIterator>::IntoIter; fn into_iter(self) -> Self::IntoIter { @@ -343,7 +340,6 @@ where &'a mut T: IntoIterator, { type Item = <&'a mut T as IntoIterator>::Item; - type IntoIter = <&'a mut T as IntoIterator>::IntoIter; fn into_iter(self) -> Self::IntoIter { diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index fff135b03ef4c..09b7e85c19e78 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -349,7 +349,6 @@ where &'a T: IntoIterator, { type Item = <&'a T as IntoIterator>::Item; - type IntoIter = <&'a T as IntoIterator>::IntoIter; fn into_iter(self) -> Self::IntoIter { @@ -739,7 +738,6 @@ where &'a T: IntoIterator, { type Item = <&'a T as IntoIterator>::Item; - type IntoIter = <&'a T as IntoIterator>::IntoIter; fn into_iter(self) -> Self::IntoIter { @@ -752,7 +750,6 @@ where &'a mut T: IntoIterator, { type Item = <&'a mut T as IntoIterator>::Item; - type IntoIter = <&'a mut T as IntoIterator>::IntoIter; fn into_iter(self) -> Self::IntoIter { From 6c2d9bf4e324c1f5b1e20d9b4ea0693184a02127 Mon Sep 17 00:00:00 2001 From: devil-ira Date: Fri, 21 Oct 2022 17:45:05 +0200 Subject: [PATCH 6/6] Oopsie --- crates/bevy_ecs/src/change_detection.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/bevy_ecs/src/change_detection.rs b/crates/bevy_ecs/src/change_detection.rs index 1924a32a573a9..e0fcc845f3796 100644 --- a/crates/bevy_ecs/src/change_detection.rs +++ b/crates/bevy_ecs/src/change_detection.rs @@ -266,6 +266,7 @@ where type IntoIter = <&'a mut T as IntoIterator>::IntoIter; fn into_iter(self) -> Self::IntoIter { + self.set_changed(); self.value.into_iter() } } @@ -343,6 +344,7 @@ where type IntoIter = <&'a mut T as IntoIterator>::IntoIter; fn into_iter(self) -> Self::IntoIter { + self.set_changed(); self.value.into_iter() } }