I'm trying to filter out all the fields of a struct that are of type bool. But the syn::Type enum doesn't seem to have a case for it, or I'm reading the definitions incorrectly:
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 } I looked trough syn::Types source, to check which variants where ommited, but that didn't bring me any further. Here's what I have until now:
#[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!() } Am I going down the wrong path? Or is this simply not possible? Or am I missing something?
boolis. TheTyperefers to syntax rules (e.g. "this is a tuple, because it looks like a tuple"), but the proc-macro can't actually do type-resolution. So by definition, if I dotype Foo = bool;orstruct Foo(bool), the proc-macro will only ever see a type namedFoo. You could special-case for a type namedbooland hope that this will resolve tocore::primitive::bool.