Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 无法使用syn/darling获取字段属性_Rust_Rust Proc Macros - Fatal编程技术网

Rust 无法使用syn/darling获取字段属性

Rust 无法使用syn/darling获取字段属性,rust,rust-proc-macros,Rust,Rust Proc Macros,我正在尝试使用syn/darling板条箱在proc宏中获取字段属性。这是一个MRE 货舱 src/lib.rs src/cat.rs 如果cat.rs的第11行(我用注释标记了它)将被注释, 程序将无错误地编译 这是。 据我所知,这方面存在一些问题 但我不知道如何解决这个问题 如何在我的StructField::attrs上设置字段attrs?如中所述,您将错过StructField的“forward\u attrs” 例如: #[derive(Clone, Debug, FromField)

我正在尝试使用syn/darling板条箱在proc宏中获取字段属性。这是一个MRE

货舱

src/lib.rs

src/cat.rs

如果cat.rs的第11行(我用注释标记了它)将被注释, 程序将无错误地编译

这是。 据我所知,这方面存在一些问题 但我不知道如何解决这个问题


如何在我的StructField::attrs上设置字段attrs?

如中所述,您将错过StructField的“forward\u 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>,
}
#[派生(克隆、调试、FromField)]
#[达林(前锋)]
结构域{
标识:选项,
ty:syn::Type,
vis:syn::可见性,
//如果这行会被评论,一切都会好的
属性:Vec,
}
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)
}
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.
#[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>,
}