ages

Initializing a struct directly in shared memory

When I worked on Arene’s messaging system at Woven by Toyota, we integrated iceoryx2 as a shared-memory transport for low-latency IPC between services in safety-critical automotive software. The core promise of shared-memory IPC is zero copies: a publisher loans an uninitialized slot in shared memory, writes the message directly into that slot, and hands the subscriber an offset into the same physical pages.

In C++, placement new constructs an object directly at a given address. Rust does not have placement new. If I construct a normal Rust struct and write it into the loaned iceoryx2 slot, the language semantics construct the value first and then move it into shared memory:

let frame = Frame {
seq,
timestamp_ns,
payload: [0; 4096],
};
slot.write(frame);

LLVM often eliminates that copy in release builds for small structs, and sometimes does not for larger or more complex ones. In safety-critical or latency-sensitive software, relying on an optimizer heuristic to avoid a stack overflow or a multi-kilobyte memcpy is not an acceptable guarantee.

Why you cannot just take a mutable reference

The first workaround people try is wrapping the loaned shared-memory slot in MaybeUninit<Frame>, taking a &mut Frame, and assigning each field:

use core::mem::MaybeUninit;
let mut slot = MaybeUninit::<Frame>::uninit();
// Undefined behavior: creating &mut Frame to uninitialized memory.
let frame: &mut Frame = unsafe { slot.assume_init_mut() };
frame.seq = 1;

This is undefined behavior even if the very next lines overwrite every field before anyone reads them. In Rust, forming a &mut T asserts to the compiler that the pointee already satisfies the validity invariant of T. For a bool, an enum, a NonNull, or a reference, uninitialized bytes violate that invariant the instant the reference exists.

Even for all-integer structs, projecting through &mut creates intermediate references and invites optimizer assumptions you did not intend. The rule is straightforward: stay in raw pointers until the entire value is initialized.

Writing fields through raw pointers

MaybeUninit::as_mut_ptr gives us a *mut Frame without asserting validity. From there, addr_of_mut! projects to each field as a raw pointer, and ptr::write writes the field without reading or dropping the old uninitialized bytes:1

use core::mem::MaybeUninit;
use core::ptr::addr_of_mut;
#[repr(C)]
struct Frame {
seq: u64,
timestamp_ns: u64,
payload: [u8; 4096],
}
fn init_frame(slot: &mut MaybeUninit<Frame>, seq: u64, ts: u64) {
let ptr = slot.as_mut_ptr();
unsafe {
addr_of_mut!((*ptr).seq).write(seq);
addr_of_mut!((*ptr).timestamp_ns).write(ts);
addr_of_mut!((*ptr).payload).write_bytes(0, 1);
}
}

Notice the third line. Calling .write([0; 4096]) can still materialize a 4 KB zero array on the stack unless optimized away. Calling .write_bytes(0, 1) on *mut [u8; 4096] compiles directly to a memset of 1 * size_of::<[u8; 4096]>() bytes at the destination address in shared memory.

Once every field has been written, slot.assume_init_mut() is sound, and the publisher can send the loaned slot so subscribers read it directly in place with zero copies.

Three traps when you scale this up

Writing init_frame by hand works for a flat 3-field struct, and breaks down quickly across nested automotive message schemas.

First, there is exhaustiveness. If someone adds a fourth field flags: u32 to Frame and forgets to update init_frame, the code still compiles without a warning while leaving uninitialized bytes in flags. With normal struct literals Frame { ... }, the compiler enforces exhaustiveness for free; dropping down to addr_of_mut! gives that up.

Second, there is partial initialization on panic. If field 1 holds a value with a Drop implementation and initializing field 2 panics, field 1 is leaked unless you track which fields were already written and drop them in a cleanup guard.

Third, there is struct padding. In #[repr(C)] IPC messages, implicit padding between a u32 and a u64 is never touched by field-by-field writes.2 If anything hashes, logs, or inspects the message as raw bytes, those padding bytes are uninitialized memory. Explicit padding fields or zeroing the loaned slot avoids that class of bugs.

Wrapping it in a procedural macro

At Woven, we solved this by building a zero-copy procedural macro framework around MaybeUninit initialization. The macro inspects the message struct at compile time, generates an in-place initializer that projects each field via raw pointers, enforces at the type level that every field is initialized before publishing, and composes recursively across nested message types.

Services get an ergonomic, checked API for constructing messages directly inside iceoryx2 shared memory, and subscribers read them back without copying a single byte.