Skip to content

Commit 56613f8

Browse files
committed
More IS_ZST in library
I noticed that `post_inc_start` and `pre_dec_end` were doing this check in different ways https://github.com/rust-lang/rust/blob/d19b64fb54391b64ce99981577c67c93ac2a9ffa/library/core/src/slice/iter/macros.rs#L76-L93 so started making this PR, then added a few more I found since I was already making changes anyway.
1 parent d19b64f commit 56613f8

File tree

4 files changed

+7
-15
lines changed

4 files changed

+7
-15
lines changed

library/alloc/src/boxed/thin.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::fmt::{self, Debug, Display, Formatter};
77
use core::marker::PhantomData;
88
#[cfg(not(no_global_oom_handling))]
99
use core::marker::Unsize;
10-
use core::mem;
10+
use core::mem::{self, SizedTypeProperties};
1111
use core::ops::{Deref, DerefMut};
1212
use core::ptr::Pointee;
1313
use core::ptr::{self, NonNull};
@@ -202,9 +202,7 @@ impl<H> WithHeader<H> {
202202
let ptr = if layout.size() == 0 {
203203
// Some paranoia checking, mostly so that the ThinBox tests are
204204
// more able to catch issues.
205-
debug_assert!(
206-
value_offset == 0 && mem::size_of::<T>() == 0 && mem::size_of::<H>() == 0
207-
);
205+
debug_assert!(value_offset == 0 && T::IS_ZST && H::IS_ZST);
208206
layout.dangling()
209207
} else {
210208
let ptr = alloc::alloc(layout);
@@ -249,9 +247,7 @@ impl<H> WithHeader<H> {
249247
alloc::dealloc(self.ptr.as_ptr().sub(value_offset), layout);
250248
} else {
251249
debug_assert!(
252-
value_offset == 0
253-
&& mem::size_of::<H>() == 0
254-
&& self.value_layout.size() == 0
250+
value_offset == 0 && H::IS_ZST && self.value_layout.size() == 0
255251
);
256252
}
257253
}

library/alloc/src/vec/drain.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ impl<'a, T, A: Allocator> Drain<'a, T, A> {
112112
let unyielded_ptr = this.iter.as_slice().as_ptr();
113113

114114
// ZSTs have no identity, so we don't need to move them around.
115-
let needs_move = mem::size_of::<T>() != 0;
116-
117-
if needs_move {
115+
if !T::IS_ZST {
118116
let start_ptr = source_vec.as_mut_ptr().add(start);
119117

120118
// memmove back unyielded elements

library/alloc/src/vec/drain_filter.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::alloc::{Allocator, Global};
2-
use core::mem::{self, ManuallyDrop};
2+
use core::mem::{ManuallyDrop, SizedTypeProperties};
33
use core::ptr;
44
use core::slice;
55

@@ -96,9 +96,7 @@ where
9696

9797
unsafe {
9898
// ZSTs have no identity, so we don't need to move them around.
99-
let needs_move = mem::size_of::<T>() != 0;
100-
101-
if needs_move && this.idx < this.old_len && this.del > 0 {
99+
if !T::IS_ZST && this.idx < this.old_len && this.del > 0 {
102100
let ptr = this.vec.as_mut_ptr();
103101
let src = ptr.add(this.idx);
104102
let dst = src.sub(this.del);

library/core/src/slice/iter/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ macro_rules! iterator {
7373
// Unsafe because the offset must not exceed `self.len()`.
7474
#[inline(always)]
7575
unsafe fn post_inc_start(&mut self, offset: usize) -> * $raw_mut T {
76-
if mem::size_of::<T>() == 0 {
76+
if T::IS_ZST {
7777
zst_shrink!(self, offset);
7878
self.ptr.as_ptr()
7979
} else {

0 commit comments

Comments
 (0)