Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions library/core/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@
#![stable(feature = "rust1", since = "1.0.0")]

use crate::intrinsics::{self, type_id, type_id_vtable};
use crate::mem::transmute;
use crate::mem::type_info::{TraitImpl, TypeKind};
use crate::mem::{SizedTypeProperties, transmute};
use crate::{fmt, hash, ptr};

///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -727,7 +727,7 @@ pub struct TypeId {
/// the TypeId actually is, allowing CTFE and miri to operate based off it.
/// At runtime all the pointers in the array contain bits of the hash, making
/// the entire `TypeId` actually just be a `u128` hash of the type.
pub(crate) data: [*const (); 16 / size_of::<*const ()>()],
pub(crate) data: [*const (); 16 / <*const ()>::SIZE],
}

// SAFETY: the raw pointer is always an integer
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
use crate::cmp::Ordering;
use crate::fmt::{self, Debug, Display};
use crate::marker::{Destruct, PhantomData, Unsize};
use crate::mem::{self, ManuallyDrop};
use crate::mem::{self, ManuallyDrop, SizedTypeProperties};
use crate::ops::{self, CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn};
use crate::panic::const_panic;
use crate::pin::PinCoerceUnsized;
Expand Down Expand Up @@ -469,7 +469,7 @@ impl<T> Cell<T> {
let src_usize = src.addr();
let dst_usize = dst.addr();
let diff = src_usize.abs_diff(dst_usize);
diff >= size_of::<T>()
diff >= T::SIZE
}

