diff options
| author | bors <bors@rust-lang.org> | 2018-04-16 13:21:56 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-04-16 13:21:56 +0000 |
| commit | 1ef1563518d48ad9231b3ec3ac463d34d819ed28 (patch) | |
| tree | 1fb05811590e2701a4d1875d9326261e655cfeec | |
| parent | d6a2dd9912e762fb0029c4463002d674686c1159 (diff) | |
| parent | 5c58eec0bd8cee8fb2a191396d5ad5b5c9b0116a (diff) | |
| download | rust-1ef1563518d48ad9231b3ec3ac463d34d819ed28.tar.gz rust-1ef1563518d48ad9231b3ec3ac463d34d819ed28.zip | |
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.
| -rw-r--r-- | src/liballoc/btree/map.rs | 3 | ||||
| -rw-r--r-- | src/liballoc/linked_list.rs | 2 | ||||
| -rw-r--r-- | src/liballoc/vec.rs | 9 | ||||
| -rw-r--r-- | src/liballoc/vec_deque.rs | 2 | ||||
| -rw-r--r-- | src/librustc_data_structures/array_vec.rs | 4 | ||||
| -rw-r--r-- | src/libstd/collections/hash/table.rs | 2 |
6 files changed, 9 insertions, 13 deletions
diff --git a/src/liballoc/btree/map.rs b/src/liballoc/btree/map.rs index c604df7049e..82cbec0517e 100644 --- a/src/liballoc/btree/map.rs +++ b/src/liballoc/btree/map.rs @@ -1287,8 +1287,7 @@ impl<K, V> IntoIterator for BTreeMap<K, V> { #[stable(feature = "btree_drop", since = "1.7.0")] impl<K, V> Drop for IntoIter<K, V> { fn drop(&mut self) { - for _ in &mut *self { - } + self.for_each(drop); unsafe { let leaf_node = ptr::read(&self.front).into_node(); if let Some(first_parent) = leaf_node.deallocate_and_ascend() { diff --git a/src/liballoc/linked_list.rs b/src/liballoc/linked_list.rs index 129b3bc6764..9844de9a57d 100644 --- a/src/liballoc/linked_list.rs +++ b/src/liballoc/linked_list.rs @@ -1019,7 +1019,7 @@ impl<'a, T, F> Drop for DrainFilter<'a, T, F> where F: FnMut(&mut T) -> bool, { fn drop(&mut self) { - for _ in self { } + self.for_each(drop); } } diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 9ae415c328b..8ab25dfe7d8 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -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); } diff --git a/src/liballoc/vec_deque.rs b/src/liballoc/vec_deque.rs index 603e38ca2ca..ff82b3a469c 100644 --- a/src/liballoc/vec_deque.rs +++ b/src/liballoc/vec_deque.rs @@ -2250,7 +2250,7 @@ unsafe impl<'a, T: Send> Send for Drain<'a, T> {} #[stable(feature = "drain", since = "1.6.0")] impl<'a, T: 'a> Drop for Drain<'a, T> { fn drop(&mut self) { - for _ in self.by_ref() {} + self.for_each(drop); let source_deque = unsafe { self.deque.as_mut() }; diff --git a/src/librustc_data_structures/array_vec.rs b/src/librustc_data_structures/array_vec.rs index db1cfb5c767..7b33ee40d8c 100644 --- a/src/librustc_data_structures/array_vec.rs +++ b/src/librustc_data_structures/array_vec.rs @@ -207,7 +207,7 @@ pub struct Iter<A: Array> { impl<A: Array> Drop for Iter<A> { fn drop(&mut self) { - for _ in self {} + self.for_each(drop); } } @@ -251,7 +251,7 @@ impl<'a, A: Array> Iterator for Drain<'a, A> { impl<'a, A: Array> Drop for Drain<'a, A> { fn drop(&mut self) { // exhaust self first - while let Some(_) = self.next() {} + self.for_each(drop); if self.tail_len > 0 { unsafe { diff --git a/src/libstd/collections/hash/table.rs b/src/libstd/collections/hash/table.rs index 93f059076d7..115f9628a23 100644 --- a/src/libstd/collections/hash/table.rs +++ b/src/libstd/collections/hash/table.rs @@ -1124,7 +1124,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); } } |
