liveBook · Manning

C representation

As explained in Embedded Software with Rust

`repr(C)` is a Rust representation attribute that applies C-compatible layout rules to a type.

`#[repr(C)]` gives a Rust type C-compatible layout rules. For a struct, fields remain in declaration order, with padding determined by C ABI alignment rules. Rust’s default struct layout makes no such guarantee, so it cannot be relied on to match a C struct or a hardware register block. section 5.4.2 section 8.2.3

C layout can include gaps between fields and padding at the end. For example, a struct with an 8-bit `id`, a 16-bit `value`, and an 8-bit `status` has offsets 0, 2, and 4 in the illustrated layout; tail padding brings its size to 6 bytes. section 8.2.3

#[repr(C)]
pub struct SensorData {
    id: u8,
    value: u16,
    status: u8,
}

The Rust version of the sensor struct uses `#[repr(C)]` to follow the corresponding C layout. section 8.2.3

Check assumptions on the target: a Rust constant assertion and a C static assertion can verify the expected struct size at build time. Matching layout is necessary at the language boundary, alongside matching type sizes and calling conventions. section 8.2.3 section 8.7

const _: () = assert!(core::mem::size_of::<SensorData>() == 6);

This Rust assertion checks that `SensorData` occupies the expected 6 bytes. section 8.2.3

For memory-mapped I/O, use `#[repr(C)]` to model register order and explicit `_reserved` fields for gaps. Layout alone does not make access volatile: use volatile reads and writes or a wrapper such as `vcell::VolatileCell`. Avoid `#[repr(packed)]` for MMIO register overlays. section 5.4.2 section 5.6

#[repr(C)]
pub struct UsartRegisters {
    // Status Register @ 0x00
    sr: vcell::VolatileCell<u32>,
    // Data Register @ 0x04
    dr: vcell::VolatileCell<u32>,
    // Baud Rate Register @ 0x08
    brr: vcell::VolatileCell<u32>,
    // Control Register 1 @ 0x0C
    cr1: vcell::VolatileCell<u32>,
    // Control Register 2 @ 0x10
    cr2: vcell::VolatileCell<u32>,
    // Control Register 3 @ 0x14
    cr3: vcell::VolatileCell<u32>,
    // Guard Time and Prescaler Register @ 0x18
    gtpr: vcell::VolatileCell<u32>,
}

This register block combines C representation with `VolatileCell` fields, showing layout and volatile access as separate concerns. section 5.4.2

Enums need separate attention: `#[repr(C)]` alone is not the whole solution for matching an enum across the boundary. A fixed-width representation can control its size, but the Rust and C sides must still agree on that representation. section 8.2.3 section 8.7

How Embedded Software with Rust covers it

How deep it goes
Explains field order, alignment, padding, and size assertions; flags enum representation as requiring additional care.
Examples
Uses a USART register block and a sensor struct with an asserted six-byte size.
What you can do after
Apply it to Rust register blocks and C-facing structs, while checking size assumptions and handling volatile access separately.
How it connects
Connects struct layout to the C ABI, memory-mapped I/O, volatile access, and alignment.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Embedded Software with Rust. The section links point to the parts of the book it draws on.