Skip to content

Commit fd78577

Browse files
nbdd0121jwnrt
authored andcommitted
[rust] use explicit repr(C, packed) instead of just repr(packed)
`repr(packed)` only guarantees no internal padding, not that the fields will not be reordered. Signed-off-by: Gary Guo <[email protected]>
1 parent 11d63ff commit fd78577

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

doc/rust_for_c_devs.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ struct MyCStruct {
169169
```
170170
This is guaranteed to lay out fields in declaration order, adding padding for alignment.
171171
`#[repr(Rust)]` is the implicit default.
172-
`#[repr(packed)]` is analogous to `__attribute__((packed))`, and will not produce any padding[^21].
173-
The alignment of the whole struct can be forced to a larger value using `#[repr(align(N))]`, similar to `_Alignas`.
172+
`#[repr(C, packed)]` is analogous to `__attribute__((packed))`, and will not produce any padding[^21].
173+
The alignment of the whole struct can be forced to a larger value using `#[repr(C, align(N))]`, similar to `_Alignas`.
174174

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

sw/host/opentitanlib/src/transport/hyperdebug/i2c.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const USB_MAX_SIZE: usize = 64;
3737
/// (receiving at most 127 bytes).
3838
#[derive(AsBytes, FromBytes, FromZeroes, Debug)]
3939
#[allow(dead_code)] // Fields not explicitly read anywhere
40-
#[repr(packed)]
40+
#[repr(C, packed)]
4141
struct CmdTransferShort {
4242
encapsulation_header: u8,
4343
port: u8,
@@ -51,7 +51,7 @@ struct CmdTransferShort {
5151
/// (receiving up to 32767 bytes).
5252
#[derive(AsBytes, FromBytes, FromZeroes, Debug)]
5353
#[allow(dead_code)] // Fields not explicitly read anywhere
54-
#[repr(packed)]
54+
#[repr(C, packed)]
5555
struct CmdTransferLong {
5656
encapsulation_header: u8,
5757
port: u8,
@@ -66,7 +66,7 @@ struct CmdTransferLong {
6666
/// Wire format of USB packet containing I2C transaction response.
6767
#[derive(AsBytes, FromBytes, FromZeroes, Debug)]
6868
#[allow(dead_code)] // Reserved field not read anywhere
69-
#[repr(packed)]
69+
#[repr(C, packed)]
7070
struct RspTransfer {
7171
encapsulation_header: u8,
7272
status_code: u16,
@@ -86,7 +86,7 @@ impl RspTransfer {
8686

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

104104
#[derive(AsBytes, FromBytes, FromZeroes, Debug)]
105-
#[repr(packed)]
105+
#[repr(C, packed)]
106106
struct RspGetDeviceStatus {
107107
encapsulation_header: u8,
108108
struct_size: u16,
@@ -126,7 +126,7 @@ impl RspGetDeviceStatus {
126126

127127
#[derive(AsBytes, FromBytes, FromZeroes, Debug)]
128128
#[allow(dead_code)] // Fields not explicitly read anywhere
129-
#[repr(packed)]
129+
#[repr(C, packed)]
130130
struct CmdPrepareReadData {
131131
encapsulation_header: u8,
132132
port: u8,

0 commit comments

Comments
 (0)