0

我正在创建我的第一个派生宏。我正在尝试从结构中过滤所有非可选字段。我想将它们用于构造函数。我不确定如何提取它们。我正在考虑获取类型的路径,并检查该路径是否等于std::option::Option. 这并没有真正奏效。谁能告诉我我做错了什么,或者我应该如何继续?

这一切都符合let non_optional_field_arguments = ...

// This function is not really interesting for this question
fn to_argument(field: &syn::Field) -> proc_macro2::TokenStream{
    let field_name = &field.ident;
    let field_type = &field.ty;
    quote! {
        #field_name : #field_type
    }
}

#[proc_macro_derive(Creator)]
pub fn derive_creator(_item: TokenStream) -> TokenStream {
    let item = parse_macro_input!(_item as DeriveInput);
    let item_ident = item.ident;

    let fields = if let syn::Data::Struct(syn::DataStruct {
        fields: syn::Fields::Named(syn::FieldsNamed {ref named, ..}),
        ..
    }) = item.data {
        named
    } else {
        panic!("You can derive only on struct!")
    };

    let field_arguments = fields.iter().map(to_argument);

    let non_optional_field_arguments = fields.iter().filter(|field| {
        return if let syn::Field{ty: syn::Type::Path(ref path), ..}= field{
            // I want to work with this, but the debug trait is not implemented
            let segments = &path.path.segments; 
            true
        } else {
            false
        }
    }).map(to_argument);
    unimplemented!()
}
4

0 回答 0