@@ -431,10 +431,16 @@ fn checkedbitpattern_aligned_struct() {
431431
432432#[ test]
433433fn checkedbitpattern_c_default_discriminant_enum_with_fields ( ) {
434+ // these cfg's *actually* care about align_of::<u64>(), but that is highly
435+ // correlated with target pointer width.
436+ #[ cfg( target_pointer_width = "64" ) ]
434437 let pod = [
435438 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0xcc , 0x55 , 0x55 , 0x55 ,
436439 0x55 , 0x55 , 0x55 , 0xcc ,
437440 ] ;
441+ #[ cfg( target_pointer_width = "32" ) ]
442+ let pod =
443+ [ 0x00 , 0x00 , 0x00 , 0x00 , 0xcc , 0x55 , 0x55 , 0x55 , 0x55 , 0x55 , 0x55 , 0xcc ] ;
438444 let value = bytemuck:: checked:: pod_read_unaligned :: <
439445 CheckedBitPatternCDefaultDiscriminantEnumWithFields ,
440446 > ( & pod) ;
@@ -443,10 +449,14 @@ fn checkedbitpattern_c_default_discriminant_enum_with_fields() {
443449 CheckedBitPatternCDefaultDiscriminantEnumWithFields :: A ( 0xcc555555555555cc )
444450 ) ;
445451
452+ #[ cfg( target_pointer_width = "64" ) ]
446453 let pod = [
447454 0x01 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0xcc , 0x55 , 0x55 , 0x55 ,
448455 0x55 , 0x55 , 0x55 , 0xcc ,
449456 ] ;
457+ #[ cfg( target_pointer_width = "32" ) ]
458+ let pod =
459+ [ 0x01 , 0x00 , 0x00 , 0x00 , 0xcc , 0x55 , 0x55 , 0x55 , 0x55 , 0x55 , 0x55 , 0xcc ] ;
450460 let value = bytemuck:: checked:: pod_read_unaligned :: <
451461 CheckedBitPatternCDefaultDiscriminantEnumWithFields ,
452462 > ( & pod) ;
@@ -490,19 +500,36 @@ fn checkedbitpattern_int_enum_with_fields() {
490500
491501#[ test]
492502fn checkedbitpattern_nested_enum_with_fields ( ) {
493- // total size 24 bytes. first byte always the u8 discriminant.
503+ // these cfg's *actually* care about align_of::<u64>(), but that is highly
504+ // correlated with target pointer width.
494505
506+ // total size 24 bytes on 64-bit. first byte always the u8 discriminant.
507+ #[ cfg( target_pointer_width = "64" ) ]
495508 #[ repr( C , align( 8 ) ) ]
496509 struct Align8Bytes ( [ u8 ; 24 ] ) ;
497510
511+ // total size 16 bytes on 64-bit. first byte always the u8 discriminant.
512+ #[ cfg( target_pointer_width = "32" ) ]
513+ #[ repr( C , align( 8 ) ) ]
514+ struct Align8Bytes ( [ u8 ; 16 ] ) ;
515+
498516 // first we'll check variantA, nested variant A
499- let pod = Align8Bytes ( [
517+ #[ cfg( target_pointer_width = "64" ) ]
518+ let mut pod = Align8Bytes ( [
500519 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
501520 0x00 , // byte 0 discriminant = 0 = variant A, bytes 1-7 irrelevant padding.
502521 0x00 , 0x00 , 0x00 , 0x00 , 0xcc , 0x55 , 0x55 ,
503522 0xcc , // bytes 8-15 are the nested CheckedBitPatternCEnumWithFields,
504523 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , // bytes 16-23 padding
505524 ] ) ;
525+ #[ cfg( target_pointer_width = "32" ) ]
526+ let mut pod = Align8Bytes ( [
527+ 0x00 , 0x00 , 0x00 ,
528+ 0x00 , // byte 0 discriminant = 0 = variant A, bytes 1-3 irrelevant padding.
529+ 0x00 , 0x00 , 0x00 , 0x00 , 0xcc , 0x55 , 0x55 ,
530+ 0xcc , // bytes 4-11 are the nested CheckedBitPatternCEnumWithFields,
531+ 0x00 , 0x00 , 0x00 , 0x00 , // bytes 12-15 padding
532+ ] ) ;
506533 let value =
507534 bytemuck:: checked:: from_bytes :: < CheckedBitPatternEnumNested > ( & pod. 0 ) ;
508535 assert_eq ! (
@@ -513,18 +540,13 @@ fn checkedbitpattern_nested_enum_with_fields() {
513540 ) ;
514541
515542 // next we'll check invalid first discriminant fails
516- let pod = Align8Bytes ( [
517- 0x02 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
518- 0x00 , // byte 0 discriminant = 2 = invalid, bytes 1-7 padding
519- 0x00 , 0x00 , 0x00 , 0x00 , 0xcc , 0x55 , 0x55 ,
520- 0xcc , // bytes 8-15 are the nested CheckedBitPatternCEnumWithFields = A,
521- 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , // bytes 16-23 padding
522- ] ) ;
543+ pod. 0 [ 0 ] = 0x02 ; // set the discriminant
523544 let result =
524545 bytemuck:: checked:: try_from_bytes :: < CheckedBitPatternEnumNested > ( & pod. 0 ) ;
525546 assert_eq ! ( result, Err ( CheckedCastError :: InvalidBitPattern ) ) ;
526547
527548 // next we'll check variant B, nested variant B
549+ #[ cfg( target_pointer_width = "64" ) ]
528550 let pod = Align8Bytes ( [
529551 0x01 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
530552 0x00 , // byte 0 discriminant = 1 = variant B, bytes 1-7 padding
@@ -533,7 +555,18 @@ fn checkedbitpattern_nested_enum_with_fields() {
533555 * CheckedBitPatternCDefaultDiscrimimantEnumWithFields, 1 (LE byte
534556 * order) = variant B */
535557 0xcc , 0x55 , 0x55 , 0x55 , 0x55 , 0x55 , 0x55 ,
536- 0xcc , // bytes 16-13 is the data contained in nested variant B
558+ 0xcc , // bytes 16-23 is the data contained in nested variant B
559+ ] ) ;
560+ #[ cfg( target_pointer_width = "32" ) ]
561+ let pod = Align8Bytes ( [
562+ 0x01 , 0x00 , 0x00 ,
563+ 0x00 , // byte 0 discriminant = 1 = variant B, bytes 1-3 padding
564+ 0x01 , 0x00 , 0x00 ,
565+ 0x00 , /* bytes 4-11 is C int size discriminant of
566+ * CheckedBitPatternCDefaultDiscrimimantEnumWithFields, 1 (LE byte
567+ * order) = variant B */
568+ 0xcc , 0x55 , 0x55 , 0x55 , 0x55 , 0x55 , 0x55 ,
569+ 0xcc , // bytes 12-15 is the data contained in nested variant B
537570 ] ) ;
538571 let value =
539572 bytemuck:: checked:: from_bytes :: < CheckedBitPatternEnumNested > ( & pod. 0 ) ;
@@ -547,6 +580,7 @@ fn checkedbitpattern_nested_enum_with_fields() {
547580 ) ;
548581
549582 // finally we'll check variant B, nested invalid discriminant
583+ #[ cfg( target_pointer_width = "64" ) ]
550584 let pod = Align8Bytes ( [
551585 0x01 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
552586 0x00 , // 1 discriminant = variant B, bytes 1-7 padding
@@ -555,7 +589,18 @@ fn checkedbitpattern_nested_enum_with_fields() {
555589 * CheckedBitPatternCDefaultDiscrimimantEnumWithFields, 0x08 is
556590 * invalid */
557591 0xcc , 0x55 , 0x55 , 0x55 , 0x55 , 0x55 , 0x55 ,
558- 0xcc , // bytes 16-13 is the data contained in nested variant B
592+ 0xcc , // bytes 16-23 is the data contained in nested variant B
593+ ] ) ;
594+ #[ cfg( target_pointer_width = "32" ) ]
595+ let pod = Align8Bytes ( [
596+ 0x01 , 0x00 , 0x00 ,
597+ 0x00 , // 1 discriminant = variant B, bytes 1-7 padding
598+ 0x08 , 0x00 , 0x00 ,
599+ 0x00 , /* bytes 4-11 is C int size discriminant of
600+ * CheckedBitPatternCDefaultDiscrimimantEnumWithFields, 0x08 is
601+ * invalid */
602+ 0xcc , 0x55 , 0x55 , 0x55 , 0x55 , 0x55 , 0x55 ,
603+ 0xcc , // bytes 12-15 is the data contained in nested variant B
559604 ] ) ;
560605 let result =
561606 bytemuck:: checked:: try_from_bytes :: < CheckedBitPatternEnumNested > ( & pod. 0 ) ;
0 commit comments