Remove IntoIterator impl for &mut EventReader#9583
Merged
alice-i-cecile merged 1 commit intobevyengine:mainfrom Aug 29, 2023
Merged
Remove IntoIterator impl for &mut EventReader#9583alice-i-cecile merged 1 commit intobevyengine:mainfrom
IntoIterator impl for &mut EventReader#9583alice-i-cecile merged 1 commit intobevyengine:mainfrom
Conversation
joseph-gio
approved these changes
Aug 26, 2023
Member
joseph-gio
left a comment
There was a problem hiding this comment.
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
13bc45a to
574990e
Compare
harudagondi
approved these changes
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()`
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
clippyrelease has a much more aggressive application of theexplicit_iter_looppedantic lint.As a result, clippy now suggests the following:
I'm generally in favor of this lint. Using
for mut item in &mut queryis also recommended overfor mut item in query.iter_mut()for good reasons IMO.But, it is my personal belief that
&mut eventsis much less clear thanevents.iter().Why? The reason is that the events from
EventReaderare not mutable, they are immutable references to each event in the event reader.&mut eventssuggests we are getting mutable access to events — similarly to&mut query— which is not the case. Using&mut eventsis therefore misleading.IntoIteratorrequires a mutableEventReaderbecause 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
IntoIteratorfor&mut events.Without the impl, clippy won't suggest its "fix". This also prevents generally people from using
&mut eventsfor iteratingEventReaders, which makes the ecosystem every-so-slightly better.Changelog
IntoIteratorimpl for&mut EventReaderMigration Guide
&mut EventReaderdoes not implementIntoIteratoranymore. replacefor foo in &mut eventsbyfor foo in events.iter()