Skip to content

Commit a9919e0

Browse files
committed
Changed the implementation of IntoIter::last()
Simply does what next() does except it goes straight to the end of the array. Woopsie
1 parent 1135cab commit a9919e0

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

library/alloc/src/vec.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -2956,11 +2956,23 @@ impl<T> Iterator for IntoIter<T> {
29562956
if mem::size_of::<T>() == 0 { mem::zeroed() } else { ptr::read(self.ptr.add(i)) }
29572957
}
29582958
#[inline]
2959-
fn last(self) -> Option<Self::Item>
2960-
where
2961-
Self: Sized,
2962-
{
2963-
if self.ptr == self.end { None } else { unsafe { Some(ptr::read(self.end.offset(-1))) } }
2959+
fn last(mut self) -> Option<T> {
2960+
unsafe {
2961+
if self.ptr as *const _ == self.end {
2962+
None
2963+
} else {
2964+
if mem::size_of::<T>() == 0 {
2965+
// Immediately marches to end of the iterator.
2966+
self.ptr = self.end;
2967+
// Make up a value of this ZST.
2968+
Some(mem::zeroed())
2969+
} else {
2970+
// Immediately marches to end of the iterator.
2971+
self.ptr = self.end;
2972+
Some(ptr::read(self.ptr.offset(-1)))
2973+
}
2974+
}
2975+
}
29642976
}
29652977
}
29662978

0 commit comments

Comments
 (0)