From b59dfb5bf06323ee8bf7d2190d138b62f0cfa26c Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Wed, 14 Jun 2023 15:44:54 -0700 Subject: [PATCH] final tweak of proc macro --- optional_optional_user/src/lib.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/optional_optional_user/src/lib.rs b/optional_optional_user/src/lib.rs index e8d036b..40e0ceb 100644 --- a/optional_optional_user/src/lib.rs +++ b/optional_optional_user/src/lib.rs @@ -5,13 +5,13 @@ use syn::{parse_macro_input, ItemStruct}; #[proc_macro_derive(OptionalOptionalUser)] pub fn derive_optional_optional_user(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as ItemStruct); - + let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); let name = &input.ident; let has_user = input .fields .iter() .find(|f| { - if let Some(ident) = f.ident.clone() { + if let Some(ref ident) = f.ident { ident == "user" } else { false @@ -19,19 +19,25 @@ pub fn derive_optional_optional_user(input: TokenStream) -> TokenStream { }) .is_some(); - let user_is_option_user = if has_user { - quote!( - &&::std::any::TypeId::of::<::std::option::Option>() == self.user.type_id() + let (use_any, user_is_option_user) = if has_user { + ( + quote!( + use ::std::any::Any; + ), + quote!( + ::std::any::TypeId::of::<::std::option::Option>() + == self.user.type_id() + ), ) } else { - quote!() + (quote!(), quote!(false)) }; let output = quote!( - impl #name { - fn has_optional_user(&self) -> bool { - use ::std::any::Any; - #has_user #user_is_option_user + impl #impl_generics #name #ty_generics #where_clause { + pub fn has_optional_user(&self) -> bool { + #use_any + #user_is_option_user } } );