-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Replace manual iterator exhaust with for_each(drop) #48945
Conversation
r? @TimNN (rust_highfive has picked a reviewer for you, use r? to override) |
049a71c
to
f109596
Compare
Re-assigning to @rust-lang/libs: r? @Kimundi
I think that would kind of defeat the purpose of the method proposed here: Not dropping / consuming the iterator seems to be an explicit goal. If consuming the iterator was the goal either |
Note that |
And putting it all together with iter.by_ref().for_each(drop);
unsafe { free(self.ptr); } I rather like the looks of what we can already write. |
Is this operation common enough to warrant a dedicated method in |
I think I agree that as_ref + for_each sufficiently covers this kind of thing. |
☔ The latest upstream changes (presumably #47813) made this pull request unmergeable. Please resolve the merge conflicts. |
(If there is a motion to close I'll open a new PR to replace the instances that I changed to |
Ping from triage @rust-lang/libs! Can we get a review/decision on this PR? |
Sorry, seems to have slipped under my radar. I think that I agree with @ExpHP that I personally don't have any strong feelings about wether to add the new method or not, but I know that something like |
@Kimundi Can we make this a matter of documentation then and include text about |
It feels mure like a "common patterns" document more than the doc comment. Is there a good place for that in one of the books? RBE is on doc.RLO now; maybe there? |
cc @steveklabnik ^ |
f109596
to
5c58eec
Compare
I've replaced instances of the method with |
@@ -1129,7 +1129,7 @@ impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V> { | |||
|
|||
impl<'a, K: 'a, V: 'a> Drop for Drain<'a, K, V> { | |||
fn drop(&mut self) { | |||
for _ in self {} | |||
self.for_each(drop); |
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.
Ooh, nice that this just works without needing .by_ref()
🙂
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.
When I saw this comment my immediate thought was to hope that the CI actually passed, because I was pretty sure it would work but not 100%. :p
Ping from triage @Kimundi! If I understood correctly this PR still needs your review. |
Ping from triage! Can @Kimundi (or someone else from @rust-lang/libs) review this? |
@bors r+ |
📌 Commit 5c58eec has been approved by |
Replace manual iterator exhaust with for_each(drop) This originally added a dedicated method, `Iterator::exhaust`, and has since been replaced with `for_each(drop)`, which is more idiomatic. <del>This is just shorthand for `for _ in &mut self {}` or `while let Some(_) = self.next() {}`. This states the intent a lot more clearly than the identical code: run the iterator to completion. <del>At least personally, my eyes tend to gloss over `for _ in &mut self {}` without fully paying attention to what it does; having a `Drop` implementation akin to: <del>`for _ in &mut self {}; unsafe { free(self.ptr); }`</del> <del>Is not as clear as: <del>`self.exhaust(); unsafe { free(self.ptr); }` <del>Additionally, I've seen debate over whether `while let Some(_) = self.next() {}` or `for _ in &mut self {}` is more clear, whereas `self.exhaust()` is clearer than both.
☀️ Test successful - status-appveyor, status-travis |
This originally added a dedicated method,
Iterator::exhaust
, and has since been replaced withfor_each(drop)
, which is more idiomatic.This is just shorthand forfor _ in &mut self {}
orwhile let Some(_) = self.next() {}
. This states the intent a lot more clearly than the identical code: run the iterator to completion.At least personally, my eyes tend to gloss overfor _ in &mut self {}
without fully paying attention to what it does; having aDrop
implementation akin to:for _ in &mut self {}; unsafe { free(self.ptr); }
Is not as clear as:self.exhaust(); unsafe { free(self.ptr); }
Additionally, I've seen debate over whetherwhile let Some(_) = self.next() {}
orfor _ in &mut self {}
is more clear, whereasself.exhaust()
is clearer than both.