How to read file in bytes to different types vec

In my situation, the data is saved in a file
first 4 bytes gives the length of array
second 4 bytes gives the type of variable in the array, like 0 means i8, 1 means i16
next the dataset in given type, for example f32

The usage looks like
let mut f = File::open(file_dir).expect("File not found!");
let data = read_data(f);

I tried to find a function to read file like this, but failed.
And I tried to combine match and something like slice::from_raw_parts_mut to get it.
But still, I can do nothing to get a function that can read such file to a vector in different types.

Can anybody show me a function to solve the problem.
Thank you very much.

The types need to be known at compile time, so you’ll need define something like an enum to unify the different types you might read and then return a vector of those enums.

On mobile, so I can’t give a more complete explanation right now.

My friend, check out deku.
For folks who struggle with serde and nom**, it's like a breath of fresh air.

** use cases where nom / serde can be replaced.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.