From 78a4d71d0be198b7cdeb6ec4b98ad5c9f9342254 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Sun, 11 Feb 2024 17:18:57 -0500 Subject: [PATCH] Don't use mem::zeroed in vec::IntoIter --- library/alloc/src/vec/into_iter.rs | 61 ++++++++++++------------------ 1 file changed, 25 insertions(+), 36 deletions(-) diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index 7800560da94f9..52f07ff0450d7 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -11,7 +11,7 @@ use core::iter::{ TrustedRandomAccessNoCoerce, }; use core::marker::PhantomData; -use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties}; +use core::mem::{ManuallyDrop, MaybeUninit, SizedTypeProperties}; use core::num::NonZeroUsize; #[cfg(not(no_global_oom_handling))] use core::ops::Deref; @@ -200,27 +200,23 @@ impl Iterator for IntoIter { #[inline] fn next(&mut self) -> Option { - if T::IS_ZST { - if self.ptr.as_ptr() == self.end as *mut _ { - None - } else { - // `ptr` has to stay where it is to remain aligned, so we reduce the length by 1 by - // reducing the `end`. - self.end = self.end.wrapping_byte_sub(1); - - // Make up a value of this ZST. - Some(unsafe { mem::zeroed() }) + let ptr = if T::IS_ZST { + if self.ptr.as_ptr() == self.end as *mut T { + return None; } + // `ptr` has to stay where it is to remain aligned, so we reduce the length by 1 by + // reducing the `end`. + self.end = self.end.wrapping_byte_sub(1); + self.ptr } else { - if self.ptr == non_null!(self.end, T) { - None - } else { - let old = self.ptr; - self.ptr = unsafe { old.add(1) }; - - Some(unsafe { ptr::read(old.as_ptr()) }) + if self.ptr == unsafe { NonNull::new_unchecked(self.end as *mut T) } { + return None; } - } + let old = self.ptr; + self.ptr = unsafe { old.add(1) }; + old + }; + Some(unsafe { ptr.read() }) } #[inline] @@ -305,7 +301,7 @@ impl Iterator for IntoIter { // Also note the implementation of `Self: TrustedRandomAccess` requires // that `T: Copy` so reading elements from the buffer doesn't invalidate // them for `Drop`. - unsafe { if T::IS_ZST { mem::zeroed() } else { self.ptr.add(i).read() } } + unsafe { self.ptr.add(i).read() } } } @@ -313,24 +309,17 @@ impl Iterator for IntoIter { impl DoubleEndedIterator for IntoIter { #[inline] fn next_back(&mut self) -> Option { + if self.ptr.as_ptr() == self.end as *mut _ { + return None; + } if T::IS_ZST { - if self.end as *mut _ == self.ptr.as_ptr() { - None - } else { - // See above for why 'ptr.offset' isn't used - self.end = self.end.wrapping_byte_sub(1); - - // Make up a value of this ZST. - Some(unsafe { mem::zeroed() }) - } + // See above for why 'ptr.offset' isn't used + self.end = self.end.wrapping_byte_sub(1); + Some(unsafe { ptr::read(self.ptr.as_ptr()) }) } else { - if non_null!(self.end, T) == self.ptr { - None - } else { - let new_end = unsafe { non_null!(self.end, T).sub(1) }; - *non_null!(mut self.end, T) = new_end; - - Some(unsafe { ptr::read(new_end.as_ptr()) }) + unsafe { + self.end = self.end.sub(1); + Some(ptr::read(self.end)) } } }