我正在创建一个在枚举上运行的自定义派生.我想生成像这样的代码
match *enum_instance {
EnumName::VariantName1 => "dummy",
EnumName::VariantName2 => "dummy",
EnumName::VariantName3 => "dummy",
}
Run Code Online (Sandbox Code Playgroud)
我已经能够使用这样的代码使用它:
let enum_name = &ast.ident;
let mut q = quote! {};
q.append_all(e.iter().map(|variant| {
let variant_name = &variant.ident;
quote! { #enum_name::#variant_name => "dummy", }
}));
quote! {
impl FullName for #name {
fn full_name(&self) -> &'static str {
match *self {
#q
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
要求临时变量并执行append_all感觉非常不优雅.有更清洁的解决方案吗?
SRC/main.rs
#[macro_use]
extern crate my_derive;
#[derive(FullName)]
enum Example {
One,
Two,
Three,
}
trait FullName {
fn full_name(&self) -> &'static str;
}
fn main() {
assert_eq!(Example::One.full_name(), "dummy");
}
Run Code Online (Sandbox Code Playgroud)
Cargo.toml
[package]
name = "example"
version = "0.0.0"
[dependencies]
my-derive = { path = "my-derive" }
Run Code Online (Sandbox Code Playgroud)
我-取得/ Cargo.toml
[package]
name = "my-derive"
version = "0.0.0"
[lib]
proc-macro = true
[dependencies]
quote = "0.3.12"
syn = "0.11.10"
Run Code Online (Sandbox Code Playgroud)
我-取得/ SRC/lib.rs
extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate quote;
use proc_macro::TokenStream;
#[proc_macro_derive(FullName)]
pub fn has_extent_derive(input: TokenStream) -> TokenStream {
let s = input.to_string();
let ast = syn::parse_macro_input(&s).expect("Unable to parse input");
let gen = impl_full_name(&ast);
gen.parse().expect("Unable to generate")
}
fn impl_full_name(ast: &syn::MacroInput) -> quote::Tokens {
use syn::Body;
let name = &ast.ident;
match ast.body {
Body::Enum(ref e) => {
let enum_name = &ast.ident;
let mut q = quote! {};
q.append_all(e.iter().map(|variant| {
let variant_name = &variant.ident;
quote! { #enum_name::#variant_name => "dummy", }
}));
quote! {
impl FullName for #name {
fn full_name(&self) -> &'static str {
match *self {
#q
}
}
}
}
}
_ => {
panic!("Only implemented for enums");
}
}
}
Run Code Online (Sandbox Code Playgroud)
当您有一个 tokens 迭代器时xs = [x1, x2, …, xN],您可以使用重复语法#( #xs & stuff );*将其扩展到宏x1 & stuff; x2 & stuff; …; xN & stuff内部quote!。
同样,您可以并行重复多个迭代器,例如#(#ks => #vs,)*将变为k1 => v1, k2 => v2, …, kN => vN,.
的重复语法quote类似于的锈自己的宏系统,只有改变$对#。理想情况下,您应该能够编写:
Body::Enum(ref e) => {
let enum_name = &ast.ident;
let variant_names = e.iter().map(|variant| &variant.ident);
quote! {
impl FullName for #name {
fn full_name(&self) -> &'static str {
match *self {
#(#enum_name::#variant_names => "dummy",)*
// ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,目前quote要求内部的每个变量#(…)*都是迭代器:编译上述内容将导致 E0599 method-not-found 错误。这是quotecrate 的一个已知错误。正如错误报告所解释的,这可以通过创建一个enum_name永远重复使用的迭代器来解决std::iter::repeat():
Body::Enum(ref e) => {
use std::iter::repeat;
let enum_names = repeat(&ast.ident);
// ^~~~~~~~~~~~~~~~~~
// creates the iterator [Example, Example, Example, Example, …]
let variant_names = e.iter().map(|variant| &variant.ident);
quote! {
impl FullName for #name {
fn full_name(&self) -> &'static str {
match *self {
#(#enum_names::#variant_names => "dummy",)*
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这会产生
impl FullName for Example {
fn full_name(&self) -> &'static str {
match *self {
Example::One => "dummy",
Example::Two => "dummy",
Example::Three => "dummy",
}
}
}
Run Code Online (Sandbox Code Playgroud)
在最终输出中。
| 归档时间: |
|
| 查看次数: |
479 次 |
| 最近记录: |