标签: rustc-serialize

如何在编译器插件中获取struct字段和字段类型?

我想生成一个HashMap使用struct字段作为键,并使用usize整数作为值.

pub struct Article {
    title: String,
    content: String,
    category: String,
    comments: Vec<Comment>
}

pub struct Comment {
    content: String
}
Run Code Online (Sandbox Code Playgroud)

我的预期输出是:

{
    title: 0,
    content: 1,
    category: 2
    comments[].content: 3
}
Run Code Online (Sandbox Code Playgroud)

我的解决办法是impl我的特质FieldsMapping两个ArticleComment:

pub trait FieldsMapping {
    fn get_fields_map(&self) -> HashMap<String, usize>;
}
Run Code Online (Sandbox Code Playgroud)

我想为自定义派生编写一个编译器插件FieldsMapping.

我的问题是:如何在编译器插件中获取所有字段?我怎么知道字段类型是Vec什么?

rust rust-compiler-plugin rustc-serialize

1
推荐指数
1
解决办法
138
查看次数