How to create an array with size equal some struct size?


#1

Hello,

I want to create an byte array whose size equal some struct size , for example :



use std::mem;

struct Foo {
    a: i64,
}

fn main () {
  let arr: [u8; mem::size_of::()] = unsafe { mem::zeroed() };
}


Above code does not work. Any idea how to create such array from rust lang only.

Is it possible to compute the structure size at compile time (using compiler plugin or macros) like this

let arr: [u8; sizeof!(Foo)] ;

Thanks
Ajay.


#3

Ok I figured it out.

Basically I wanted to read and write binary structures, some thing like this https://github.com/ajayprataps/rust-examples/blob/master/raw1.rs