Skip to content

Commit b30ba78

Browse files
committed
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<RemovedComponentsEntity>` --- ## Changelog - `RemovedComponents` now internally uses an `Events<RemovedComponentsEntity>` instead of an `Events<Entity>`
1 parent 32023a5 commit b30ba78

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

crates/bevy_ecs/src/removal_detection.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,25 @@ use std::{
1919
option,
2020
};
2121

22-
/// Wrapper around a [`ManualEventReader<Entity>`] so that we
22+
/// Wrapper around [`Entity`] for [`RemovedComponents`].
23+
/// Internally, `RemovedComponents` uses these as an `Events<RemovedComponentEntity>`.
24+
#[derive(Debug, Clone)]
25+
pub struct RemovedComponentEntity(Entity);
26+
27+
impl From<RemovedComponentEntity> for Entity {
28+
fn from(value: RemovedComponentEntity) -> Self {
29+
value.0
30+
}
31+
}
32+
33+
/// Wrapper around a [`ManualEventReader<RemovedComponentEntity>`] so that we
2334
/// can differentiate events between components.
2435
#[derive(Debug)]
2536
pub struct RemovedComponentReader<T>
2637
where
2738
T: Component,
2839
{
29-
reader: ManualEventReader<Entity>,
40+
reader: ManualEventReader<RemovedComponentEntity>,
3041
marker: PhantomData<T>,
3142
}
3243

@@ -40,7 +51,7 @@ impl<T: Component> Default for RemovedComponentReader<T> {
4051
}
4152

4253
impl<T: Component> Deref for RemovedComponentReader<T> {
43-
type Target = ManualEventReader<Entity>;
54+
type Target = ManualEventReader<RemovedComponentEntity>;
4455
fn deref(&self) -> &Self::Target {
4556
&self.reader
4657
}
@@ -52,11 +63,11 @@ impl<T: Component> DerefMut for RemovedComponentReader<T> {
5263
}
5364
}
5465

55-
/// Wrapper around a map of components to [`Events<Entity>`].
66+
/// Wrapper around a map of components to [`Events<RemovedComponentEntity>`].
5667
/// So that we can find the events without naming the type directly.
5768
#[derive(Default, Debug)]
5869
pub struct RemovedComponentEvents {
59-
event_sets: SparseSet<ComponentId, Events<Entity>>,
70+
event_sets: SparseSet<ComponentId, Events<RemovedComponentEntity>>,
6071
}
6172

6273
impl RemovedComponentEvents {
@@ -70,14 +81,17 @@ impl RemovedComponentEvents {
7081
}
7182
}
7283

73-
pub fn get(&self, component_id: impl Into<ComponentId>) -> Option<&Events<Entity>> {
84+
pub fn get(
85+
&self,
86+
component_id: impl Into<ComponentId>,
87+
) -> Option<&Events<RemovedComponentEntity>> {
7488
self.event_sets.get(component_id.into())
7589
}
7690

7791
pub fn send(&mut self, component_id: impl Into<ComponentId>, entity: Entity) {
7892
self.event_sets
7993
.get_or_insert_with(component_id.into(), Default::default)
80-
.send(entity);
94+
.send(RemovedComponentEntity(entity));
8195
}
8296
}
8397

@@ -122,8 +136,10 @@ pub struct RemovedComponents<'w, 's, T: Component> {
122136
/// Iterator over entities that had a specific component removed.
123137
///
124138
/// See [`RemovedComponents`].
125-
pub type RemovedIter<'a> =
126-
iter::Flatten<option::IntoIter<iter::Cloned<ManualEventIterator<'a, Entity>>>>;
139+
pub type RemovedIter<'a> = iter::Map<
140+
iter::Flatten<option::IntoIter<iter::Cloned<ManualEventIterator<'a, RemovedComponentEntity>>>>,
141+
fn(RemovedComponentEntity) -> Entity,
142+
>;
127143

128144
impl<'w, 's, T: Component> RemovedComponents<'w, 's, T> {
129145
pub fn iter(&mut self) -> RemovedIter<'_> {
@@ -132,6 +148,7 @@ impl<'w, 's, T: Component> RemovedComponents<'w, 's, T> {
132148
.map(|events| self.reader.iter(events).cloned())
133149
.into_iter()
134150
.flatten()
151+
.map(RemovedComponentEntity::into)
135152
}
136153
}
137154

crates/bevy_ecs/src/world/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ impl World {
788788
.map(|removed| removed.iter_current_update_events().cloned())
789789
.into_iter()
790790
.flatten()
791+
.map(|e| e.into())
791792
}
792793

793794
/// Initializes a new resource and returns the [`ComponentId`] created for it.

0 commit comments

Comments
 (0)