Skip to content

Commit 1135cab

Browse files
committed
Overload last for Vec.
Simply use the fact that `IntoIter` stores the end pointer and we can perform pointer arithmetic.
1 parent a9cd294 commit 1135cab

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

library/alloc/src/vec.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2955,6 +2955,12 @@ impl<T> Iterator for IntoIter<T> {
29552955
unsafe {
29562956
if mem::size_of::<T>() == 0 { mem::zeroed() } else { ptr::read(self.ptr.add(i)) }
29572957
}
2958+
#[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))) } }
29582964
}
29592965
}
29602966

0 commit comments

Comments
 (0)