I want to build a struct from an array like so:
use std::convert::TryFrom;
impl TryFrom for Chunk {
type Error = Error;
fn try_from<&[u8]>(bytes: &[u8]) -> Result<Self> {
// construct
}
}
The array needs to be sliced up in some pieces, those pieces are then converted to their required datatypes and used for initializing the struct.
But I ran across this issue: The size of the varying member of the array is given as first four bytes of the struct bytes[0..4]. But when I try to access even those four bytes I get error that the size of array is not known at compile time, therefore Rust is not allowing me to index it.
What is the correct, Rustacean way of doing what I am trying to accomplish? How does one index an array size of which is unknown at compile time?
Chunk? Are the types being pulled from the byte array simple, numeric types, such asu8,f32, etc? If so, thebytescrate is very useful for doing exactly this.bytes(argument passed to function) is just an array ofu8numbers, not all types pulled from the array are numeric. But thats not really what I am after. I just want to divide the array into fixed number of slices.bytescrate won't do exactly what you need, so see my answer.