Skip to content

[rust] use explicit repr(C, packed) instead of just repr(packed) #24256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading