-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
alice-i-cecile
merged 1 commit into
bevyengine:main
from
nicopap:remove-impl-mut-iter-events
Aug 29, 2023
Merged
Remove IntoIterator
impl for &mut EventReader
#9583
alice-i-cecile
merged 1 commit into
bevyengine:main
from
nicopap:remove-impl-mut-iter-events
Aug 29, 2023
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
JoJoJet
approved these changes
Aug 26, 2023
There was a problem hiding this 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.
tguichaoua
approved these changes
Aug 29, 2023
nicopap
force-pushed
the
remove-impl-mut-iter-events
branch
from
August 29, 2023 08:15
13bc45a
to
574990e
Compare
harudagondi
approved these changes
Aug 29, 2023
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Objective
The latest
clippy
release has a much more aggressive application of theexplicit_iter_loop
pedantic lint.As a result, clippy now suggests the following:
I'm generally in favor of this lint. Using
for mut item in &mut query
is also recommended overfor mut item in query.iter_mut()
for good reasons IMO.But, it is my personal belief that
&mut events
is much less clear thanevents.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 mutableEventReader
because it updates the internallast_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 iteratingEventReader
s, which makes the ecosystem every-so-slightly better.Changelog
IntoIterator
impl for&mut EventReader
Migration Guide
&mut EventReader
does not implementIntoIterator
anymore. replacefor foo in &mut events
byfor foo in events.iter()