Skip to content

Commit

Permalink
Add a wrapper around Entity for RemovedComponents (bevyengine#7503)
Browse files Browse the repository at this point in the history
# 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>`
  • Loading branch information
CatThingy committed Feb 5, 2023
1 parent 32023a5 commit b30ba78
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
35 changes: 26 additions & 9 deletions crates/bevy_ecs/src/removal_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,25 @@ use std::{
option,
};

/// Wrapper around a [`ManualEventReader<Entity>`] so that we
/// Wrapper around [`Entity`] for [`RemovedComponents`].
/// Internally, `RemovedComponents` uses these as an `Events<RemovedComponentEntity>`.
#[derive(Debug, Clone)]
pub struct RemovedComponentEntity(Entity);

impl From<RemovedComponentEntity> for Entity {
fn from(value: RemovedComponentEntity) -> Self {
value.0
}
}

/// Wrapper around a [`ManualEventReader<RemovedComponentEntity>`] so that we
/// can differentiate events between components.
#[derive(Debug)]
pub struct RemovedComponentReader<T>
where
T: Component,
{
reader: ManualEventReader<Entity>,
reader: ManualEventReader<RemovedComponentEntity>,
marker: PhantomData<T>,
}

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

impl<T: Component> Deref for RemovedComponentReader<T> {
type Target = ManualEventReader<Entity>;
type Target = ManualEventReader<RemovedComponentEntity>;
fn deref(&self) -> &Self::Target {
&self.reader
}
Expand All @@ -52,11 +63,11 @@ impl<T: Component> DerefMut for RemovedComponentReader<T> {
}
}

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

impl RemovedComponentEvents {
Expand All @@ -70,14 +81,17 @@ impl RemovedComponentEvents {
}
}

pub fn get(&self, component_id: impl Into<ComponentId>) -> Option<&Events<Entity>> {
pub fn get(
&self,
component_id: impl Into<ComponentId>,
) -> Option<&Events<RemovedComponentEntity>> {
self.event_sets.get(component_id.into())
}

pub fn send(&mut self, component_id: impl Into<ComponentId>, entity: Entity) {
self.event_sets
.get_or_insert_with(component_id.into(), Default::default)
.send(entity);
.send(RemovedComponentEntity(entity));
}
}

Expand Down Expand Up @@ -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<option::IntoIter<iter::Cloned<ManualEventIterator<'a, Entity>>>>;
pub type RemovedIter<'a> = iter::Map<
iter::Flatten<option::IntoIter<iter::Cloned<ManualEventIterator<'a, RemovedComponentEntity>>>>,
fn(RemovedComponentEntity) -> Entity,
>;

impl<'w, 's, T: Component> RemovedComponents<'w, 's, T> {
pub fn iter(&mut self) -> RemovedIter<'_> {
Expand All @@ -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)
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit b30ba78

Please sign in to comment.