Skip to content

Commit

Permalink
Reorganize macro modules under util module (#1623)
Browse files Browse the repository at this point in the history
This prepares for a follow-up change in which we will move macros from
the crate root to a new `macros` module.
  • Loading branch information
joshlf committed Sep 9, 2024
1 parent 4d8c51a commit de883a7
Show file tree
Hide file tree
Showing 55 changed files with 207 additions and 189 deletions.
27 changes: 12 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,9 @@
#[cfg(any(feature = "derive", test))]
extern crate self as zerocopy;

#[doc(hidden)]
#[macro_use]
mod macros;
#[macro_use]
mod util;
pub mod util;

pub mod byte_slice;
pub mod byteorder;
Expand All @@ -313,8 +312,6 @@ mod impls;
#[doc(hidden)]
pub mod layout;
#[doc(hidden)]
pub mod macro_util;
#[doc(hidden)]
pub mod pointer;
mod r#ref;
// TODO(#252): If we make this pub, come up with a better name.
Expand Down Expand Up @@ -4443,9 +4440,9 @@ macro_rules! transmute {
// - We can't annotate the types; this macro is designed to
// infer the types from the calling context.
#[allow(clippy::useless_transmute, clippy::missing_transmute_annotations)]
$crate::macro_util::core_reexport::mem::transmute(e)
$crate::util::macro_util::core_reexport::mem::transmute(e)
};
$crate::macro_util::must_use(u)
$crate::util::macro_util::must_use(u)
}
}}
}
Expand Down Expand Up @@ -4588,8 +4585,8 @@ macro_rules! transmute_ref {
// the use of `assert_size_eq!` above.
// - We know that `align_of::<Src>() >= align_of::<Dst>()` thanks to
// the use of `assert_align_gt_eq!` above.
let u = unsafe { $crate::macro_util::transmute_ref(e) };
$crate::macro_util::must_use(u)
let u = unsafe { $crate::util::macro_util::transmute_ref(e) };
$crate::util::macro_util::must_use(u)
}
}}
}
Expand Down Expand Up @@ -4740,8 +4737,8 @@ macro_rules! transmute_mut {
// the use of `assert_size_eq!` above.
// - We know that `align_of::<Src>() >= align_of::<Dst>()` thanks to
// the use of `assert_align_gt_eq!` above.
let u = unsafe { $crate::macro_util::transmute_mut(e) };
$crate::macro_util::must_use(u)
let u = unsafe { $crate::util::macro_util::transmute_mut(e) };
$crate::util::macro_util::must_use(u)
}
}}
}
Expand Down Expand Up @@ -4807,10 +4804,10 @@ macro_rules! try_transmute {
Ok(unsafe {
// Clippy: It's okay to transmute a type to itself.
#[allow(clippy::useless_transmute, clippy::missing_transmute_annotations)]
$crate::macro_util::core_reexport::mem::transmute(e)
$crate::util::macro_util::core_reexport::mem::transmute(e)
})
} else {
$crate::macro_util::try_transmute::<_, _>(e)
$crate::util::macro_util::try_transmute::<_, _>(e)
}
}}
}
Expand Down Expand Up @@ -4916,7 +4913,7 @@ macro_rules! try_transmute_ref {

Ok(&u)
} else {
$crate::macro_util::try_transmute_ref::<_, _>(e)
$crate::util::macro_util::try_transmute_ref::<_, _>(e)
}
}}
}
Expand Down Expand Up @@ -5025,7 +5022,7 @@ macro_rules! try_transmute_mut {

Ok(&mut u)
} else {
$crate::macro_util::try_transmute_mut::<_, _>(e)
$crate::util::macro_util::try_transmute_mut::<_, _>(e)
}
}}
}
Expand Down
24 changes: 13 additions & 11 deletions src/macro_util.rs → src/util/macro_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ macro_rules! trailing_field_offset {
($ty:ty, $trailing_field_name:tt) => {{
let min_size = {
let zero_elems: *const [()] =
$crate::macro_util::core_reexport::ptr::slice_from_raw_parts(
$crate::util::macro_util::core_reexport::ptr::slice_from_raw_parts(
// Work around https://github.com/rust-lang/rust-clippy/issues/12280
#[allow(clippy::incompatible_msrv)]
$crate::macro_util::core_reexport::ptr::NonNull::<()>::dangling()
$crate::util::macro_util::core_reexport::ptr::NonNull::<()>::dangling()
.as_ptr()
.cast_const(),
0,
Expand All @@ -153,7 +153,9 @@ macro_rules! trailing_field_offset {
// [1] https://doc.rust-lang.org/nightly/std/mem/fn.size_of_val_raw.html
unsafe {
#[allow(clippy::as_conversions)]
$crate::macro_util::core_reexport::mem::size_of_val_raw(zero_elems as *const $ty)
$crate::util::macro_util::core_reexport::mem::size_of_val_raw(
zero_elems as *const $ty,
)
}
};

Expand Down Expand Up @@ -186,7 +188,7 @@ macro_rules! trailing_field_offset {
//
// [2] https://github.com/rust-lang/reference/pull/1387
let field = unsafe {
$crate::macro_util::core_reexport::ptr::addr_of!((*ptr).$trailing_field_name)
$crate::util::macro_util::core_reexport::ptr::addr_of!((*ptr).$trailing_field_name)
};
// SAFETY:
// - Both `ptr` and `field` are derived from the same allocated object.
Expand Down Expand Up @@ -262,7 +264,7 @@ macro_rules! align_of {
/// `$ts` is the list of the type of every field in `$t`. `$t` must be a
/// struct type, or else `struct_has_padding!`'s result may be meaningless.
///
/// Note that `struct_has_padding!`'s results are independent of `repr` since
/// Note that `struct_has_padding!`'s results are independent of `repcr` since
/// they only consider the size of the type and the sizes of the fields.
/// Whatever the repr, the size of the type already takes into account any
/// padding that the compiler has decided to add. Structs with well-defined
Expand All @@ -273,7 +275,7 @@ macro_rules! align_of {
#[macro_export]
macro_rules! struct_has_padding {
($t:ty, $($ts:ty),*) => {
::zerocopy::macro_util::core_reexport::mem::size_of::<$t>() > 0 $(+ ::zerocopy::macro_util::core_reexport::mem::size_of::<$ts>())*
::zerocopy::util::macro_util::core_reexport::mem::size_of::<$t>() > 0 $(+ ::zerocopy::util::macro_util::core_reexport::mem::size_of::<$ts>())*
};
}

Expand All @@ -293,7 +295,7 @@ macro_rules! struct_has_padding {
#[macro_export]
macro_rules! union_has_padding {
($t:ty, $($ts:ty),*) => {
false $(|| ::zerocopy::macro_util::core_reexport::mem::size_of::<$t>() != ::zerocopy::macro_util::core_reexport::mem::size_of::<$ts>())*
false $(|| ::zerocopy::util::macro_util::core_reexport::mem::size_of::<$t>() != ::zerocopy::util::macro_util::core_reexport::mem::size_of::<$ts>())*
};
}

Expand All @@ -309,11 +311,11 @@ macro_rules! assert_align_gt_eq {
if false {
// The type wildcard in this bound is inferred to be `T` because
// `align_of.into_t()` is assigned to `t` (which has type `T`).
let align_of: $crate::macro_util::AlignOf<_> = unreachable!();
let align_of: $crate::util::macro_util::AlignOf<_> = unreachable!();
$t = align_of.into_t();
// `max_aligns` is inferred to have type `MaxAlignsOf<T, U>` because
// of the inferred types of `t` and `u`.
let mut max_aligns = $crate::macro_util::MaxAlignsOf::new($t, $u);
let mut max_aligns = $crate::util::macro_util::MaxAlignsOf::new($t, $u);

// This transmute will only compile successfully if
// `align_of::<T>() == max(align_of::<T>(), align_of::<U>())` - in
Expand All @@ -324,7 +326,7 @@ macro_rules! assert_align_gt_eq {
// Clippy: We can't annotate the types; this macro is designed
// to infer the types from the calling context.
#[allow(clippy::missing_transmute_annotations)]
$crate::macro_util::core_reexport::mem::transmute(align_of)
$crate::util::macro_util::core_reexport::mem::transmute(align_of)
};
} else {
loop {}
Expand All @@ -349,7 +351,7 @@ macro_rules! assert_size_eq {
// - We can't annotate the types; this macro is designed to
// infer the types from the calling context.
#[allow(clippy::useless_transmute, clippy::missing_transmute_annotations)]
$crate::macro_util::core_reexport::mem::transmute($t)
$crate::util::macro_util::core_reexport::mem::transmute($t)
};
} else {
loop {}
Expand Down
File renamed without changes.
18 changes: 18 additions & 0 deletions src/util.rs → src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
// This file may not be copied, modified, or distributed except according to
// those terms.

#[macro_use]
mod macros;

#[doc(hidden)]
pub mod macro_util;

use core::{
cell::UnsafeCell,
marker::PhantomData,
Expand Down Expand Up @@ -136,6 +142,7 @@ unsafe impl<T, I: Invariants> TransparentWrapper<I> for MaybeUninit<T> {
// `MaybeUninit<T>` is not necessarily a valid `T`.
type ValidityVariance = Invariant;

#[inline(always)]
fn cast_into_inner(ptr: *mut MaybeUninit<T>) -> *mut T {
// SAFETY: Per [1] (from comment above), `MaybeUninit<T>` has the same
// layout as `T`. Thus, this cast preserves size.
Expand All @@ -144,6 +151,7 @@ unsafe impl<T, I: Invariants> TransparentWrapper<I> for MaybeUninit<T> {
ptr.cast::<T>()
}

#[inline(always)]
fn cast_from_inner(ptr: *mut T) -> *mut MaybeUninit<T> {
// SAFETY: Per [1] (from comment above), `MaybeUninit<T>` has the same
// layout as `T`. Thus, this cast preserves size.
Expand Down Expand Up @@ -187,6 +195,7 @@ unsafe impl<T: ?Sized, I: Invariants> TransparentWrapper<I> for ManuallyDrop<T>
// validity as `T`.
type ValidityVariance = Covariant;

#[inline(always)]
fn cast_into_inner(ptr: *mut ManuallyDrop<T>) -> *mut T {
// SAFETY: Per [1] (from comment above), `ManuallyDrop<T>` has the same
// layout as `T`. Thus, this cast preserves size even if `T` is unsized.
Expand All @@ -196,6 +205,7 @@ unsafe impl<T: ?Sized, I: Invariants> TransparentWrapper<I> for ManuallyDrop<T>
return ptr as *mut T;
}

#[inline(always)]
fn cast_from_inner(ptr: *mut T) -> *mut ManuallyDrop<T> {
// SAFETY: Per [1] (from comment above), `ManuallyDrop<T>` has the same
// layout as `T`. Thus, this cast preserves size even if `T` is unsized.
Expand Down Expand Up @@ -249,6 +259,7 @@ unsafe impl<T, I: Invariants> TransparentWrapper<I> for Wrapping<T> {
// [2] https://doc.rust-lang.org/core/num/struct.Wrapping.html
type ValidityVariance = Covariant;

#[inline(always)]
fn cast_into_inner(ptr: *mut Wrapping<T>) -> *mut T {
// SAFETY: Per [1] (from comment above), `Wrapping<T>` has the same
// layout as `T`. Thus, this cast preserves size.
Expand All @@ -257,6 +268,7 @@ unsafe impl<T, I: Invariants> TransparentWrapper<I> for Wrapping<T> {
ptr.cast::<T>()
}

#[inline(always)]
fn cast_from_inner(ptr: *mut T) -> *mut Wrapping<T> {
// SAFETY: Per [1] (from comment above), `Wrapping<T>` has the same
// layout as `T`. Thus, this cast preserves size.
Expand Down Expand Up @@ -296,6 +308,7 @@ unsafe impl<T: ?Sized, I: Invariants> TransparentWrapper<I> for UnsafeCell<T> {
// between `T` and `UnsafeCell<T>`.
type ValidityVariance = Covariant;

#[inline(always)]
fn cast_into_inner(ptr: *mut UnsafeCell<T>) -> *mut T {
// SAFETY: Per [1] (from comment above), `UnsafeCell<T>` has the same
// representation as `T`. Thus, this cast preserves size.
Expand All @@ -305,6 +318,7 @@ unsafe impl<T: ?Sized, I: Invariants> TransparentWrapper<I> for UnsafeCell<T> {
return ptr as *mut T;
}

#[inline(always)]
fn cast_from_inner(ptr: *mut T) -> *mut UnsafeCell<T> {
// SAFETY: Per [1] (from comment above), `UnsafeCell<T>` has the same
// representation as `T`. Thus, this cast preserves size.
Expand Down Expand Up @@ -333,6 +347,7 @@ unsafe impl<T, I: Invariants> TransparentWrapper<I> for Unalign<T> {
// SAFETY: `Unalign<T>` promises to have the same validity as `T`.
type ValidityVariance = Covariant;

#[inline(always)]
fn cast_into_inner(ptr: *mut Unalign<T>) -> *mut T {
// SAFETY: Per the safety comment on the impl block, `Unalign<T>` has
// the size as `T`. Thus, this cast preserves size.
Expand All @@ -341,6 +356,7 @@ unsafe impl<T, I: Invariants> TransparentWrapper<I> for Unalign<T> {
ptr.cast::<T>()
}

#[inline(always)]
fn cast_from_inner(ptr: *mut T) -> *mut Unalign<T> {
// SAFETY: Per the safety comment on the impl block, `Unalign<T>` has
// the size as `T`. Thus, this cast preserves size.
Expand Down Expand Up @@ -436,6 +452,7 @@ macro_rules! unsafe_impl_transparent_wrapper_for_atomic {
// integer type
type ValidityVariance = crate::util::Covariant;

#[inline(always)]
fn cast_into_inner(ptr: *mut $atomic) -> *mut UnsafeCell<$native> {
// SAFETY: Per [1] (from comment on impl block), `$atomic` has the
// same size as `$native`. Thus, this cast preserves size.
Expand All @@ -444,6 +461,7 @@ macro_rules! unsafe_impl_transparent_wrapper_for_atomic {
ptr.cast::<UnsafeCell<$native>>()
}

#[inline(always)]
fn cast_from_inner(ptr: *mut UnsafeCell<$native>) -> *mut $atomic {
// SAFETY: Per [1] (from comment on impl block), `$atomic` has the
// same size as `$native`. Thus, this cast preserves size.
Expand Down
4 changes: 2 additions & 2 deletions tests/trybuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
// particular:
// - Some tests directly include `zerocopy-derive/tests/include.rs`, which
// derives traits on the `AU16` type.
// - The file `invalid-impls.rs` directly includes `src/macros.rs` in order to
// test the `impl_or_verify!` macro which is defined in that file.
// - The file `invalid-impls.rs` directly includes `src/util/macros.rs` in order
// to test the `impl_or_verify!` macro which is defined in that file.
// Specifically, it tests the verification portion of that macro, which is
// enabled when `cfg(any(feature = "derive", test))`. While `--cfg test` is of
// course passed to the code in the file you're reading right now, `trybuild`
Expand Down
20 changes: 10 additions & 10 deletions tests/ui-msrv/invalid-impls/invalid-impls.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0277]: the trait bound `T: zerocopy::TryFromBytes` is not satisfied
--> tests/ui-msrv/invalid-impls/../../../src/macros.rs
--> tests/ui-msrv/invalid-impls/../../../src/util/macros.rs
|
| impl<$($tyvar $(: $(? $optbound +)* $($bound +)*)?),*> Subtrait for $ty {}
| ^^^^^^^^ the trait `zerocopy::TryFromBytes` is not implemented for `T`
Expand All @@ -15,7 +15,7 @@ note: required because of the requirements on the impl of `zerocopy::TryFromByte
22 | #[derive(FromBytes, IntoBytes, Unaligned)]
| ^^^^^^^^^
note: required by a bound in `_::Subtrait`
--> tests/ui-msrv/invalid-impls/../../../src/macros.rs
--> tests/ui-msrv/invalid-impls/../../../src/util/macros.rs
|
| trait Subtrait: $trait {}
| ^^^^^^ required by this bound in `_::Subtrait`
Expand All @@ -31,7 +31,7 @@ help: consider restricting type parameter `T`
| ++++++++++++++++++++++++

error[E0277]: the trait bound `T: zerocopy::FromZeros` is not satisfied
--> tests/ui-msrv/invalid-impls/../../../src/macros.rs
--> tests/ui-msrv/invalid-impls/../../../src/util/macros.rs
|
| impl<$($tyvar $(: $(? $optbound +)* $($bound +)*)?),*> Subtrait for $ty {}
| ^^^^^^^^ the trait `zerocopy::FromZeros` is not implemented for `T`
Expand All @@ -47,7 +47,7 @@ note: required because of the requirements on the impl of `zerocopy::FromZeros`
22 | #[derive(FromBytes, IntoBytes, Unaligned)]
| ^^^^^^^^^
note: required by a bound in `_::Subtrait`
--> tests/ui-msrv/invalid-impls/../../../src/macros.rs
--> tests/ui-msrv/invalid-impls/../../../src/util/macros.rs
|
| trait Subtrait: $trait {}
| ^^^^^^ required by this bound in `_::Subtrait`
Expand All @@ -63,7 +63,7 @@ help: consider restricting type parameter `T`
| +++++++++++++++++++++

error[E0277]: the trait bound `T: zerocopy::FromBytes` is not satisfied
--> tests/ui-msrv/invalid-impls/../../../src/macros.rs
--> tests/ui-msrv/invalid-impls/../../../src/util/macros.rs
|
| impl<$($tyvar $(: $(? $optbound +)* $($bound +)*)?),*> Subtrait for $ty {}
| ^^^^^^^^ the trait `zerocopy::FromBytes` is not implemented for `T`
Expand All @@ -79,7 +79,7 @@ note: required because of the requirements on the impl of `zerocopy::FromBytes`
22 | #[derive(FromBytes, IntoBytes, Unaligned)]
| ^^^^^^^^^
note: required by a bound in `_::Subtrait`
--> tests/ui-msrv/invalid-impls/../../../src/macros.rs
--> tests/ui-msrv/invalid-impls/../../../src/util/macros.rs
|
| trait Subtrait: $trait {}
| ^^^^^^ required by this bound in `_::Subtrait`
Expand All @@ -95,7 +95,7 @@ help: consider restricting type parameter `T`
| +++++++++++++++++++++

error[E0277]: the trait bound `T: zerocopy::IntoBytes` is not satisfied
--> tests/ui-msrv/invalid-impls/../../../src/macros.rs
--> tests/ui-msrv/invalid-impls/../../../src/util/macros.rs
|
| impl<$($tyvar $(: $(? $optbound +)* $($bound +)*)?),*> Subtrait for $ty {}
| ^^^^^^^^ the trait `zerocopy::IntoBytes` is not implemented for `T`
Expand All @@ -111,7 +111,7 @@ note: required because of the requirements on the impl of `zerocopy::IntoBytes`
22 | #[derive(FromBytes, IntoBytes, Unaligned)]
| ^^^^^^^^^
note: required by a bound in `_::Subtrait`
--> tests/ui-msrv/invalid-impls/../../../src/macros.rs
--> tests/ui-msrv/invalid-impls/../../../src/util/macros.rs
|
| trait Subtrait: $trait {}
| ^^^^^^ required by this bound in `_::Subtrait`
Expand All @@ -127,7 +127,7 @@ help: consider restricting type parameter `T`
| +++++++++++++++++++++

error[E0277]: the trait bound `T: zerocopy::Unaligned` is not satisfied
--> tests/ui-msrv/invalid-impls/../../../src/macros.rs
--> tests/ui-msrv/invalid-impls/../../../src/util/macros.rs
|
| impl<$($tyvar $(: $(? $optbound +)* $($bound +)*)?),*> Subtrait for $ty {}
| ^^^^^^^^ the trait `zerocopy::Unaligned` is not implemented for `T`
Expand All @@ -143,7 +143,7 @@ note: required because of the requirements on the impl of `zerocopy::Unaligned`
22 | #[derive(FromBytes, IntoBytes, Unaligned)]
| ^^^^^^^^^
note: required by a bound in `_::Subtrait`
--> tests/ui-msrv/invalid-impls/../../../src/macros.rs
--> tests/ui-msrv/invalid-impls/../../../src/util/macros.rs
|
| trait Subtrait: $trait {}
| ^^^^^^ required by this bound in `_::Subtrait`
Expand Down
Loading

0 comments on commit de883a7

Please sign in to comment.