I just realized this is quite similar to What is the best way to parse binary protocols with Rust
Is this the most natural way to read structs from a binary file using Rust? It works but seems a bit odd (why can't I just fill the struct wholesale?).
extern crate byteorder;
use byteorder::{ByteOrder, LittleEndian};
struct Record {
latch: u32,
total_energy: f32,
x_cm: f32,
y_cm: f32,
x_cos: f32,
y_cos: f32,
weight: f32
}
impl Record {
fn make_from_bytes(buffer: &[u8]) -> Record {
Record {
latch: LittleEndian::read_u32(&buffer[0..4]),
total_energy: LittleEndian::read_f32(&buffer[4..8]),
x_cm: LittleEndian::read_f32(&buffer[8..12]),
y_cm: LittleEndian::read_f32(&buffer[12..16]),
x_cos: LittleEndian::read_f32(&buffer[16..20]),
y_cos: LittleEndian::read_f32(&buffer[20..24]),
weight: LittleEndian::read_f32(&buffer[24..28]),
}
}
}