如何从 Rust 中的文件读取结构体?

2024-04-16

有没有办法可以直接从 Rust 文件中读取结构?我的代码是:

use std::fs::File;

struct Configuration {
    item1: u8,
    item2: u16,
    item3: i32,
    item4: [char; 8],
}

fn main() {
    let file = File::open("config_file").unwrap();

    let mut config: Configuration;
    // How to read struct from file?
}

我如何将我的配置直接读入config从文件中?这可能吗?


干得好:

use std::io::Read;
use std::mem;
use std::slice;

#[repr(C, packed)]
#[derive(Debug, Copy, Clone)]
struct Configuration {
    item1: u8,
    item2: u16,
    item3: i32,
    item4: [char; 8],
}

const CONFIG_DATA: &[u8] = &[
    0xfd, // u8
    0xb4, 0x50, // u16
    0x45, 0xcd, 0x3c, 0x15, // i32
    0x71, 0x3c, 0x87, 0xff, // char
    0xe8, 0x5d, 0x20, 0xe7, // char
    0x5f, 0x38, 0x05, 0x4a, // char
    0xc4, 0x58, 0x8f, 0xdc, // char
    0x67, 0x1d, 0xb4, 0x64, // char
    0xf2, 0xc5, 0x2c, 0x15, // char
    0xd8, 0x9a, 0xae, 0x23, // char
    0x7d, 0xce, 0x4b, 0xeb, // char
];

fn main() {
    let mut buffer = CONFIG_DATA;

    let mut config: Configuration = unsafe { mem::zeroed() };

    let config_size = mem::size_of::<Configuration>();
    unsafe {
        let config_slice = slice::from_raw_parts_mut(&mut config as *mut _ as *mut u8, config_size);
        // `read_exact()` comes from `Read` impl for `&[u8]`
        buffer.read_exact(config_slice).unwrap();
    }

    println!("Read structure: {:#?}", config);
}

(针对 Rust 1.38 进行了更新)

但是,您需要小心,因为不安全的代码确实不安全。之后slice::from_raw_parts_mut()调用时,同一数据同时存在两个可变句柄,这违反了 Rust 别名规则。因此,您希望在尽可能短的时间内保留从结构中创建的可变切片。我还假设您了解字节顺序问题 - 上面的代码绝不是可移植的,并且如果在不同类型的机器(例如 ARM 与 x86)上编译和运行,将返回不同的结果。

如果您可以选择格式并且想要紧凑的二进制格式,请考虑使用bincode https://github.com/TyOverby/bincode。否则,如果您需要例如解析一些预定义的二进制结构,字节顺序 https://github.com/BurntSushi/byteorder/板条箱是要走的路。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 Rust 中的文件读取结构体? 的相关文章

随机推荐