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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ zerocopy-panic-in-const = "1.57.0"
[package.metadata.ci]
# The versions of the stable and nightly compiler toolchains to use in CI.
pinned-stable = "1.78.0"
pinned-nightly = "nightly-2024-06-05"
pinned-nightly = "nightly-2024-06-06"

[package.metadata.docs.rs]
all-features = true
Expand Down
4 changes: 2 additions & 2 deletions src/byteorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ define_type!(
Usize,
usize,
mem::size_of::<usize>() * 8,
mem::size_of::<usize>(),
size_of::<usize>(),
usize::from_be_bytes,
usize::to_be_bytes,
usize::from_le_bytes,
Expand Down Expand Up @@ -746,7 +746,7 @@ define_type!(
Isize,
isize,
mem::size_of::<isize>() * 8,
mem::size_of::<isize>(),
size_of::<isize>(),
isize::from_be_bytes,
isize::to_be_bytes,
isize::from_le_bytes,
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ where
f.write_str("the conversion failed because the address of the source (a multiple of ")?;
addr_align.fmt(f)?;
f.write_str(") is not a multiple of the alignment (")?;
core::mem::align_of::<Dst>().fmt(f)?;
align_of::<Dst>().fmt(f)?;
f.write_str(") of the destination type: ")?;
f.write_str(core::any::type_name::<Dst>())?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ mod tests {
let mut t = Self::new_zeroed();
let ptr: *mut T = &mut t;
// SAFETY: `T: FromBytes`
unsafe { ptr::write_bytes(ptr.cast::<u8>(), 0xFF, mem::size_of::<T>()) };
unsafe { ptr::write_bytes(ptr.cast::<u8>(), 0xFF, size_of::<T>()) };
t
};

Expand Down
12 changes: 6 additions & 6 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
// This file may not be copied, modified, or distributed except according to
// those terms.

use core::{mem, num::NonZeroUsize};
use core::num::NonZeroUsize;

use crate::util;

/// The target pointer width, counted in bits.
const POINTER_WIDTH_BITS: usize = mem::size_of::<usize>() * 8;
const POINTER_WIDTH_BITS: usize = size_of::<usize>() * 8;

/// The layout of a type which might be dynamically-sized.
///
Expand Down Expand Up @@ -160,11 +160,11 @@ impl DstLayout {
// sound to initialize `size_info` to `SizeInfo::Sized { size }`; the
// `size` field is also correct by construction.
DstLayout {
align: match NonZeroUsize::new(mem::align_of::<T>()) {
align: match NonZeroUsize::new(align_of::<T>()) {
Some(align) => align,
None => const_unreachable!(),
},
size_info: SizeInfo::Sized { size: mem::size_of::<T>() },
size_info: SizeInfo::Sized { size: size_of::<T>() },
}
}

Expand All @@ -183,13 +183,13 @@ impl DstLayout {
// construction. Since `[T]` is a (degenerate case of a) slice DST, it
// is correct to initialize `size_info` to `SizeInfo::SliceDst`.
DstLayout {
align: match NonZeroUsize::new(mem::align_of::<T>()) {
align: match NonZeroUsize::new(align_of::<T>()) {
Some(align) => align,
None => const_unreachable!(),
},
size_info: SizeInfo::SliceDst(TrailingSliceLayout {
offset: 0,
elem_size: mem::size_of::<T>(),
elem_size: size_of::<T>(),
}),
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2043,7 +2043,7 @@ pub unsafe trait FromZeros: TryFromBytes {
#[inline(always)]
fn zero(&mut self) {
let slf: *mut Self = self;
let len = mem::size_of_val(self);
let len = size_of_val(self);
// SAFETY:
// - `self` is guaranteed by the type system to be valid for writes of
// size `size_of_val(self)`.
Expand Down Expand Up @@ -2165,10 +2165,10 @@ pub unsafe trait FromZeros: TryFromBytes {
where
Self: Sized,
{
let size = mem::size_of::<Self>()
let size = size_of::<Self>()
.checked_mul(len)
.expect("mem::size_of::<Self>() * len overflows `usize`");
let align = mem::align_of::<Self>();
let align = align_of::<Self>();
// On stable Rust versions <= 1.64.0, `Layout::from_size_align` has a
// bug in which sufficiently-large allocations (those which, when
// rounded up to the alignment, overflow `isize`) are not rejected,
Expand Down Expand Up @@ -3817,7 +3817,7 @@ pub unsafe trait IntoBytes {
{
// Note that this method does not have a `Self: Sized` bound;
// `size_of_val` works for unsized values too.
let len = mem::size_of_val(self);
let len = size_of_val(self);
let slf: *const Self = self;

// SAFETY:
Expand Down Expand Up @@ -3894,7 +3894,7 @@ pub unsafe trait IntoBytes {
{
// Note that this method does not have a `Self: Sized` bound;
// `size_of_val` works for unsized values too.
let len = mem::size_of_val(self);
let len = size_of_val(self);
let slf: *mut Self = self;

// SAFETY:
Expand Down Expand Up @@ -3972,7 +3972,7 @@ pub unsafe trait IntoBytes {
where
Self: Immutable,
{
if bytes.len() != mem::size_of_val(self) {
if bytes.len() != size_of_val(self) {
return Err(SizeError::new(self));
}
bytes.copy_from_slice(self.as_bytes());
Expand Down Expand Up @@ -4032,7 +4032,7 @@ pub unsafe trait IntoBytes {
where
Self: Immutable,
{
let size = mem::size_of_val(self);
let size = size_of_val(self);
match bytes.get_mut(..size) {
Some(bytes) => {
bytes.copy_from_slice(self.as_bytes());
Expand Down Expand Up @@ -4102,7 +4102,7 @@ pub unsafe trait IntoBytes {
where
Self: Immutable,
{
let start = if let Some(start) = bytes.len().checked_sub(mem::size_of_val(self)) {
let start = if let Some(start) = bytes.len().checked_sub(size_of_val(self)) {
start
} else {
return Err(SizeError::new(self));
Expand Down Expand Up @@ -4989,7 +4989,7 @@ mod alloc_support {
#[should_panic(expected = "assertion failed: size <= max_alloc")]
fn test_new_box_slice_zeroed_panics_isize_overflow() {
let max = usize::try_from(isize::MAX).unwrap();
let _ = u16::new_box_slice_zeroed((max / mem::size_of::<u16>()) + 1);
let _ = u16::new_box_slice_zeroed((max / size_of::<u16>()) + 1);
}
}
}
Expand Down Expand Up @@ -5053,7 +5053,7 @@ mod tests {
test!(u8, layout(1, 1, None));
// Use `align_of` because `u64` alignment may be smaller than 8 on some
// platforms.
test!(u64, layout(8, mem::align_of::<u64>(), None));
test!(u64, layout(8, align_of::<u64>(), None));
test!(AU64, layout(8, 8, None));

test!(Option<&'static ()>, usize::LAYOUT);
Expand Down
2 changes: 1 addition & 1 deletion src/macro_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ where
I::Aliasing: AtLeast<invariant::Shared>,
R: AliasingSafeReason,
{
static_assert!(Src, Dst => mem::size_of::<Dst>() >= mem::size_of::<Src>());
static_assert!(Src, Dst => size_of::<Dst>() >= size_of::<Src>());

// SAFETY: This is a pointer cast, satisfying the following properties:
// - `p as *mut Dst` addresses a subset of the `bytes` addressed by `src`,
Expand Down
4 changes: 2 additions & 2 deletions src/pointer/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1573,7 +1573,7 @@ mod tests {
let t = slf.bikeshed_recall_valid().as_ref();

let bytes = {
let len = mem::size_of_val(t);
let len = size_of_val(t);
let t: *const T = t;
// SAFETY:
// - We know `t`'s bytes are all initialized
Expand All @@ -1596,7 +1596,7 @@ mod tests {
// shouldn't be (see the comment above).
assert_eq!(bytes, vec![0u8; bytes.len()]);

mem::size_of_val(t)
size_of_val(t)
}

for meta in metas.clone().into_iter() {
Expand Down
12 changes: 6 additions & 6 deletions src/ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ where
{
#[must_use = "has no side effects"]
pub(crate) fn sized_from(bytes: B) -> Result<Ref<B, T>, CastError<B, T>> {
if bytes.len() != mem::size_of::<T>() {
if bytes.len() != size_of::<T>() {
return Err(SizeError::new(bytes).into());
}
if !util::aligned_to::<_, T>(bytes.deref()) {
Expand All @@ -227,14 +227,14 @@ where
{
#[must_use = "has no side effects"]
pub(crate) fn sized_from_prefix(bytes: B) -> Result<(Ref<B, T>, B), CastError<B, T>> {
if bytes.len() < mem::size_of::<T>() {
if bytes.len() < size_of::<T>() {
return Err(SizeError::new(bytes).into());
}
if !util::aligned_to::<_, T>(bytes.deref()) {
return Err(AlignmentError::new(bytes).into());
}
let (bytes, suffix) =
try_split_at(bytes, mem::size_of::<T>()).map_err(|b| SizeError::new(b).into())?;
try_split_at(bytes, size_of::<T>()).map_err(|b| SizeError::new(b).into())?;
// SAFETY: We just validated alignment and that `bytes` is at least as
// large as `T`. `try_split_at(bytes, mem::size_of::<T>())?` ensures
// that the new `bytes` is exactly the size of `T`. By safety
Expand All @@ -247,7 +247,7 @@ where
#[must_use = "has no side effects"]
pub(crate) fn sized_from_suffix(bytes: B) -> Result<(B, Ref<B, T>), CastError<B, T>> {
let bytes_len = bytes.len();
let split_at = if let Some(split_at) = bytes_len.checked_sub(mem::size_of::<T>()) {
let split_at = if let Some(split_at) = bytes_len.checked_sub(size_of::<T>()) {
split_at
} else {
return Err(SizeError::new(bytes).into());
Expand Down Expand Up @@ -1324,7 +1324,7 @@ mod tests {
// will fail thanks to the buffer being too short; these are different
// error paths, and while the error types are the same, the distinction
// shows up in code coverage metrics.
let n = (usize::MAX / mem::size_of::<AU64>()) + 1;
let n = (usize::MAX / size_of::<AU64>()) + 1;
assert!(Ref::<_, [AU64]>::from_bytes_with_elems(&buf.t[..], n).is_err());
assert!(Ref::<_, [AU64]>::from_bytes_with_elems(&buf.t[..], 2).is_err());
assert!(Ref::<_, [AU64]>::from_prefix_with_elems(&buf.t[..], n).is_err());
Expand Down Expand Up @@ -1358,7 +1358,7 @@ mod tests {
// Fail due to arithmetic overflow.

let buf = Align::<[u8; 16], AU64>::default();
let unreasonable_len = usize::MAX / mem::size_of::<AU64>() + 1;
let unreasonable_len = usize::MAX / size_of::<AU64>() + 1;
assert!(Ref::<_, [AU64]>::from_prefix_with_elems(&buf.t[..], unreasonable_len).is_err());
assert!(Ref::<_, [AU64]>::from_suffix_with_elems(&buf.t[..], unreasonable_len).is_err());
assert!(Ref::<_, [[u8; 8]]>::unaligned_from_prefix_with_elems(
Expand Down
4 changes: 2 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use core::{
cell::UnsafeCell,
mem::{self, ManuallyDrop, MaybeUninit},
mem::{ManuallyDrop, MaybeUninit},
num::{NonZeroUsize, Wrapping},
ptr::NonNull,
sync::atomic::{
Expand Down Expand Up @@ -484,7 +484,7 @@ pub(crate) fn aligned_to<T: AsAddress, U>(t: T) -> bool {
// `mem::align_of::<U>()` is guaranteed to return a non-zero value, which in
// turn guarantees that this mod operation will not panic.
#[allow(clippy::arithmetic_side_effects)]
let remainder = t.addr() % mem::align_of::<U>();
let remainder = t.addr() % align_of::<U>();
remainder == 0
}

Expand Down