3

我正在尝试使用 syn/darling crates 在 proc-macro 中获取字段属性。这是一个 MRE

货运.toml

[package]
name = "darling_attrs"
version = "0.1.0"
edition = "2018"

[lib]
proc-macro = true

[dependencies]
darling = "0.10"
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "1.0", features = ["full"] }

src/lib.rs

extern crate proc_macro;
extern crate proc_macro2;

mod cat;

use proc_macro::TokenStream;
use syn::DeriveInput;

#[proc_macro_derive(Cat)]
pub fn derive_cat(input: TokenStream) -> TokenStream {
    let input = syn::parse_macro_input!(input as DeriveInput);
    cat::expand_derive_cat(&input)
}

src/cat.rs

use darling::{FromField, FromDeriveInput};
use proc_macro::TokenStream;
use quote::{ToTokens, quote};

#[derive(Clone, Debug, FromField)]
struct StructField {
    ident: Option<syn::Ident>,
    ty: syn::Type,
    vis: syn::Visibility,
    // If the line would be commented, all will be fine
    attrs: Vec<syn::Attribute>,
}

#[derive(Debug, FromDeriveInput)]
#[darling(supports(struct_named))]
struct Cat {
    ident: syn::Ident,
    data: darling::ast::Data<(), StructField>,
}

impl ToTokens for Cat {
    fn to_tokens(&self, out: &mut proc_macro2::TokenStream) {
        let tokens = quote!{};

        let fields = self.data.clone().take_struct().unwrap();

        //dbg!(fields);

        out.extend(tokens)
    }
}

pub fn expand_derive_cat(input: &syn::DeriveInput) -> TokenStream {
    let cat = match Cat::from_derive_input(&input) {
        Ok(parsed) => parsed,
        Err(e) => return e.write_errors().into(),
    };

    let tokens = quote! { #cat };
    tokens.into()
}

但是错误被抛出

error[E0425]: cannot find value `__fwd_attrs` in this scope
 --> src/cat.rs:5:24
  |
5 | #[derive(Clone, Debug, FromField)]
  |                        ^^^^^^^^^ not found in this scope

error: aborting due to previous error

For more information about this error, try `rustc --explain E0425`.
error: could not compile `darling_attrs`.

To learn more, run the command again with --verbose.

如果 cat.rs 的第 11 行(我用注释标记它)将被注释,程序将编译而没有错误。

这是Vec 的 impl FromField。据我了解,此代码存在一些问题, 但我不知道如何解决。

如何在我的StructField::attrs 上有一个字段 attrs

4

1 回答 1

2

如crate 文档中所述,您错过了 struct 'StructField' 的 'forward_attrs' 。

例如:

#[derive(Clone, Debug, FromField)]
#[darling(forward_attrs(cfg)]
struct StructField {
    ident: Option<syn::Ident>,
    ty: syn::Type,
    vis: syn::Visibility,
    // If the line would be commented, all will be fine
    attrs: Vec<syn::Attribute>,
}
于 2021-03-24T17:54:50.013 回答