Skip to content

Commit

Permalink
Simplify the implementation of IntoIter::last() a bit.
Browse files Browse the repository at this point in the history
Suggested by @pickfire.

Signed-off-by: Hanif Bin Ariffin <[email protected]>
  • Loading branch information
hbina committed Oct 23, 2020
1 parent a9919e0 commit 91984e7
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions library/alloc/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2955,24 +2955,11 @@ impl<T> Iterator for IntoIter<T> {
unsafe {
if mem::size_of::<T>() == 0 { mem::zeroed() } else { ptr::read(self.ptr.add(i)) }
}
}

#[inline]
fn last(mut self) -> Option<T> {
unsafe {
if self.ptr as *const _ == self.end {
None
} else {
if mem::size_of::<T>() == 0 {
// Immediately marches to end of the iterator.
self.ptr = self.end;
// Make up a value of this ZST.
Some(mem::zeroed())
} else {
// Immediately marches to end of the iterator.
self.ptr = self.end;
Some(ptr::read(self.ptr.offset(-1)))
}
}
}
self.next_back()
}
}

Expand Down

0 comments on commit 91984e7

Please sign in to comment.