0

我想在编译时(不是运行时)从文件加载的 JSON 数据创建一个静态实例(类似于 constexpr):

例子:

// a file in the project /path/to/project/data.json
{
  "field1" : "a"
}
// project code:

use serde_json::json;

struct MyStruct {
    field1: String
}


// this is what I have, created at runtime
let json = json!(include!("data.json"));
let instance = serde_json::from_value::<MyStruct>(json);

// what I want:
// compile checked and created "statically"
let compiled_instance : MyStruct = static_instance_from_file!("data.json"); // how can this be verified by the compiler?
4

1 回答 1

0

您可以使用在项目build.rs中生成 Rust 源代码OUT_DIR并将include!其放入项目中。

于 2021-08-10T19:30:27.510 回答