我正在尝试过滤掉 struct 类型的所有字段bool
。但是syn::Type
枚举似乎没有案例,或者我错误地阅读了定义:
pub enum Type {
Array(TypeArray),
BareFn(TypeBareFn),
Group(TypeGroup),
ImplTrait(TypeImplTrait),
Infer(TypeInfer),
Macro(TypeMacro),
Never(TypeNever),
Paren(TypeParen),
Path(TypePath),
Ptr(TypePtr),
Reference(TypeReference),
Slice(TypeSlice),
TraitObject(TypeTraitObject),
Tuple(TypeTuple),
Verbatim(TokenStream),
// some variants omitted
}
我查看syn::Types
了源代码,以检查省略了哪些变体,但这并没有让我更进一步。这是我到目前为止所拥有的:
#[proc_macro_derive(Creator)]
pub fn derive_creator(_item: TokenStream) -> TokenStream {
let item = parse_macro_input!(_item as syn::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 Creator only on a struct!")
};
let bool_fields = fields.iter().filter(|field|
match field.ty {
// case when field type is bool => true
_ => false
}
);
unimplemented!()
}
我走错路了吗?或者这根本不可能?还是我错过了什么?