DeriveInput {
attrs: [],
vis: Public(
VisPublic {
pub_token: Pub,
},
),
ident: Ident {
ident: "Command",
span: #0 bytes(62..69),
},
generics: Generics {
lt_token: None,
params: [],
gt_token: None,
where_clause: None,
},
data: Struct(
DataStruct {
struct_token: Struct,
fields: Named(
FieldsNamed {
brace_token: Brace,
named: [
Field {
attrs: [],
vis: Inherited,
ident: Some(
Ident {
ident: "executable",
span: #0 bytes(76..86),
},
),
colon_token: Some(
Colon,
),
ty: Path(
TypePath {
qself: None,
path: Path {
leading_colon: None,
segments: [
PathSegment {
ident: Ident {
ident: "String",
span: #0 bytes(88..94),
},
arguments: None,
},
],
},
},
),
},
Comma,
Field {
attrs: [],
vis: Inherited,
ident: Some(
Ident {
ident: "args",
span: #0 bytes(100..104),
},
),
colon_token: Some(
Colon,
),
ty: Path(
TypePath {
qself: None,
path: Path {
leading_colon: None,
segments: [
PathSegment {
ident: Ident {
ident: "Vec",
span: #0 bytes(106..109),
},
arguments: AngleBracketed(
AngleBracketedGenericArguments {
colon2_token: None,
lt_token: Lt,
args: [
Type(
Path(
TypePath {
qself: None,
path: Path {
leading_colon: None,
segments: [
PathSegment {
ident: Ident {
ident: "String",
span: #0 bytes(110..116),
},
arguments: None,
},
],
},
},
),
),
],
gt_token: Gt,
},
),
},
],
},
},
),
},
Comma,
Field {
attrs: [],
vis: Inherited,
ident: Some(
Ident {
ident: "env",
span: #0 bytes(123..126),
},
),
colon_token: Some(
Colon,
),
ty: Path(
TypePath {
qself: None,
path: Path {
leading_colon: None,
segments: [
PathSegment {
ident: Ident {
ident: "Vec",
span: #0 bytes(128..131),
},
arguments: AngleBracketed(
AngleBracketedGenericArguments {
colon2_token: None,
lt_token: Lt,
args: [
Type(
Path(
TypePath {
qself: None,
path: Path {
leading_colon: None,
segments: [
PathSegment {
ident: Ident {
ident: "String",
span: #0 bytes(132..138),
},
arguments: None,
},
],
},
},
),
),
],
gt_token: Gt,
},
),
},
],
},
},
),
},
Comma,
Field {
attrs: [],
vis: Inherited,
ident: Some(
Ident {
ident: "current_dir",
span: #0 bytes(145..156),
},
),
colon_token: Some(
Colon,
),
ty: Path(
TypePath {
qself: None,
path: Path {
leading_colon: None,
segments: [
PathSegment {
ident: Ident {
ident: "String",
span: #0 bytes(158..164),
},
arguments: None,
},
],
},
},
),
},
Comma,
],
},
),
semi_token: None,
},
),
}
属性式过程宏
参考资料: https://blog.ideawand.com/2021/02/27/rust_procedural_macro/rust_proc_marco_workshop_guide-01/
初始化项目
cargo new my_demo --bin cd my_demo cargo new proc_macro_first --libmy_demo/proc_macro_first/Cargo.toml, 添加:my_demo/proc_macro_first/src/lib.rs, 添加:my_demo/Cargo.toml, 添加:my_demo/src/main.rs, 添加:执行`cargo check`
* 使用
syn将TokenStream转换成更高级的TokenStream,quoto!可以将高级的TokenStream转换回函数支持的返回值类型的TokenStream进行返回my_demo/proc_macro_first/Cargo.toml, 添加:my_demo/proc_macro_first/src/lib.rs, 添加:执行`cargo check`
派生式过程宏
derive参考资料
* 第一关
my_demo/proc_macro_first/src/lib.rs, 写入:my_demo/src/main.rs, 写入:执行`cargo check`
* 其他关卡,完整代码
my_demo/proc_macro_first/Cargo.toml, 添加:my_demo/proc_macro_first/src/lib.rs, 更改为:# cargo install cargo-expand cargo expand生成结果打印如下: