1
Fork 0

Auto merge of #48945 - clarcharr:iter_exhaust, r=Kimundi

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.
This commit is contained in:
bors 2018-04-16 13:21:56 +00:00
commit 1ef1563518
6 changed files with 9 additions and 13 deletions

View file

@ -2521,7 +2521,7 @@ impl<'a, T> DoubleEndedIterator for Drain<'a, T> {
impl<'a, T> Drop for Drain<'a, T> {
fn drop(&mut self) {
// exhaust self first
while let Some(_) = self.next() {}
self.for_each(drop);
if self.tail_len > 0 {
unsafe {
@ -2590,9 +2590,7 @@ impl<'a, I: Iterator> ExactSizeIterator for Splice<'a, I> {}
#[stable(feature = "vec_splice", since = "1.21.0")]
impl<'a, I: Iterator> Drop for Splice<'a, I> {
fn drop(&mut self) {
// exhaust drain first
while let Some(_) = self.drain.next() {}
self.drain.by_ref().for_each(drop);
unsafe {
if self.drain.tail_len == 0 {
@ -2721,8 +2719,7 @@ impl<'a, T, F> Drop for DrainFilter<'a, T, F>
where F: FnMut(&mut T) -> bool,
{
fn drop(&mut self) {
for _ in self.by_ref() { }
self.for_each(drop);
unsafe {
self.vec.set_len(self.old_len - self.del);
}