Skip to content

Commit 7e8437f

Browse files
author
Markus Westerlind
committed
refactor: Factor out dropping of iterator elements
1 parent 908fe2d commit 7e8437f

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/raw/mod.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,6 +1968,14 @@ impl<T> RawIter<T> {
19681968
}
19691969
}
19701970
}
1971+
1972+
unsafe fn drop_elements(&mut self) {
1973+
if mem::needs_drop::<T>() && self.len() != 0 {
1974+
while let Some(item) = self.next() {
1975+
item.drop();
1976+
}
1977+
}
1978+
}
19711979
}
19721980

19731981
impl<T> Clone for RawIter<T> {
@@ -2030,11 +2038,7 @@ unsafe impl<#[may_dangle] T, A: Allocator + Clone> Drop for RawIntoIter<T, A> {
20302038
fn drop(&mut self) {
20312039
unsafe {
20322040
// Drop all remaining elements
2033-
if mem::needs_drop::<T>() && self.iter.len() != 0 {
2034-
while let Some(item) = self.iter.next() {
2035-
item.drop();
2036-
}
2037-
}
2041+
self.iter.drop_elements();
20382042

20392043
// Free the table
20402044
if let Some((ptr, layout)) = self.allocation {
@@ -2049,11 +2053,7 @@ impl<T, A: Allocator + Clone> Drop for RawIntoIter<T, A> {
20492053
fn drop(&mut self) {
20502054
unsafe {
20512055
// Drop all remaining elements
2052-
if mem::needs_drop::<T>() && self.iter.len() != 0 {
2053-
while let Some(item) = self.iter.next() {
2054-
item.drop();
2055-
}
2056-
}
2056+
self.iter.drop_elements();
20572057

20582058
// Free the table
20592059
if let Some((ptr, layout)) = self.allocation {
@@ -2111,11 +2111,7 @@ impl<T, A: Allocator + Clone> Drop for RawDrain<'_, T, A> {
21112111
fn drop(&mut self) {
21122112
unsafe {
21132113
// Drop all remaining elements. Note that this may panic.
2114-
if mem::needs_drop::<T>() && self.iter.len() != 0 {
2115-
while let Some(item) = self.iter.next() {
2116-
item.drop();
2117-
}
2118-
}
2114+
self.iter.drop_elements();
21192115

21202116
// Reset the contents of the table now that all elements have been
21212117
// dropped.

0 commit comments

Comments
 (0)