Skip to content

Commit 0afbe8f

Browse files
authored
Fix 32bit tests (#331)
* Fix CheckedBitPattern derive tests on i686. * Fix try_box_from_bytes test on i686. * Fix CI * Add MSRV note to derive feature
1 parent da74816 commit 0afbe8f

File tree

5 files changed

+64
-19
lines changed

5 files changed

+64
-19
lines changed

.github/workflows/rust.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
include:
2020
# versions (all on linux-x86_64)
2121
- { rust: 1.34.0, os: ubuntu-latest }
22-
- { rust: 1.61.0, os: ubuntu-latest }
22+
- { rust: 1.68.0, os: ubuntu-latest }
2323
- { rust: stable, os: ubuntu-latest }
2424
- { rust: beta, os: ubuntu-latest }
2525
- { rust: nightly, os: ubuntu-latest }
@@ -38,7 +38,7 @@ jobs:
3838
- run: cargo test --verbose --no-default-features
3939
- run: cargo test --verbose
4040
- run: cargo test --verbose --features derive
41-
if: matrix.rust == '1.61.0'
41+
if: matrix.rust == '1.68.0'
4242
- run: cargo test --verbose --all-features
4343
if: matrix.rust == 'nightly'
4444
- run: cargo test --verbose --manifest-path=derive/Cargo.toml --all-features

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ exclude = ["/pedantic.bat", "derive/", "contiguous_bitset/"]
1414
[features]
1515
# In v2 we'll fix these names to be more "normal".
1616

17-
# Enable deriving the various `bytemuck` traits.
17+
# All MSRV notes below are GUIDELINES and future versions may require even more
18+
# MSRV on any feature.
19+
20+
# MSRV 1.68.0: Enable deriving the various `bytemuck` traits.
1821
derive = ["bytemuck_derive"]
1922
# Enable features requiring items from `extern crate alloc`.
2023
extern_crate_alloc = []
@@ -25,9 +28,6 @@ zeroable_maybe_uninit = []
2528
# Implement `Zeroable` for `std::sync::atomic` types.
2629
zeroable_atomics = []
2730

28-
# All MSRV notes below are GUIDELINES and future versions may require even more
29-
# MSRV on any feature.
30-
3131
# MSRV 1.36: Use `align_offset` method instead of casting to `usize` to check
3232
# alignment of pointers, this *may* improve codegen in some cases (but it has
3333
# never been formally benchmarked!)

derive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ keywords = ["transmute", "bytes", "casting"]
99
categories = ["encoding", "no-std"]
1010
edition = "2018"
1111
license = "Zlib OR Apache-2.0 OR MIT"
12-
rust-version = "1.61"
12+
rust-version = "1.68"
1313
#note(lokathor): do not publish with this set
1414
#resolver = "3"
1515

derive/tests/basic.rs

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,16 @@ fn checkedbitpattern_aligned_struct() {
431431

432432
#[test]
433433
fn 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]
492502
fn 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);

tests/std_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn test_try_from_box_bytes() {
5858

5959
// Different layout: target alignment is less than source alignment.
6060
assert_eq!(
61-
try_from_box_bytes::<u32>(Box::new(0u64).into()).map_err(|(x, _)| x),
61+
try_from_box_bytes::<u16>(Box::new(0u64).into()).map_err(|(x, _)| x),
6262
Err(PodCastError::AlignmentMismatch)
6363
);
6464

0 commit comments

Comments
 (0)