这是使用Rust从二进制文件读取结构的最自然方式吗?它有效,但似乎有点奇怪(为什么我只能填充结构批发?)。
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]),
}
}
}