diff --git a/crates/bevy_ecs/src/removal_detection.rs b/crates/bevy_ecs/src/removal_detection.rs index 5d92c5ee7a8f02..be274895912cd0 100644 --- a/crates/bevy_ecs/src/removal_detection.rs +++ b/crates/bevy_ecs/src/removal_detection.rs @@ -12,9 +12,10 @@ use bevy_ecs_macros::SystemParam; use std::{ fmt::Debug, - iter::{Cloned, Flatten}, + iter, marker::PhantomData, ops::{Deref, DerefMut}, + option, }; /// Wrapper around a [`ManualEventReader`] so that we @@ -117,7 +118,8 @@ pub struct RemovedComponents<'w, 's, T: Component> { event_sets: &'w RemovedComponentEvents, } -type RemovedIter<'a> = Flatten>>>; +type RemovedIter<'a> = + iter::Flatten>>>; impl<'w, 's, T: Component> RemovedComponents<'w, 's, T> { pub fn iter(&mut self) -> RemovedIter<'_> { diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index b84603495f4770..1df42b352882ee 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -1322,7 +1322,7 @@ impl<'a> SystemParam for &'a RemovedComponentEvents { type State = RemovedComponentEventsState; } -// SAFETY: Only reads World components +// SAFETY: Only reads World removed component events unsafe impl<'a> ReadOnlySystemParam for &'a RemovedComponentEvents {} /// The [`SystemParamState`] of [`RemovedComponentEvents`]. diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 9089d43a9c63f4..de8b9dcd92d5f9 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -742,12 +742,12 @@ impl World { /// Returns an iterator of entities that had components of type `T` removed /// since the last call to [`World::clear_trackers`]. - pub fn removed(&self) -> Box + '_> { - if let Some(component_id) = self.components.get_id(TypeId::of::()) { - Box::new(self.removed_with_id(component_id)) - } else { - Box::new(std::iter::empty()) - } + pub fn removed(&self) -> impl DoubleEndedIterator + '_ { + self.components + .get_id(TypeId::of::()) + .map(|component_id| self.removed_with_id(component_id)) + .into_iter() + .flatten() } /// Returns an iterator of entities that had components with the given `component_id` removed @@ -755,12 +755,12 @@ impl World { pub fn removed_with_id( &self, component_id: ComponentId, - ) -> Box + '_> { - if let Some(removed) = self.removed_components.get(component_id) { - Box::new(removed.iter_current_update_events().cloned()) - } else { - Box::new(std::iter::empty()) - } + ) -> impl DoubleEndedIterator + '_ { + self.removed_components + .get(component_id) + .map(|removed| removed.iter_current_update_events().cloned()) + .into_iter() + .flatten() } /// Inserts a new resource with standard starting values.