如何从Rust中的文件中读取结构? [英] How to read a struct from a file in Rust?

查看:932
本文介绍了如何从Rust中的文件中读取结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way I can read a structure directly from a file in Rust? My code is:

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 来自档案?这有可能吗?

How would I read my configuration directly into config from the file? Is this even possible?

推荐答案

注意:针对稳定的Rust(目前为1.10)进行了更新。

Note: updated for the stable Rust (1.10 as of now).

在这里:

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

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

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

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);
}

在这里试试

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

You need to be careful, however, as unsafe code is, well, unsafe. Here in particular after slice::from_raw_parts_mut() invocation there exist two mutable handles to the same data at the same time, which is a violation of Rust aliasing rules. Therefore you would want to keep the mutable slice created out of a structure for the shortest possible time. I also assume that you know about endianness issues - the code above is by no means portable, and will return different results if compiled and run on different kinds of machines (ARM vs x86, for example).

如果您可以选择格式并且想要一个紧凑的二进制格式,请考虑使用 bincode 。否则,如果您需要,例如要解析一些预先定义的二进制结构, byteorder crate是可行的方法。

If you can choose the format and you want a compact binary one, consider using bincode. Otherwise, if you need e.g. to parse some pre-defined binary structure, byteorder crate is the way to go.

这篇关于如何从Rust中的文件中读取结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