You can do the same in Rust. You just have to be careful when defining the structure.
use std::mem; #[repr(C)] #[packed] struct YourProtoHeader { magic: u8, len: u32 } let mut buf = [0u8, ..1024];
Unfortunately, I do not know how to specify the buffer exactly size_of::<YourProtoHeader>() size; the length of the buffer should be constant, but calling size_of() is technically a function, so Rust complains when I use it in an array initializer. However, a sufficiently large buffer will also work.
Here we convert the pointer to the beginning of the buffer to the pointer to your structure. This is the same thing you would do in C. The structure itself should be annotated with the attributes #[repr(C)] and #[pack] : the first eliminates the possible reordering of the field, the second disables the filling to align the fields.
Vladimir Matveev
source share