Skip to content

Commit

Permalink
Improve safety comment, don't box unnecessarily
Browse files Browse the repository at this point in the history
  • Loading branch information
Aceeri committed Jan 5, 2023
1 parent df404c2 commit f3bf55e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
6 changes: 4 additions & 2 deletions crates/bevy_ecs/src/removal_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity>`] so that we
Expand Down Expand Up @@ -117,7 +118,8 @@ pub struct RemovedComponents<'w, 's, T: Component> {
event_sets: &'w RemovedComponentEvents,
}

type RemovedIter<'a> = Flatten<std::option::IntoIter<Cloned<ManualEventIterator<'a, Entity>>>>;
type RemovedIter<'a> =
iter::Flatten<option::IntoIter<iter::Cloned<ManualEventIterator<'a, Entity>>>>;

impl<'w, 's, T: Component> RemovedComponents<'w, 's, T> {
pub fn iter(&mut self) -> RemovedIter<'_> {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/system/system_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down
24 changes: 12 additions & 12 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,25 +742,25 @@ 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<T: Component>(&self) -> Box<dyn Iterator<Item = Entity> + '_> {
if let Some(component_id) = self.components.get_id(TypeId::of::<T>()) {
Box::new(self.removed_with_id(component_id))
} else {
Box::new(std::iter::empty())
}
pub fn removed<T: Component>(&self) -> impl DoubleEndedIterator<Item = Entity> + '_ {
self.components
.get_id(TypeId::of::<T>())
.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
/// since the last call to [`World::clear_trackers`].
pub fn removed_with_id(
&self,
component_id: ComponentId,
) -> Box<dyn Iterator<Item = Entity> + '_> {
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<Item = Entity> + '_ {
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.
Expand Down

0 comments on commit f3bf55e

Please sign in to comment.