final tweak of proc macro
This commit is contained in:
parent
f456e07559
commit
b59dfb5bf0
1 changed files with 16 additions and 10 deletions
|
@ -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 {
|
||||
let (use_any, user_is_option_user) = if has_user {
|
||||
(
|
||||
quote!(
|
||||
&&::std::any::TypeId::of::<::std::option::Option<crate::User>>() == self.user.type_id()
|
||||
use ::std::any::Any;
|
||||
),
|
||||
quote!(
|
||||
::std::any::TypeId::of::<::std::option::Option<crate::User>>()
|
||||
== 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
|
||||
}
|
||||
}
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue