@@ -329,9 +329,12 @@ pub use __size_of::size_of;
329329#[ doc( hidden) ] // `#[macro_export]` bypasses this module's `#[doc(hidden)]`.
330330#[ macro_export]
331331macro_rules! struct_padding {
332- ( $t: ty, [ $( $ts: ty) ,* ] ) => {
332+ ( $t: ty, $_align: expr, $_packed: expr, [ $( $ts: ty) ,* ] ) => { {
333+ // The `align` and `packed` directives can be ignored here. Regardless
334+ // of if and how they are set, comparing the size of `$t` to the sum of
335+ // its field sizes is a reliable indicator of the presence of padding.
333336 $crate:: util:: macro_util:: size_of:: <$t>( ) - ( 0 $( + $crate:: util:: macro_util:: size_of:: <$ts>( ) ) * )
334- } ;
337+ } } ;
335338}
336339
337340/// Does the `repr(C)` struct type `$t` have padding?
@@ -342,10 +345,10 @@ macro_rules! struct_padding {
342345#[ doc( hidden) ] // `#[macro_export]` bypasses this module's `#[doc(hidden)]`.
343346#[ macro_export]
344347macro_rules! repr_c_struct_has_padding {
345- ( $t: ty, [ $( $ts: tt) ,* ] ) => { {
348+ ( $t: ty, $align : expr , $packed : expr , [ $( $ts: tt) ,* ] ) => { {
346349 let layout = $crate:: DstLayout :: for_repr_c_struct(
347- $crate :: util :: macro_util :: core_reexport :: option :: Option :: None ,
348- $crate :: util :: macro_util :: core_reexport :: option :: Option :: None ,
350+ $align ,
351+ $packed ,
349352 & [ $( $crate:: repr_c_struct_has_padding!( @field $ts) , ) * ]
350353 ) ;
351354 layout. requires_static_padding( ) || layout. requires_dynamic_padding( )
@@ -379,7 +382,10 @@ macro_rules! repr_c_struct_has_padding {
379382#[ doc( hidden) ] // `#[macro_export]` bypasses this module's `#[doc(hidden)]`.
380383#[ macro_export]
381384macro_rules! union_padding {
382- ( $t: ty, [ $( $ts: ty) ,* ] ) => { {
385+ ( $t: ty, $_align: expr, $_packed: expr, [ $( $ts: ty) ,* ] ) => { {
386+ // The `align` and `packed` directives can be ignored here. Regardless
387+ // of if and how they are set, comparing the size of `$t` to each of its
388+ // field sizes is a reliable indicator of the presence of padding.
383389 let mut max = 0 ;
384390 $( {
385391 let padding = $crate:: util:: macro_util:: size_of:: <$t>( ) - $crate:: util:: macro_util:: size_of:: <$ts>( ) ;
@@ -410,7 +416,14 @@ macro_rules! union_padding {
410416#[ doc( hidden) ] // `#[macro_export]` bypasses this module's `#[doc(hidden)]`.
411417#[ macro_export]
412418macro_rules! enum_padding {
413- ( $t: ty, $disc: ty, $( [ $( $ts: ty) ,* ] ) ,* ) => { {
419+ ( $t: ty, $_align: expr, $packed: expr, $disc: ty, $( [ $( $ts: ty) ,* ] ) ,* ) => { {
420+ // The `align` and `packed` directives are irrelevant. `$align` can be
421+ // ignored because regardless of if and how it is set, comparing the
422+ // size of `$t` to each of its field sizes is a reliable indicator of
423+ // the presence of padding. `$packed` is irrelevant because it is
424+ // forbidden on enums.
425+ #[ allow( clippy:: as_conversions) ]
426+ const _: [ ( ) ; 1 ] = [ ( ) ; $packed. is_none( ) as usize ] ;
414427 let mut max = 0 ;
415428 $( {
416429 let padding = $crate:: util:: macro_util:: size_of:: <$t>( )
@@ -927,6 +940,8 @@ pub mod core_reexport {
927940
928941#[ cfg( test) ]
929942mod tests {
943+ use core:: num:: NonZeroUsize ;
944+
930945 use crate :: util:: testutil:: * ;
931946
932947 #[ cfg( __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS) ]
@@ -1125,7 +1140,7 @@ mod tests {
11251140 #[ $cfg]
11261141 #[ allow( dead_code) ]
11271142 struct Test ( $( $ts) ,* ) ;
1128- assert_eq!( struct_padding!( Test , [ $( $ts) ,* ] ) , $expect) ;
1143+ assert_eq!( struct_padding!( Test , None :: < NonZeroUsize > , None :: < NonZeroUsize > , [ $( $ts) ,* ] ) , $expect) ;
11291144 } } ;
11301145 ( #[ $cfg: meta] $( #[ $cfgs: meta] ) * ( $( $ts: ty) ,* ) => $expect: expr) => {
11311146 test!( #[ $cfg] ( $( $ts) ,* ) => $expect) ;
@@ -1156,7 +1171,7 @@ mod tests {
11561171 #[ repr( C ) ]
11571172 #[ allow( dead_code) ]
11581173 struct Test ( $( $ts) ,* ) ;
1159- assert_eq!( repr_c_struct_has_padding!( Test , [ $( $ts) ,* ] ) , $expect) ;
1174+ assert_eq!( repr_c_struct_has_padding!( Test , None :: < NonZeroUsize > , None :: < NonZeroUsize > , [ $( $ts) ,* ] ) , $expect) ;
11601175 } } ;
11611176 }
11621177
@@ -1192,7 +1207,7 @@ mod tests {
11921207 #[ $cfg]
11931208 #[ allow( unused) ] // fields are never read
11941209 union Test { $( $fs: $ts) ,* }
1195- assert_eq!( union_padding!( Test , [ $( $ts) ,* ] ) , $expect) ;
1210+ assert_eq!( union_padding!( Test , None :: < NonZeroUsize > , None :: < usize > , [ $( $ts) ,* ] ) , $expect) ;
11961211 } } ;
11971212 ( #[ $cfg: meta] $( #[ $cfgs: meta] ) * { $( $fs: ident: $ts: ty) ,* } => $expect: expr) => {
11981213 test!( #[ $cfg] { $( $fs: $ts) ,* } => $expect) ;
@@ -1231,7 +1246,7 @@ mod tests {
12311246 $( $vs ( $( $ts) ,* ) , ) *
12321247 }
12331248 assert_eq!(
1234- enum_padding!( Test , $disc, $( [ $( $ts) ,* ] ) ,* ) ,
1249+ enum_padding!( Test , None :: < NonZeroUsize > , None :: < NonZeroUsize > , $disc, $( [ $( $ts) ,* ] ) ,* ) ,
12351250 $expect
12361251 ) ;
12371252 } } ;
0 commit comments