Generated Rust structures without `size` can fail size check due to Rust layout alignment
philpax opened this issue · comments
Philpax commented
For a type like
type MyType {
// elided
#[address(0xEC4)]
enabled: bool,
}
The following code is generated:
#[repr(C)]
pub struct MyType {
// elided
_field_0: [u8; 3780],
pub enabled: bool,
}
#[allow(non_snake_case)]
#[allow(dead_code)]
fn _MyType_size_check() {
unsafe {
::std::mem::transmute::<_, MyType>([0u8; 3781usize]);
}
unreachable!()
}
This fails to compile as Rust will pad MyType
's enabled
field to 4 bytes, resulting in a type size of 3784 bytes. To fix this, Pyxis either needs to
- know about the padding rules of its target language, and generate code appropriately
- just use the
packed
repr and deal with the corresponding pain - do nothing and force the user to specify their type exactly (i.e. specifying
size
or inserting fields that fix alignment)
At this time, I'm leaning towards 3), but may do 2) for correctness in the future. (Rust makes working with unaligned fields in packed structs annoying, but this may not be as big an issue as I think it is?)