diff options
author | Emilio Cobos Álvarez <emilio@crisal.io> | 2020-12-20 17:07:40 +0100 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2020-12-20 21:29:47 +0100 |
commit | 669dc1b628b16da33cc382142c53f6377120a3b8 (patch) | |
tree | f08354425b49284f14abf02db51e817fc52b2d06 /src/codegen/bitfield_unit_tests.rs | |
parent | 98841b32ed51e4474377712aa636251d9a4f7215 (diff) |
comp: Fix bitfields to allow underaligned fields after them to take padding space.
Fixes #1947.
There are two separate issues here: First, the change in comp.rs ensures
that we don't round up the amount of storage to the alignment of the
bitfield. That generates the "expected" output in #1947
(`__BindgenBitfieldUnit<[u8; 3], u16>`).
But that's still not enough to fix that test-case because
__BindgenBitfieldUnit would be aligned and have padding, and Rust won't
put the extra field in the padding.
In order to ensure the bitfield starts at the right alignment, but that
Rust can put stuff in the extra field, we need to make a breaking change
and split the generated fields in two: One preceding that guarantees
alignment, and the actual storage, bit-aligned.
This keeps the existing behavior while fixing that test-case.
Diffstat (limited to 'src/codegen/bitfield_unit_tests.rs')
-rw-r--r-- | src/codegen/bitfield_unit_tests.rs | 49 |
1 files changed, 5 insertions, 44 deletions
diff --git a/src/codegen/bitfield_unit_tests.rs b/src/codegen/bitfield_unit_tests.rs index 3a9239c2..e143e4ea 100644 --- a/src/codegen/bitfield_unit_tests.rs +++ b/src/codegen/bitfield_unit_tests.rs @@ -22,12 +22,10 @@ //! ``` use super::bitfield_unit::__BindgenBitfieldUnit; -use std::mem; #[test] fn bitfield_unit_get_bit() { - let unit = - __BindgenBitfieldUnit::<[u8; 2], u64>::new([0b10011101, 0b00011101]); + let unit = __BindgenBitfieldUnit::<[u8; 2]>::new([0b10011101, 0b00011101]); let mut bits = vec![]; for i in 0..16 { @@ -50,7 +48,7 @@ fn bitfield_unit_get_bit() { #[test] fn bitfield_unit_set_bit() { let mut unit = - __BindgenBitfieldUnit::<[u8; 2], u64>::new([0b00000000, 0b00000000]); + __BindgenBitfieldUnit::<[u8; 2]>::new([0b00000000, 0b00000000]); for i in 0..16 { if i % 3 == 0 { @@ -63,7 +61,7 @@ fn bitfield_unit_set_bit() { } let mut unit = - __BindgenBitfieldUnit::<[u8; 2], u64>::new([0b11111111, 0b11111111]); + __BindgenBitfieldUnit::<[u8; 2]>::new([0b11111111, 0b11111111]); for i in 0..16 { if i % 3 == 0 { @@ -76,43 +74,6 @@ fn bitfield_unit_set_bit() { } } -#[test] -fn bitfield_unit_align() { - assert_eq!( - mem::align_of::<__BindgenBitfieldUnit<[u8; 1], u8>>(), - mem::align_of::<u8>() - ); - assert_eq!( - mem::align_of::<__BindgenBitfieldUnit<[u8; 1], u16>>(), - mem::align_of::<u16>() - ); - assert_eq!( - mem::align_of::<__BindgenBitfieldUnit<[u8; 1], u32>>(), - mem::align_of::<u32>() - ); - assert_eq!( - mem::align_of::<__BindgenBitfieldUnit<[u8; 1], u64>>(), - mem::align_of::<u64>() - ); - - assert_eq!( - mem::align_of::<__BindgenBitfieldUnit<[u8; 8], u8>>(), - mem::align_of::<u8>() - ); - assert_eq!( - mem::align_of::<__BindgenBitfieldUnit<[u8; 8], u16>>(), - mem::align_of::<u16>() - ); - assert_eq!( - mem::align_of::<__BindgenBitfieldUnit<[u8; 8], u32>>(), - mem::align_of::<u32>() - ); - assert_eq!( - mem::align_of::<__BindgenBitfieldUnit<[u8; 8], u64>>(), - mem::align_of::<u64>() - ); -} - macro_rules! bitfield_unit_get { ( $( @@ -123,7 +84,7 @@ macro_rules! bitfield_unit_get { fn bitfield_unit_get() { $({ let expected = $expected; - let unit = __BindgenBitfieldUnit::<_, u64>::new($storage); + let unit = __BindgenBitfieldUnit::<_>::new($storage); let actual = unit.get($start, $len); println!(); @@ -223,7 +184,7 @@ macro_rules! bitfield_unit_set { #[test] fn bitfield_unit_set() { $( - let mut unit = __BindgenBitfieldUnit::<[u8; 4], u64>::new([0, 0, 0, 0]); + let mut unit = __BindgenBitfieldUnit::<[u8; 4]>::new([0, 0, 0, 0]); unit.set($start, $len, $val); let actual = unit.get(0, 32); |