From d28bd2fdef85a0b5d3426ac85b4718f357850f8a Mon Sep 17 00:00:00 2001 From: Leo dev Date: Mon, 23 Dec 2024 12:26:32 +0100 Subject: [PATCH] some fixes and changes --- Cargo.toml | 6 +++++- lib.rs | 38 +++++++++++++++++++------------------- main.rs | 14 ++++++++++++++ 3 files changed, 38 insertions(+), 20 deletions(-) create mode 100644 main.rs diff --git a/Cargo.toml b/Cargo.toml index 22e738e..dbda4ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "osui-element" -version = "0.1.21" +version = "0.1.22" edition = "2021" description = "The element attribute for defining elements in OSUI" license = "Apache-2.0" @@ -9,6 +9,10 @@ license = "Apache-2.0" proc-macro = true path = "./lib.rs" +[[bin]] +path = "./main.rs" +name = "osui-element" + [dependencies] proc-macro2 = "1.0.89" quote = "1.0" diff --git a/lib.rs b/lib.rs index e84b29b..14ff449 100644 --- a/lib.rs +++ b/lib.rs @@ -1,9 +1,8 @@ use proc_macro::TokenStream; -use proc_macro2::Span; use quote::quote; use syn::{ - parse::Parser, parse_macro_input, DeriveInput, FnArg, ItemFn, Lifetime, LifetimeParam, - PatIdent, PatType, Type, TypeReference, + parse::Parser, parse_macro_input, DeriveInput, FnArg, GenericParam, ItemFn, Lifetime, + LifetimeParam, PatIdent, PatType, }; #[proc_macro_attribute] @@ -101,7 +100,20 @@ pub fn component(_args: TokenStream, input: TokenStream) -> TokenStream { syn::ReturnType::Default => syn::parse_quote! { Element }, syn::ReturnType::Type(_, t) => t, }; - let mut fn_lifetime = false; + + let lifetimes: Vec<_> = input_fn + .sig + .generics + .params + .iter() + .filter_map(|param| { + if let GenericParam::Lifetime(lifetime) = param { + Some(&lifetime.lifetime) + } else { + None + } + }) + .collect(); let mut struct_fields = Vec::new(); @@ -109,32 +121,20 @@ pub fn component(_args: TokenStream, input: TokenStream) -> TokenStream { if let FnArg::Typed(PatType { pat, ty, .. }) = arg { if let syn::Pat::Ident(PatIdent { ident, .. }) = &**pat { let field_name = ident.clone(); - let mut field_type = *(ty.clone()); - - if let Type::Reference(TypeReference { lifetime, .. }) = &mut field_type { - fn_lifetime = true; - *lifetime = Some(Lifetime::new("'a", Span::call_site())); - } - + let field_type = ty.clone(); struct_fields.push(quote! { pub #field_name: #field_type }); } } } - let lifetime = if fn_lifetime { - quote! {<'a>} - } else { - quote! {} - }; - let expanded = quote! { #[derive(Debug, Default)] #[allow(non_camel_case_types)] - #visibility struct #fn_name #lifetime { + #visibility struct #fn_name<#(#lifetimes),*> { #(#struct_fields),* } - impl #lifetime Component for #lifetime #fn_name #lifetime { + impl <#(#lifetimes),*> Component for #fn_name <#(#lifetimes),*> { fn create_element(self) -> #return_type { #code } diff --git a/main.rs b/main.rs new file mode 100644 index 0000000..061fdff --- /dev/null +++ b/main.rs @@ -0,0 +1,14 @@ +use osui_element::component; + +pub type Element = i32; + +pub trait Component { + fn create_element(self) -> Element; +} + +#[component] +pub fn MyComponent<'a>(age: &'a str) { + 0 +} + +fn main() {}