Skip to content

Commit

Permalink
[rust] use explicit repr(C, packed) instead of just repr(packed)
Browse files Browse the repository at this point in the history
`repr(packed)` only guarantees no internal padding, not that the fields
will not be reordered.

Signed-off-by: Gary Guo <[email protected]>
  • Loading branch information
nbdd0121 authored and jwnrt committed Aug 12, 2024
1 parent 11d63ff commit fd78577
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions doc/rust_for_c_devs.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ struct MyCStruct {
```
This is guaranteed to lay out fields in declaration order, adding padding for alignment.
`#[repr(Rust)]` is the implicit default.
`#[repr(packed)]` is analogous to `__attribute__((packed))`, and will not produce any padding[^21].
The alignment of the whole struct can be forced to a larger value using `#[repr(align(N))]`, similar to `_Alignas`.
`#[repr(C, packed)]` is analogous to `__attribute__((packed))`, and will not produce any padding[^21].
The alignment of the whole struct can be forced to a larger value using `#[repr(C, align(N))]`, similar to `_Alignas`.

Fields can be accessed using the same dot syntax as C: `my_struct.foo`, `my_struct.bar = 5;`.

Expand Down
12 changes: 6 additions & 6 deletions sw/host/opentitanlib/src/transport/hyperdebug/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const USB_MAX_SIZE: usize = 64;
/// (receiving at most 127 bytes).
#[derive(AsBytes, FromBytes, FromZeroes, Debug)]
#[allow(dead_code)] // Fields not explicitly read anywhere
#[repr(packed)]
#[repr(C, packed)]
struct CmdTransferShort {
encapsulation_header: u8,
port: u8,
Expand All @@ -51,7 +51,7 @@ struct CmdTransferShort {
/// (receiving up to 32767 bytes).
#[derive(AsBytes, FromBytes, FromZeroes, Debug)]
#[allow(dead_code)] // Fields not explicitly read anywhere
#[repr(packed)]
#[repr(C, packed)]
struct CmdTransferLong {
encapsulation_header: u8,
port: u8,
Expand All @@ -66,7 +66,7 @@ struct CmdTransferLong {
/// Wire format of USB packet containing I2C transaction response.
#[derive(AsBytes, FromBytes, FromZeroes, Debug)]
#[allow(dead_code)] // Reserved field not read anywhere
#[repr(packed)]
#[repr(C, packed)]
struct RspTransfer {
encapsulation_header: u8,
status_code: u16,
Expand All @@ -86,7 +86,7 @@ impl RspTransfer {

#[derive(AsBytes, FromBytes, FromZeroes, Debug)]
#[allow(dead_code)] // Fields not explicitly read anywhere
#[repr(packed)]
#[repr(C, packed)]
struct CmdGetDeviceStatus {
encapsulation_header: u8,
port: u8,
Expand All @@ -102,7 +102,7 @@ const I2C_DEVICE_CMD_PREPARE_READ_DATA: u8 = 0x01;
const I2C_DEVICE_FLAG_STICKY: u8 = 0x80;

#[derive(AsBytes, FromBytes, FromZeroes, Debug)]
#[repr(packed)]
#[repr(C, packed)]
struct RspGetDeviceStatus {
encapsulation_header: u8,
struct_size: u16,
Expand All @@ -126,7 +126,7 @@ impl RspGetDeviceStatus {

#[derive(AsBytes, FromBytes, FromZeroes, Debug)]
#[allow(dead_code)] // Fields not explicitly read anywhere
#[repr(packed)]
#[repr(C, packed)]
struct CmdPrepareReadData {
encapsulation_header: u8,
port: u8,
Expand Down

0 comments on commit fd78577

Please sign in to comment.