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

Remove IntoIterator impl for &mut EventReader #9583

Merged
merged 1 commit into from
Aug 29, 2023

Conversation

nicopap
Copy link
Contributor

@nicopap nicopap commented Aug 26, 2023

Objective

The latest clippy release has a much more aggressive application of the explicit_iter_loop pedantic lint.

As a result, clippy now suggests the following:

-for event in events.iter() {
+for event in &mut events {

I'm generally in favor of this lint. Using for mut item in &mut query is also recommended over for mut item in query.iter_mut() for good reasons IMO.

But, it is my personal belief that &mut events is much less clear than events.iter().

Why? The reason is that the events from EventReader are not mutable, they are immutable references to each event in the event reader. &mut events suggests we are getting mutable access to events — similarly to &mut query — which is not the case. Using &mut events is therefore misleading.

IntoIterator requires a mutable EventReader because it updates the internal last_event_count, not because it let you mutate it.

So clippy's suggested improvement is a downgrade.

Solution

Do not implement IntoIterator for &mut events.

Without the impl, clippy won't suggest its "fix". This also prevents generally people from using &mut events for iterating EventReaders, which makes the ecosystem every-so-slightly better.


Changelog

  • Removed IntoIterator impl for &mut EventReader

Migration Guide

  • &mut EventReader does not implement IntoIterator anymore. replace for foo in &mut events by for foo in events.iter()

@nicopap nicopap added C-Code-Quality A section of code that is hard to understand or change M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide labels Aug 26, 2023
Copy link
Member

@JoJoJet JoJoJet left a comment

Choose a reason for hiding this comment

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

Agreed. I am also in favor of renaming .iter() to something clearer like .read(). Having a function named .iter() that modifies state has always rubbed me the wrong way.

@nicopap nicopap force-pushed the remove-impl-mut-iter-events branch from 13bc45a to 574990e Compare August 29, 2023 08:15
@harudagondi harudagondi added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Aug 29, 2023
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Aug 29, 2023
Merged via the queue into bevyengine:main with commit 4f212a5 Aug 29, 2023
21 checks passed
@nicopap nicopap deleted the remove-impl-mut-iter-events branch August 30, 2023 13:35
rdrpenguin04 pushed a commit to rdrpenguin04/bevy that referenced this pull request Jan 9, 2024
# Objective

The latest `clippy` release has a much more aggressive application of
the
[`explicit_iter_loop`](https://rust-lang.github.io/rust-clippy/master/index.html#/explicit_into_iter_loop?groups=pedantic)
pedantic lint.

As a result, clippy now suggests the following:

```diff
-for event in events.iter() {
+for event in &mut events {
```

I'm generally in favor of this lint. Using `for mut item in &mut query`
is also recommended over `for mut item in query.iter_mut()` for good
reasons IMO.

But, it is my personal belief that `&mut events` is much less clear than
`events.iter()`.

Why? The reason is that the events from `EventReader` **are not
mutable**, they are immutable references to each event in the event
reader. `&mut events` suggests we are getting mutable access to events —
similarly to `&mut query` — which is not the case. Using `&mut events`
is therefore misleading.

`IntoIterator` requires a mutable `EventReader` because it updates the
internal `last_event_count`, not because it let you mutate it.

So clippy's suggested improvement is a downgrade.

## Solution

Do not implement `IntoIterator` for `&mut events`.

Without the impl, clippy won't suggest its "fix". This also prevents
generally people from using `&mut events` for iterating `EventReader`s,
which makes the ecosystem every-so-slightly better.

---

## Changelog

- Removed `IntoIterator` impl for `&mut EventReader`

## Migration Guide

- `&mut EventReader` does not implement `IntoIterator` anymore. replace
`for foo in &mut events` by `for foo in events.iter()`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Code-Quality A section of code that is hard to understand or change M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants