Skip to content

Commit 1ef1563

Browse files
committed
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.
2 parents d6a2dd9 + 5c58eec commit 1ef1563

File tree

6 files changed

+9
-13
lines changed

6 files changed

+9
-13
lines changed

src/liballoc/btree/map.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1287,8 +1287,7 @@ impl<K, V> IntoIterator for BTreeMap<K, V> {
12871287
#[stable(feature = "btree_drop", since = "1.7.0")]
12881288
impl<K, V> Drop for IntoIter<K, V> {
12891289
fn drop(&mut self) {
1290-
for _ in &mut *self {
1291-
}
1290+
self.for_each(drop);
12921291
unsafe {
12931292
let leaf_node = ptr::read(&self.front).into_node();
12941293
if let Some(first_parent) = leaf_node.deallocate_and_ascend() {

src/liballoc/linked_list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ impl<'a, T, F> Drop for DrainFilter<'a, T, F>
10191019
where F: FnMut(&mut T) -> bool,
10201020
{
10211021
fn drop(&mut self) {
1022-
for _ in self { }
1022+
self.for_each(drop);
10231023
}
10241024
}
10251025

src/liballoc/vec.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -2521,7 +2521,7 @@ impl<'a, T> DoubleEndedIterator for Drain<'a, T> {
25212521
impl<'a, T> Drop for Drain<'a, T> {
25222522
fn drop(&mut self) {
25232523
// exhaust self first
2524-
while let Some(_) = self.next() {}
2524+
self.for_each(drop);
25252525

25262526
if self.tail_len > 0 {
25272527
unsafe {
@@ -2590,9 +2590,7 @@ impl<'a, I: Iterator> ExactSizeIterator for Splice<'a, I> {}
25902590
#[stable(feature = "vec_splice", since = "1.21.0")]
25912591
impl<'a, I: Iterator> Drop for Splice<'a, I> {
25922592
fn drop(&mut self) {
2593-
// exhaust drain first
2594-
while let Some(_) = self.drain.next() {}
2595-
2593+
self.drain.by_ref().for_each(drop);
25962594

25972595
unsafe {
25982596
if self.drain.tail_len == 0 {
@@ -2721,8 +2719,7 @@ impl<'a, T, F> Drop for DrainFilter<'a, T, F>
27212719
where F: FnMut(&mut T) -> bool,
27222720
{
27232721
fn drop(&mut self) {
2724-
for _ in self.by_ref() { }
2725-
2722+
self.for_each(drop);
27262723
unsafe {
27272724
self.vec.set_len(self.old_len - self.del);
27282725
}

src/liballoc/vec_deque.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2250,7 +2250,7 @@ unsafe impl<'a, T: Send> Send for Drain<'a, T> {}
22502250
#[stable(feature = "drain", since = "1.6.0")]
22512251
impl<'a, T: 'a> Drop for Drain<'a, T> {
22522252
fn drop(&mut self) {
2253-
for _ in self.by_ref() {}
2253+
self.for_each(drop);
22542254

22552255
let source_deque = unsafe { self.deque.as_mut() };
22562256

src/librustc_data_structures/array_vec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ pub struct Iter<A: Array> {
207207

208208
impl<A: Array> Drop for Iter<A> {
209209
fn drop(&mut self) {
210-
for _ in self {}
210+
self.for_each(drop);
211211
}
212212
}
213213

@@ -251,7 +251,7 @@ impl<'a, A: Array> Iterator for Drain<'a, A> {
251251
impl<'a, A: Array> Drop for Drain<'a, A> {
252252
fn drop(&mut self) {
253253
// exhaust self first
254-
while let Some(_) = self.next() {}
254+
self.for_each(drop);
255255

256256
if self.tail_len > 0 {
257257
unsafe {

src/libstd/collections/hash/table.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V> {
11241124

11251125
impl<'a, K: 'a, V: 'a> Drop for Drain<'a, K, V> {
11261126
fn drop(&mut self) {
1127-
for _ in self {}
1127+
self.for_each(drop);
11281128
}
11291129
}
11301130

0 commit comments

Comments
 (0)