Rename RemovedComponents::iter/iter_with_id to read/read_with_id (#9778)

# Objective

Rename RemovedComponents::iter/iter_with_id to read/read_with_id to make
it clear that it consume the data

Fixes #9755.

(It's my first pull request, if i've made any mistake, please let me
know)

## Solution

Refactor RemovedComponents::iter/iter_with_id to read/read_with_id



## Changelog

Refactor RemovedComponents::iter/iter_with_id to read/read_with_id

Deprecate RemovedComponents::iter/iter_with_id

Remove IntoIterator implementation

Update removal_detection example accordingly

---

## Migration Guide

Rename calls of RemovedComponents::iter/iter_with_id to
read/read_with_id

Replace IntoIterator iteration (&mut <RemovedComponents>) with .read()

---------

Co-authored-by: denshi_ika <mojang2824@gmail.com>
This commit is contained in:
louis-le-cam 2023-09-15 14:37:20 +02:00 committed by GitHub
parent 0607116699
commit 9ee9d627d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 30 additions and 27 deletions

View File

@ -200,7 +200,7 @@ impl<'w, 's, T: Component> RemovedComponents<'w, 's, T> {
/// Iterates over the events this [`RemovedComponents`] has not seen yet. This updates the
/// [`RemovedComponents`]'s event counter, which means subsequent event reads will not include events
/// that happened before now.
pub fn iter(&mut self) -> RemovedIter<'_> {
pub fn read(&mut self) -> RemovedIter<'_> {
self.reader_mut_with_events()
.map(|(reader, events)| reader.read(events).cloned())
.into_iter()
@ -208,8 +208,16 @@ impl<'w, 's, T: Component> RemovedComponents<'w, 's, T> {
.map(RemovedComponentEntity::into)
}
/// Like [`iter`](Self::iter), except also returning the [`EventId`] of the events.
pub fn iter_with_id(&mut self) -> RemovedIterWithId<'_> {
/// Iterates over the events this [`RemovedComponents`] has not seen yet. This updates the
/// [`RemovedComponents`]'s event counter, which means subsequent event reads will not include events
/// that happened before now.
#[deprecated = "use `.read()` instead."]
pub fn iter(&mut self) -> RemovedIter<'_> {
self.read()
}
/// Like [`read`](Self::read), except also returning the [`EventId`] of the events.
pub fn read_with_id(&mut self) -> RemovedIterWithId<'_> {
self.reader_mut_with_events()
.map(|(reader, events)| reader.read_with_id(events))
.into_iter()
@ -217,6 +225,12 @@ impl<'w, 's, T: Component> RemovedComponents<'w, 's, T> {
.map(map_id_events)
}
/// Like [`iter`](Self::iter), except also returning the [`EventId`] of the events.
#[deprecated = "use `.read_with_id()` instead."]
pub fn iter_with_id(&mut self) -> RemovedIterWithId<'_> {
self.read_with_id()
}
/// Determines the number of removal events available to be read from this [`RemovedComponents`] without consuming any.
pub fn len(&self) -> usize {
self.events()
@ -233,8 +247,8 @@ impl<'w, 's, T: Component> RemovedComponents<'w, 's, T> {
/// Consumes all available events.
///
/// This means these events will not appear in calls to [`RemovedComponents::iter()`] or
/// [`RemovedComponents::iter_with_id()`] and [`RemovedComponents::is_empty()`] will return `true`.
/// This means these events will not appear in calls to [`RemovedComponents::read()`] or
/// [`RemovedComponents::read_with_id()`] and [`RemovedComponents::is_empty()`] will return `true`.
pub fn clear(&mut self) {
if let Some((reader, events)) = self.reader_mut_with_events() {
reader.clear(events);
@ -242,17 +256,6 @@ impl<'w, 's, T: Component> RemovedComponents<'w, 's, T> {
}
}
impl<'a, 'w, 's: 'a, T> IntoIterator for &'a mut RemovedComponents<'w, 's, T>
where
T: Component,
{
type Item = Entity;
type IntoIter = RemovedIter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
// SAFETY: Only reads World removed component events
unsafe impl<'a> ReadOnlySystemParam for &'a RemovedComponentEvents {}

View File

@ -943,7 +943,7 @@ pub mod common_conditions {
// Simply checking `is_empty` would not be enough.
// PERF: note that `count` is efficient (not actually looping/iterating),
// due to Bevy having a specialized implementation for events.
move |mut removals: RemovedComponents<T>| removals.iter().count() != 0
move |mut removals: RemovedComponents<T>| removals.read().count() != 0
}
/// Generates a [`Condition`](super::Condition) that inverses the result of passed one.

View File

@ -1172,7 +1172,7 @@ mod tests {
mut n_systems: ResMut<NSystems>,
) {
assert_eq!(
removed_i32.iter().collect::<Vec<_>>(),
removed_i32.read().collect::<Vec<_>>(),
&[despawned.0],
"despawning causes the correct entity to show up in the 'RemovedComponent' system parameter."
);
@ -1200,7 +1200,7 @@ mod tests {
// The despawned entity from the previous frame was
// double buffered so we now have it in this system as well.
assert_eq!(
removed_i32.iter().collect::<Vec<_>>(),
removed_i32.read().collect::<Vec<_>>(),
&[despawned.0, removed.0],
"removing a component causes the correct entity to show up in the 'RemovedComponent' system parameter."
);

View File

@ -34,7 +34,7 @@ pub fn sync_simple_transforms(
});
// Update orphaned entities.
let mut query = query.p1();
let mut iter = query.iter_many_mut(orphaned.iter());
let mut iter = query.iter_many_mut(orphaned.read());
while let Some((transform, mut global_transform)) = iter.fetch_next() {
if !transform.is_changed() && !global_transform.is_added() {
*global_transform = GlobalTransform::from(*transform);
@ -57,7 +57,7 @@ pub fn propagate_transforms(
mut orphaned_entities: Local<Vec<Entity>>,
) {
orphaned_entities.clear();
orphaned_entities.extend(orphaned.iter());
orphaned_entities.extend(orphaned.read());
orphaned_entities.sort_unstable();
root_query.par_iter_mut().for_each(
|(entity, children, transform, mut global_transform)| {

View File

@ -281,10 +281,10 @@ pub fn ui_layout_system(
}
// clean up removed nodes
ui_surface.remove_entities(removed_nodes.iter());
ui_surface.remove_entities(removed_nodes.read());
// When a `ContentSize` component is removed from an entity, we need to remove the measure from the corresponding taffy node.
for entity in removed_content_sizes.iter() {
for entity in removed_content_sizes.read() {
ui_surface.try_remove_measure(entity);
}
@ -292,7 +292,7 @@ pub fn ui_layout_system(
ui_surface.set_window_children(primary_window_entity, root_node_query.iter());
// update and remove children
for entity in removed_children.iter() {
for entity in removed_children.read() {
ui_surface.try_remove_children(entity);
}
for (entity, children) in &children_query {

View File

@ -110,7 +110,7 @@ pub(crate) fn despawn_windows(
mut close_events: EventWriter<WindowClosed>,
mut winit_windows: NonSendMut<WinitWindows>,
) {
for window in closed.iter() {
for window in closed.read() {
info!("Closing window {:?}", window);
// Guard to verify that the window is in fact actually gone,
// rather than having the component added and removed in the same frame.

View File

@ -51,9 +51,9 @@ fn remove_component(
}
fn react_on_removal(mut removed: RemovedComponents<MyComponent>, mut query: Query<&mut Sprite>) {
// `RemovedComponents<T>::iter()` returns an iterator with the `Entity`s that had their
// `RemovedComponents<T>::read()` returns an iterator with the `Entity`s that had their
// `Component` `T` (in this case `MyComponent`) removed at some point earlier during the frame.
for entity in &mut removed {
for entity in removed.read() {
if let Ok(mut sprite) = query.get_mut(entity) {
sprite.color.set_r(0.0);
}