From 0b5b7f09dba636e59aa12bb542de4ab123e87e9b Mon Sep 17 00:00:00 2001 From: Muhannad Alrusayni Date: Fri, 6 May 2022 11:11:34 +0300 Subject: [PATCH] feat: Support generic component with `inline_props` with some limitation --- packages/core-macro/src/inlineprops.rs | 18 +++++++++++++++--- packages/core-macro/src/props/mod.rs | 4 +++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/core-macro/src/inlineprops.rs b/packages/core-macro/src/inlineprops.rs index 782d13a48..05d9d5240 100644 --- a/packages/core-macro/src/inlineprops.rs +++ b/packages/core-macro/src/inlineprops.rs @@ -17,6 +17,7 @@ pub struct InlinePropsBody { pub inputs: Punctuated, // pub fields: FieldsNamed, pub output: ReturnType, + pub where_clause: Option, pub block: Box, } @@ -28,7 +29,7 @@ impl Parse for InlinePropsBody { let fn_token = input.parse()?; let ident = input.parse()?; - let generics = input.parse()?; + let generics: Generics = input.parse()?; let content; let paren_token = syn::parenthesized!(content in input); @@ -47,6 +48,11 @@ impl Parse for InlinePropsBody { let output = input.parse()?; + let where_clause = input + .peek(syn::token::Where) + .then(|| input.parse()) + .transpose()?; + let block = input.parse()?; Ok(Self { @@ -57,6 +63,7 @@ impl Parse for InlinePropsBody { paren_token, inputs, output, + where_clause, block, cx_token, attrs, @@ -73,6 +80,7 @@ impl ToTokens for InlinePropsBody { generics, inputs, output, + where_clause, block, cx_token, attrs, @@ -136,12 +144,16 @@ impl ToTokens for InlinePropsBody { out_tokens.append_all(quote! { #modifiers #[allow(non_camel_case_types)] - #vis struct #struct_name #struct_generics { + #vis struct #struct_name #struct_generics + #where_clause + { #(#fields),* } #(#attrs)* - #vis fn #ident #fn_generics (#cx_token: Scope<#scope_lifetime #struct_name #generics>) #output { + #vis fn #ident #fn_generics (#cx_token: Scope<#scope_lifetime #struct_name #generics>) #output + #where_clause + { let #struct_name { #(#field_names),* } = &cx.props; #block } diff --git a/packages/core-macro/src/props/mod.rs b/packages/core-macro/src/props/mod.rs index 6c65c5944..92772df00 100644 --- a/packages/core-macro/src/props/mod.rs +++ b/packages/core-macro/src/props/mod.rs @@ -653,7 +653,9 @@ Finally, call `.build()` to create the instance of `{name}`. } } - impl #impl_generics dioxus::prelude::Properties for #name #ty_generics{ + impl #impl_generics dioxus::prelude::Properties for #name #ty_generics + #b_generics_where_extras_predicates + { type Builder = #builder_name #generics_with_empty; const IS_STATIC: bool = #is_static; fn builder() -> Self::Builder {