Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upReplace manual iterator exhaust with for_each(drop) #48945
Conversation
rust-highfive
assigned
TimNN
Mar 12, 2018
This comment has been minimized.
This comment has been minimized.
|
r? @TimNN (rust_highfive has picked a reviewer for you, use r? to override) |
rust-highfive
added
the
S-waiting-on-review
label
Mar 12, 2018
clarfon
force-pushed the
clarfon:iter_exhaust
branch
5 times, most recently
from
049a71c
to
f109596
Mar 12, 2018
kennytm
added
the
T-libs
label
Mar 12, 2018
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
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 |
rust-highfive
assigned
Kimundi
and unassigned
TimNN
Mar 12, 2018
This comment has been minimized.
This comment has been minimized.
|
Note that |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
Is this operation common enough to warrant a dedicated method in |
This comment has been minimized.
This comment has been minimized.
|
I think I agree that as_ref + for_each sufficiently covers this kind of thing. |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
(If there is a motion to close I'll open a new PR to replace the instances that I changed to |
This comment has been minimized.
This comment has been minimized.
|
Ping from triage @rust-lang/libs! Can we get a review/decision on this PR? |
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
|
@Kimundi Can we make this a matter of documentation then and include text about |
This comment has been minimized.
This comment has been minimized.
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? |
This comment has been minimized.
This comment has been minimized.
|
cc @steveklabnik ^ |
clarfon
force-pushed the
clarfon:iter_exhaust
branch
from
f109596
to
5c58eec
Apr 4, 2018
clarfon
changed the title
Add Iterator::exhaust.
Replace manual iterator exhaust with for_each(drop)
Apr 4, 2018
This comment has been minimized.
This comment has been minimized.
|
I've replaced instances of the method with |
scottmcm
approved these changes
Apr 4, 2018
| @@ -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); | |||
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
clarfon
Apr 5, 2018
Author
Contributor
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
This comment has been minimized.
This comment has been minimized.
|
Ping from triage @Kimundi! If I understood correctly this PR still needs your review. |
This comment has been minimized.
This comment has been minimized.
|
Ping from triage! Can @Kimundi (or someone else from @rust-lang/libs) review this? |
This comment has been minimized.
This comment has been minimized.
|
@bors r+ |
This comment has been minimized.
This comment has been minimized.
|
|
bors
added
S-waiting-on-bors
and removed
S-waiting-on-review
labels
Apr 16, 2018
This comment has been minimized.
This comment has been minimized.
bors
added a commit
that referenced
this pull request
Apr 16, 2018
This comment has been minimized.
This comment has been minimized.
|
|
clarfon commentedMar 12, 2018
•
edited
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 aDropimplementation 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.