Skip to content

Commit

Permalink
Auto merge of #73951 - pickfire:liballoc-intoiter, r=Mark-Simulacrum
Browse files Browse the repository at this point in the history
Liballoc intoiter refactor
  • Loading branch information
bors committed Sep 11, 2020
2 parents 9911160 + 5031523 commit 12c10e3
Showing 1 changed file with 25 additions and 33 deletions.
58 changes: 25 additions & 33 deletions library/alloc/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2882,25 +2882,21 @@ impl<T> Iterator for IntoIter<T> {

#[inline]
fn next(&mut self) -> Option<T> {
unsafe {
if self.ptr as *const _ == self.end {
None
} else {
if mem::size_of::<T>() == 0 {
// purposefully don't use 'ptr.offset' because for
// vectors with 0-size elements this would return the
// same pointer.
self.ptr = arith_offset(self.ptr as *const i8, 1) as *mut T;

// Make up a value of this ZST.
Some(mem::zeroed())
} else {
let old = self.ptr;
self.ptr = self.ptr.offset(1);

Some(ptr::read(old))
}
}
if self.ptr as *const _ == self.end {
None
} else if mem::size_of::<T>() == 0 {
// purposefully don't use 'ptr.offset' because for
// vectors with 0-size elements this would return the
// same pointer.
self.ptr = unsafe { arith_offset(self.ptr as *const i8, 1) as *mut T };

// Make up a value of this ZST.
Some(unsafe { mem::zeroed() })
} else {
let old = self.ptr;
self.ptr = unsafe { self.ptr.offset(1) };

Some(unsafe { ptr::read(old) })
}
}

Expand Down Expand Up @@ -2935,22 +2931,18 @@ impl<T> Iterator for IntoIter<T> {
impl<T> DoubleEndedIterator for IntoIter<T> {
#[inline]
fn next_back(&mut self) -> Option<T> {
unsafe {
if self.end == self.ptr {
None
} else {
if mem::size_of::<T>() == 0 {
// See above for why 'ptr.offset' isn't used
self.end = arith_offset(self.end as *const i8, -1) as *mut T;
if self.end == self.ptr {
None
} else if mem::size_of::<T>() == 0 {
// See above for why 'ptr.offset' isn't used
self.end = unsafe { arith_offset(self.end as *const i8, -1) as *mut T };

// Make up a value of this ZST.
Some(mem::zeroed())
} else {
self.end = self.end.offset(-1);
// Make up a value of this ZST.
Some(unsafe { mem::zeroed() })
} else {
self.end = unsafe { self.end.offset(-1) };

Some(ptr::read(self.end))
}
}
Some(unsafe { ptr::read(self.end) })
}
}
}
Expand Down

0 comments on commit 12c10e3

Please sign in to comment.