if ptr::eq(self, other) {
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/fmt/num.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Integer and floating-point number formatting

use crate::fmt::NumBuffer;
use crate::mem::MaybeUninit;
use crate::mem::{MaybeUninit, SizedTypeProperties};
use crate::num::imp::fmt as numfmt;
use crate::{fmt, str};

Expand Down Expand Up @@ -195,7 +195,7 @@ macro_rules! impl_Display {

// Format per four digits from the lookup table.
// Four digits need a 16-bit $Unsigned or wider.
while size_of::<Self>() > 1 && remain > 999.try_into().expect("branch is not hit for types that cannot fit 999 (u8)") {
while Self::SIZE > 1 && remain > 999.try_into().expect("branch is not hit for types that cannot fit 999 (u8)") {
// SAFETY: All of the decimals fit in buf due to MAX_DEC_N
// and the while condition ensures at least 4 more decimals.
unsafe { core::hint::assert_unchecked(offset >= 4) }
Expand Down
5 changes: 3 additions & 2 deletions library/core/src/hash/sip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![allow(deprecated)] // the types in this module are deprecated

use crate::marker::PhantomData;
use crate::mem::SizedTypeProperties;
use crate::{cmp, ptr};

/// An implementation of SipHash 1-3.
Expand Down Expand Up @@ -101,12 +102,12 @@ macro_rules! compress {
/// `$i..$i+size_of::<$int_ty>()`, so that must be in-bounds.
macro_rules! load_int_le {
($buf:expr, $i:expr, $int_ty:ident) => {{
debug_assert!($i + size_of::<$int_ty>() <= $buf.len());
debug_assert!($i + <$int_ty>::SIZE <= $buf.len());
let mut data = 0 as $int_ty;
ptr::copy_nonoverlapping(
$buf.as_ptr().add($i),
&mut data as *mut _ as *mut u8,
size_of::<$int_ty>(),
<$int_ty>::SIZE,
);
data.to_le()
}};
Expand Down
7 changes: 4 additions & 3 deletions library/core/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@

use crate::ffi::{VaArgSafe, VaList};
use crate::marker::{ConstParamTy, DiscriminantKind, PointeeSized, Tuple};
use crate::mem::SizedTypeProperties;
use crate::num::imp::libm;
use crate::{mem, ptr};
use crate::ptr;

mod bounds;
pub mod fallback;
Expand Down Expand Up @@ -2193,7 +2194,7 @@ pub const fn rotate_left<T: [const] fallback::FunnelShift>(x: T, shift: u32) ->
// Make sure to call the intrinsic for `funnel_shl`, not the fallback impl.
// SAFETY: we modulo `shift` so that the result is definitely less than the size of
// `T` in bits.
unsafe { unchecked_funnel_shl(x, x, shift % (mem::size_of::<T>() as u32 * 8)) }
unsafe { unchecked_funnel_shl(x, x, shift % (T::SIZE as u32 * 8)) }
}

/// Performs rotate right.
Expand All @@ -2215,7 +2216,7 @@ pub const fn rotate_right<T: [const] fallback::FunnelShift>(x: T, shift: u32) ->
// Make sure to call the intrinsic for `funnel_shr`, not the fallback impl.
// SAFETY: we modulo `shift` so that the result is definitely less than the size of
// `T` in bits.
unsafe { unchecked_funnel_shr(x, x, shift % (mem::size_of::<T>() as u32 * 8)) }
unsafe { unchecked_funnel_shr(x, x, shift % (T::SIZE as u32 * 8)) }
}

/// Wrapping (modular) addition. Computes `a + b`,
Expand Down
21 changes: 11 additions & 10 deletions library/core/src/io/error/repr_bitpacked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
//! the time.

use core::marker::PhantomData;
use core::mem::SizedTypeProperties;
use core::num::NonZeroUsize;
use core::ptr::NonNull;

Expand Down Expand Up @@ -138,7 +139,7 @@ impl Repr {
// Should only be possible if an allocator handed out a pointer with
// wrong alignment.
debug_assert_eq!(p.addr() & TAG_MASK, 0);
// Note: We know `TAG_CUSTOM <= size_of::<Custom>()` (static_assert at
// Note: We know `TAG_CUSTOM <= Custom::SIZE` (static_assert at
// end of file), and both the start and end of the expression must be
// valid without address space wraparound due to `Box`'s semantics.
//
Expand Down Expand Up @@ -302,14 +303,14 @@ macro_rules! static_assert {
}

// The bitpacking we use requires pointers be exactly 64 bits.
static_assert!(@usize_eq: size_of::<NonNull<()>>(), 8);
static_assert!(@usize_eq: NonNull::<()>::SIZE, 8);

// We also require pointers and usize be the same size.
static_assert!(@usize_eq: size_of::<NonNull<()>>(), size_of::<usize>());
static_assert!(@usize_eq: NonNull::<()>::SIZE, usize::SIZE);

// `Custom` and `SimpleMessage` need to be thin pointers.
static_assert!(@usize_eq: size_of::<&'static SimpleMessage>(), 8);
static_assert!(@usize_eq: size_of::<CustomOwner>(), 8);
static_assert!(@usize_eq: <&'static SimpleMessage>::SIZE, 8);
static_assert!(@usize_eq: CustomOwner::SIZE, 8);

static_assert!((TAG_MASK + 1).is_power_of_two());
// And they must have sufficient alignment.
Expand All @@ -330,7 +331,7 @@ static_assert!(@usize_eq: TAG_MASK & TAG_SIMPLE, TAG_SIMPLE);
// check isn't needed for that one, although the assertion that we don't
// actually wrap around in that wrapping_add does simplify the safety reasoning
// elsewhere considerably.
static_assert!(size_of::<Custom>() >= TAG_CUSTOM);
static_assert!(Custom::SIZE >= TAG_CUSTOM);

// These two store a payload which is allowed to be zero, so they must be
// non-zero to preserve the `NonNull`'s range invariant.
Expand All @@ -345,7 +346,7 @@ static_assert!(@usize_eq: TAG_SIMPLE_MESSAGE, 0);
// as it's not `#[repr(transparent)]`/`#[repr(C)]`. We could add that, but
// the `#[repr()]` would show up in rustdoc, which might be seen as a stable
// commitment.
static_assert!(@usize_eq: size_of::<Repr>(), 8);
static_assert!(@usize_eq: size_of::<Option<Repr>>(), 8);
static_assert!(@usize_eq: size_of::<Result<(), Repr>>(), 8);
static_assert!(@usize_eq: size_of::<Result<usize, Repr>>(), 16);
static_assert!(@usize_eq: Repr::SIZE, 8);
static_assert!(@usize_eq: Option::<Repr>::SIZE, 8);
static_assert!(@usize_eq: Result::<(), Repr>::SIZE, 8);
static_assert!(@usize_eq: Result::<usize, Repr>::SIZE, 16);
3 changes: 2 additions & 1 deletion library/core/src/mem/alignment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(clippy::enum_clike_unportable_variant)]

use crate::marker::MetaSized;
use crate::mem::SizedTypeProperties;
use crate::num::NonZero;
use crate::ub_checks::assert_unsafe_precondition;
use crate::{cmp, fmt, hash, mem, num};
Expand All @@ -22,7 +23,7 @@ pub struct Alignment {
}

// Alignment is `repr(usize)`, but via extra steps.
const _: () = assert!(size_of::<Alignment>() == size_of::<usize>());
const _: () = assert!(Alignment::SIZE == usize::SIZE);
const _: () = assert!(align_of::<Alignment>() == align_of::<usize>());

fn _alignment_can_be_structurally_matched(a: Alignment) -> bool {
Expand Down
13 changes: 3 additions & 10 deletions library/core/src/mem/maybe_uninit.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::any::type_name;
use crate::clone::TrivialClone;
use crate::marker::Destruct;
use crate::mem::{ManuallyDrop, transmute_neo};
use crate::mem::{ManuallyDrop, SizedTypeProperties, transmute_neo};
use crate::{fmt, intrinsics, ptr, slice};

/// A wrapper type to construct uninitialized instances of `T`.
Expand Down Expand Up @@ -1077,9 +1077,7 @@ impl<T> MaybeUninit<T> {
#[unstable(feature = "maybe_uninit_as_bytes", issue = "93092")]
pub const fn as_bytes(&self) -> &[MaybeUninit<u8>] {
// SAFETY: MaybeUninit<u8> is always valid, even for padding bytes
unsafe {
slice::from_raw_parts(self.as_ptr().cast::<MaybeUninit<u8>>(), super::size_of::<T>())
}
unsafe { slice::from_raw_parts(self.as_ptr().cast::<MaybeUninit<u8>>(), T::SIZE) }
}

/// Returns the contents of this `MaybeUninit` as a mutable slice of potentially uninitialized
Expand Down Expand Up @@ -1108,12 +1106,7 @@ impl<T> MaybeUninit<T> {
#[unstable(feature = "maybe_uninit_as_bytes", issue = "93092")]
pub const fn as_bytes_mut(&mut self) -> &mut [MaybeUninit<u8>] {
// SAFETY: MaybeUninit<u8> is always valid, even for padding bytes
unsafe {
slice::from_raw_parts_mut(
self.as_mut_ptr().cast::<MaybeUninit<u8>>(),
super::size_of::<T>(),
)
}
unsafe { slice::from_raw_parts_mut(self.as_mut_ptr().cast::<MaybeUninit<u8>>(), T::SIZE) }
}
}

Expand Down
3 changes: 2 additions & 1 deletion library/core/src/num/imp/dec2flt/fpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub(super) use fpu_precision::set_precision;
#[cfg(all(target_arch = "x86", not(target_feature = "sse2")))]
mod fpu_precision {
use core::arch::asm;
use core::mem::SizedTypeProperties;

/// A structure used to preserve the original value of the FPU control word, so that it can be
/// restored when the structure is dropped.
Expand Down Expand Up @@ -60,7 +61,7 @@ mod fpu_precision {
let mut cw = 0_u16;

// Compute the value for the Precision Control field that is appropriate for `T`.
let cw_precision = match size_of::<T>() {
let cw_precision = match T::SIZE {
4 => 0x0000, // 32 bits
8 => 0x0200, // 64 bits
_ => 0x0300, // default, 80 bits
Expand Down
12 changes: 6 additions & 6 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3751,7 +3751,7 @@ macro_rules! int_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn to_be_bytes(self) -> [u8; size_of::<Self>()] {
pub const fn to_be_bytes(self) -> [u8; Self::SIZE] {
self.to_be().to_ne_bytes()
}

Expand All @@ -3771,7 +3771,7 @@ macro_rules! int_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn to_le_bytes(self) -> [u8; size_of::<Self>()] {
pub const fn to_le_bytes(self) -> [u8; Self::SIZE] {
self.to_le().to_ne_bytes()
}

Expand Down Expand Up @@ -3808,7 +3808,7 @@ macro_rules! int_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn to_ne_bytes(self) -> [u8; size_of::<Self>()] {
pub const fn to_ne_bytes(self) -> [u8; Self::SIZE] {
// SAFETY: integers are plain old datatypes so we can always transmute them to
// arrays of bytes
unsafe { mem::transmute(self) }
Expand Down Expand Up @@ -3839,7 +3839,7 @@ macro_rules! int_impl {
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
#[must_use]
#[inline]
pub const fn from_be_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
pub const fn from_be_bytes(bytes: [u8; Self::SIZE]) -> Self {
Self::from_be(Self::from_ne_bytes(bytes))
}

Expand Down Expand Up @@ -3868,7 +3868,7 @@ macro_rules! int_impl {
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
#[must_use]
#[inline]
pub const fn from_le_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
pub const fn from_le_bytes(bytes: [u8; Self::SIZE]) -> Self {
Self::from_le(Self::from_ne_bytes(bytes))
}

Expand Down Expand Up @@ -3911,7 +3911,7 @@ macro_rules! int_impl {
// SAFETY: const sound because integers are plain old datatypes so we can always
// transmute to them
#[inline]
pub const fn from_ne_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
pub const fn from_ne_bytes(bytes: [u8; Self::SIZE]) -> Self {
// SAFETY: integers are plain old datatypes so we can always transmute to them
unsafe { mem::transmute(bytes) }
}
Expand Down
9 changes: 5 additions & 4 deletions library/core/src/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
#![stable(feature = "rust1", since = "1.0.0")]

use crate::convert::{BoundedCastFromInt, CheckedCastFromInt};
use crate::mem::{self, SizedTypeProperties};
use crate::panic::const_panic;
use crate::str::FromStr;
use crate::ub_checks::assert_unsafe_precondition;
use crate::{ascii, intrinsics, mem};
use crate::{ascii, intrinsics};

// FIXME(const-hack): Used because the `?` operator is not allowed in a const context.
macro_rules! try_opt {
Expand Down Expand Up @@ -1479,15 +1480,15 @@ impl usize {
/// Returns an `usize` where every byte is equal to `x`.
#[inline]
pub(crate) const fn repeat_u8(x: u8) -> usize {
usize::from_ne_bytes([x; size_of::<usize>()])
usize::from_ne_bytes([x; usize::SIZE])
}

/// Returns an `usize` where every byte pair is equal to `x`.
#[inline]
pub(crate) const fn repeat_u16(x: u16) -> usize {
let mut r = 0usize;
let mut i = 0;
while i < size_of::<usize>() {
while i < usize::SIZE {
// Use `wrapping_shl` to make it work on targets with 16-bit `usize`
r = r.wrapping_shl(16) | (x as usize);
i += 2;
Expand Down Expand Up @@ -1568,7 +1569,7 @@ pub enum FpCategory {
#[inline(always)]
#[unstable(issue = "none", feature = "std_internals")]
pub const fn can_not_overflow<T>(radix: u32, is_signed_ty: bool, digits: &[u8]) -> bool {
radix <= 16 && digits.len() <= size_of::<T>() * 2 - is_signed_ty as usize
radix <= 16 && digits.len() <= T::SIZE * 2 - is_signed_ty as usize
}

#[cfg_attr(not(panic = "immediate-abort"), inline(never))]
Expand Down
5 changes: 3 additions & 2 deletions library/core/src/num/traits.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// Definitions of traits for numeric types
// Implementation based on `num_conv` by jhpratt, under (MIT OR Apache-2.0).
use crate::mem::SizedTypeProperties;

/// Trait for types that this type can be truncated to
#[unstable(feature = "num_internals", reason = "internal implementation detail", issue = "none")]
Expand All @@ -26,7 +27,7 @@ pub impl(self) const trait WidenTarget<Target> {
macro_rules! impl_truncate {
($($from:ty => $($to:ty),+;)*) => {$($(
const _: () = assert!(
size_of::<$from>() >= size_of::<$to>(),
<$from>::SIZE >= <$to>::SIZE,
concat!(
"cannot truncate ",
stringify!($from),
Expand Down Expand Up @@ -73,7 +74,7 @@ macro_rules! impl_truncate {
macro_rules! impl_widen {
($($from:ty => $($to:ty),+;)*) => {$($(
const _: () = assert!(
size_of::<$from>() <= size_of::<$to>(),
<$from>::SIZE <= <$to>::SIZE,
concat!(
"cannot widen ",
stringify!($from),
Expand Down
Loading
Loading