@@ -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,14 @@ 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.
389+ // However, it appears we do not have any tests exercising this.
390+ // TODO: Add tests and revise the above comment.
391+ const _: [ ( ) ; 1 ] = [ ( ) ; $align. is_none( ) as usize ] ;
392+ const _: [ ( ) ; 1 ] = [ ( ) ; $packed. is_none( ) as usize ] ;
383393 let mut max = 0 ;
384394 $( {
385395 let padding = $crate:: util:: macro_util:: size_of:: <$t>( ) - $crate:: util:: macro_util:: size_of:: <$ts>( ) ;
@@ -410,7 +420,14 @@ macro_rules! union_padding {
410420#[ doc( hidden) ] // `#[macro_export]` bypasses this module's `#[doc(hidden)]`.
411421#[ macro_export]
412422macro_rules! enum_padding {
413- ( $t: ty, $disc: ty, $( [ $( $ts: ty) ,* ] ) ,* ) => { {
423+ ( $t: ty, $align: expr, $packed: expr, $disc: ty, $( [ $( $ts: ty) ,* ] ) ,* ) => { {
424+ // The `align` and `packed` directives can be ignored here. Regardless
425+ // of if and how they are set, comparing the size of `$t` to each of its
426+ // field sizes is a reliable indicator of the presence of padding.
427+ // However, it appears we do not have any tests exercising this.
428+ // TODO: Add tests and revise the above comment.
429+ const _: [ ( ) ; 1 ] = [ ( ) ; $align. is_none( ) as usize ] ;
430+ const _: [ ( ) ; 1 ] = [ ( ) ; $packed. is_none( ) as usize ] ;
414431 let mut max = 0 ;
415432 $( {
416433 let padding = $crate:: util:: macro_util:: size_of:: <$t>( )
@@ -927,6 +944,8 @@ pub mod core_reexport {
927944
928945#[ cfg( test) ]
929946mod tests {
947+ use core:: num:: NonZeroUsize ;
948+
930949 use crate :: util:: testutil:: * ;
931950
932951 #[ cfg( __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS) ]
@@ -1125,7 +1144,7 @@ mod tests {
11251144 #[ $cfg]
11261145 #[ allow( dead_code) ]
11271146 struct Test ( $( $ts) ,* ) ;
1128- assert_eq!( struct_padding!( Test , [ $( $ts) ,* ] ) , $expect) ;
1147+ assert_eq!( struct_padding!( Test , None :: < NonZeroUsize > , None :: < NonZeroUsize > , [ $( $ts) ,* ] ) , $expect) ;
11291148 } } ;
11301149 ( #[ $cfg: meta] $( #[ $cfgs: meta] ) * ( $( $ts: ty) ,* ) => $expect: expr) => {
11311150 test!( #[ $cfg] ( $( $ts) ,* ) => $expect) ;
@@ -1156,7 +1175,7 @@ mod tests {
11561175 #[ repr( C ) ]
11571176 #[ allow( dead_code) ]
11581177 struct Test ( $( $ts) ,* ) ;
1159- assert_eq!( repr_c_struct_has_padding!( Test , [ $( $ts) ,* ] ) , $expect) ;
1178+ assert_eq!( repr_c_struct_has_padding!( Test , None :: < NonZeroUsize > , None :: < NonZeroUsize > , [ $( $ts) ,* ] ) , $expect) ;
11601179 } } ;
11611180 }
11621181
@@ -1192,7 +1211,7 @@ mod tests {
11921211 #[ $cfg]
11931212 #[ allow( unused) ] // fields are never read
11941213 union Test { $( $fs: $ts) ,* }
1195- assert_eq!( union_padding!( Test , [ $( $ts) ,* ] ) , $expect) ;
1214+ assert_eq!( union_padding!( Test , None :: < NonZeroUsize > , None :: < usize > , [ $( $ts) ,* ] ) , $expect) ;
11961215 } } ;
11971216 ( #[ $cfg: meta] $( #[ $cfgs: meta] ) * { $( $fs: ident: $ts: ty) ,* } => $expect: expr) => {
11981217 test!( #[ $cfg] { $( $fs: $ts) ,* } => $expect) ;
@@ -1231,7 +1250,7 @@ mod tests {
12311250 $( $vs ( $( $ts) ,* ) , ) *
12321251 }
12331252 assert_eq!(
1234- enum_padding!( Test , $disc, $( [ $( $ts) ,* ] ) ,* ) ,
1253+ enum_padding!( Test , None :: < NonZeroUsize > , None :: < NonZeroUsize > , $disc, $( [ $( $ts) ,* ] ) ,* ) ,
12351254 $expect
12361255 ) ;
12371256 } } ;
0 commit comments