Member-only story
I Built a Rust Data Structure Whose Memory Layout Depends on Its Type
Generic consts, compile-time dimensions, raw layout APIs, and ownership make it possible to push configuration decisions out of runtime — but they also reveal how quickly “zero-cost abstraction” becomes a compiler problem.
I wanted a grid whose dimensions were part of its type. A 3 × 4 grid should contain exactly twelve elements, occupy exactly the space those elements require, and reject operations that expect a different shape.
There would be no stored row count, column count, capacity, or pointer to a separate buffer in the inline version. Changing the element type or dimensions would change the object’s memory layout.
The working implementation turned out to be small. The interesting problems appeared around it: expressing arithmetic in a type, constructing large objects without large stack temporaries, handling partially initialized memory, and understanding what the compiler could actually prove.
I tested the stable implementation with Rust 1.99.0 in an x86–64 Linux environment. I also exercised its initialization paths with Miri. The resulting evidence was concrete: exact object sizes, correct…










