diff options
| author | bors <bors@rust-lang.org> | 2024-02-17 18:20:41 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-02-17 18:20:41 +0000 |
| commit | cabdf3ad257693aa79ffcc4b7dd1fdab41dc209e (patch) | |
| tree | 0b9165c5f70c60fd421dcf652a9998e5e132034f /library/alloc/src | |
| parent | 12b5498f3bcaa9f7b7641ed829873da0907b21dc (diff) | |
| parent | eafa74ab629df7d02dc73fde7db4027f90b2538a (diff) | |
| download | rust-cabdf3ad257693aa79ffcc4b7dd1fdab41dc209e.tar.gz rust-cabdf3ad257693aa79ffcc4b7dd1fdab41dc209e.zip | |
Auto merge of #121240 - matthiaskrgr:rollup-lfb5i9w, r=matthiaskrgr
Rollup of 9 pull requests Successful merges: - #120952 (Don't use mem::zeroed in vec::IntoIter) - #121085 (errors: only eagerly translate subdiagnostics) - #121091 (use build.rustc config and skip-stage0-validation flag) - #121149 (Fix typo in VecDeque::handle_capacity_increase() doc comment.) - #121193 (Use fulfillment in next trait solver coherence) - #121209 (Make `CodegenBackend::join_codegen` infallible.) - #121210 (Fix `cfg(target_abi = "sim")` on `i386-apple-ios`) - #121228 (create stamp file for clippy) - #121231 (remove a couple of redundant clones) r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'library/alloc/src')
| -rw-r--r-- | library/alloc/src/collections/vec_deque/mod.rs | 8 | ||||
| -rw-r--r-- | library/alloc/src/vec/into_iter.rs | 63 |
2 files changed, 33 insertions, 38 deletions
diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index d062587b8f5..bcba6525fda 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -485,10 +485,10 @@ impl<T, A: Allocator> VecDeque<T, A> { // H := head // L := last element (`self.to_physical_idx(self.len - 1)`) // - // H L - // [o o o o o o o . ] - // H L - // A [o o o o o o o . . . . . . . . . ] + // H L + // [o o o o o o o o ] + // H L + // A [o o o o o o o o . . . . . . . . ] // L H // [o o o o o o o o ] // H L diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index 63d8fe19ac3..dfd42ca0619 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::NonZero; #[cfg(not(no_global_oom_handling))] use core::ops::Deref; @@ -200,27 +200,23 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> { #[inline] fn next(&mut self) -> Option<T> { - 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()) }) + return None; } - } + let old = self.ptr; + self.ptr = unsafe { old.add(1) }; + old + }; + Some(unsafe { ptr.read() }) } #[inline] @@ -305,7 +301,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> { // 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() } } } @@ -314,23 +310,22 @@ impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> { #[inline] fn next_back(&mut self) -> Option<T> { 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() }) + if self.ptr.as_ptr() == self.end as *mut _ { + return None; } + // See above for why 'ptr.offset' isn't used + self.end = self.end.wrapping_byte_sub(1); + // Note that even though this is next_back() we're reading from `self.ptr`, not + // `self.end`. We track our length using the byte offset from `self.ptr` to `self.end`, + // so the end pointer may not be suitably aligned for T. + 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()) }) + if self.ptr == non_null!(self.end, T) { + return None; + } + unsafe { + self.end = self.end.sub(1); + Some(ptr::read(self.end)) } } } |
