From b30ba78e16ce37b6c15f4e2b75083087570407c4 Mon Sep 17 00:00:00 2001 From: CatThingy Date: Sun, 5 Feb 2023 15:37:07 +0000 Subject: [PATCH] Add a wrapper around `Entity` for `RemovedComponents` (#7503) # Objective - Make the internals of `RemovedComponents` clearer ## Solution - Add a wrapper around `Entity`, used in `RemovedComponents` as `Events` --- ## Changelog - `RemovedComponents` now internally uses an `Events` instead of an `Events` --- crates/bevy_ecs/src/removal_detection.rs | 35 ++++++++++++++++++------ crates/bevy_ecs/src/world/mod.rs | 1 + 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/crates/bevy_ecs/src/removal_detection.rs b/crates/bevy_ecs/src/removal_detection.rs index e0e0bd7430cde..f9953ff77cf9e 100644 --- a/crates/bevy_ecs/src/removal_detection.rs +++ b/crates/bevy_ecs/src/removal_detection.rs @@ -19,14 +19,25 @@ use std::{ option, }; -/// Wrapper around a [`ManualEventReader`] so that we +/// Wrapper around [`Entity`] for [`RemovedComponents`]. +/// Internally, `RemovedComponents` uses these as an `Events`. +#[derive(Debug, Clone)] +pub struct RemovedComponentEntity(Entity); + +impl From for Entity { + fn from(value: RemovedComponentEntity) -> Self { + value.0 + } +} + +/// Wrapper around a [`ManualEventReader`] so that we /// can differentiate events between components. #[derive(Debug)] pub struct RemovedComponentReader where T: Component, { - reader: ManualEventReader, + reader: ManualEventReader, marker: PhantomData, } @@ -40,7 +51,7 @@ impl Default for RemovedComponentReader { } impl Deref for RemovedComponentReader { - type Target = ManualEventReader; + type Target = ManualEventReader; fn deref(&self) -> &Self::Target { &self.reader } @@ -52,11 +63,11 @@ impl DerefMut for RemovedComponentReader { } } -/// Wrapper around a map of components to [`Events`]. +/// Wrapper around a map of components to [`Events`]. /// So that we can find the events without naming the type directly. #[derive(Default, Debug)] pub struct RemovedComponentEvents { - event_sets: SparseSet>, + event_sets: SparseSet>, } impl RemovedComponentEvents { @@ -70,14 +81,17 @@ impl RemovedComponentEvents { } } - pub fn get(&self, component_id: impl Into) -> Option<&Events> { + pub fn get( + &self, + component_id: impl Into, + ) -> Option<&Events> { self.event_sets.get(component_id.into()) } pub fn send(&mut self, component_id: impl Into, entity: Entity) { self.event_sets .get_or_insert_with(component_id.into(), Default::default) - .send(entity); + .send(RemovedComponentEntity(entity)); } } @@ -122,8 +136,10 @@ pub struct RemovedComponents<'w, 's, T: Component> { /// Iterator over entities that had a specific component removed. /// /// See [`RemovedComponents`]. -pub type RemovedIter<'a> = - iter::Flatten>>>; +pub type RemovedIter<'a> = iter::Map< + iter::Flatten>>>, + fn(RemovedComponentEntity) -> Entity, +>; impl<'w, 's, T: Component> RemovedComponents<'w, 's, T> { pub fn iter(&mut self) -> RemovedIter<'_> { @@ -132,6 +148,7 @@ impl<'w, 's, T: Component> RemovedComponents<'w, 's, T> { .map(|events| self.reader.iter(events).cloned()) .into_iter() .flatten() + .map(RemovedComponentEntity::into) } } diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 981accba96f87..ced43f4002094 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -788,6 +788,7 @@ impl World { .map(|removed| removed.iter_current_update_events().cloned()) .into_iter() .flatten() + .map(|e| e.into()) } /// Initializes a new resource and returns the [`ComponentId`] created for it.