Crates.io | procmeta-core |
lib.rs | procmeta-core |
version | 0.3.5 |
source | src |
created_at | 2024-02-15 00:58:11.271271 |
updated_at | 2024-05-03 02:46:11.161538 |
description | proc-macro helper |
homepage | |
repository | |
max_upload_size | |
id | 1140470 |
size | 39,341 |
用于rust过程宏开发辅助工具包
用于生成获取改类型值的token stream,如:
#[test]
fn test_token_stream() {
#[derive(GetTokenStream)]
pub enum Ass {
S(Student),
}
#[derive(GetTokenStream)]
pub struct Student {
pub name: String,
pub age: i32,
}
let student = Student {
name: "lloyd".to_string(),
age: 18,
};
let a = Ass::S(student);
println!("{}", a.get_token_stream());
}
输出结果为: Ass :: S (Student { name : "lloyd" . into () , age : 18i32 , } ,)
用于解析宏辅助参数,将参数元数据解析为特定的枚举
#[test]
fn test_meta_parser() {
#[derive(MetaParser)]
enum Context {
#[name("note")]
Note(#[converter(LitStr)] String),
#[name("note")]
NoteNameSize { name: LitStr, size: LitInt },
#[name("note")]
NoteName { name: LitStr },
}
let input: ItemStruct = parse_quote! {
#[note("111")]
#[note(name = "222", size = 18)]
#[note(name = "333")]
pub struct MyStruct {
pub name: String,
}
};
let attrs = input.attrs;
for attr in attrs {
let context = Context::parse(&attr.meta);
if let Ok(context) = context {
match context {
Context::Note(name) => {
println!("{}", name);
}
Context::NoteNameSize { name, size } => {
println!(
"name = {}, size = {}",
name.value(),
size.base10_parse::<i64>().unwrap()
);
}
Context::NoteName { name } => {
println!("name = {}", name.value());
}
}
}
}
}