... seems not possible. Consider the case.
#[repr(packed)]
#[derive(Debug)]
struct EthernetHeader {
src: [u8; 6],
dst: [u8; 6],
ether_type: u16
}
So it's Sized, but let hdr: &[u8; mem::size_of::<EthernetHeader>::()];
fails with
error: array length constant evaluation error: unimplemented constant expression: calling non-local const fn [E0250]
let hdr: &[u8; mem::size_of::<EthernetHeader>()];
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
help: run `rustc --explain E0250` to see a detailed explanation
Is there any way to make it work?
My current goal is to create generic function and use something like [u8; mem::size_of::<T>()]
inside. In C++ sizeof(T)
works in this case, I guess. Another solution to the latest problem that comes to my mind is to add generic integer parameter, manually compute the size and use this parameter instead of mem::size_of::<T>()
, like
fn decode<T, N>(hdr: &T) {
let bytes: [u8; N];
...
}
...
decode<EthernetHeader, 14>(hdr);
This seems doable in C++, but I can't find syntax analogue in rust.
Thank you!