Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Add a wrapper around Entity for RemovedComponents #7503

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we improve these docs? Maybe try to explain how they're used in events?

/// 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 @@ -791,6 +791,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