From e7e21fa2f02c58dd852fd28fccea152309fd3f6c Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Fri, 23 Dec 2022 17:47:57 -0500 Subject: [PATCH] fix: errors when using render macro --- examples/readme.rs | 4 ++-- packages/core-macro/src/lib.rs | 6 ++--- packages/rsx/src/lib.rs | 41 ++++++++++++++++++---------------- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/examples/readme.rs b/examples/readme.rs index f4668488b..8815e1f17 100644 --- a/examples/readme.rs +++ b/examples/readme.rs @@ -11,9 +11,9 @@ fn main() { fn app(cx: Scope) -> Element { let mut count = use_state(cx, || 0); - cx.render(rsx! { + render! { h1 { "High-Five counter: {count}" } button { onclick: move |_| count += 1, "Up high!" } button { onclick: move |_| count -= 1, "Down low!" } - }) + } } diff --git a/packages/core-macro/src/lib.rs b/packages/core-macro/src/lib.rs index 4e4f5600a..501c38e7b 100644 --- a/packages/core-macro/src/lib.rs +++ b/packages/core-macro/src/lib.rs @@ -1,5 +1,6 @@ use proc_macro::TokenStream; use quote::ToTokens; +use rsx::RenderCallBody; use syn::parse_macro_input; mod inlineprops; @@ -41,10 +42,7 @@ pub fn rsx(s: TokenStream) -> TokenStream { pub fn render(s: TokenStream) -> TokenStream { match syn::parse::(s) { Err(err) => err.to_compile_error().into(), - Ok(mut body) => { - body.inline_cx = true; - body.into_token_stream().into() - } + Ok(body) => RenderCallBody(body).into_token_stream().into(), } } diff --git a/packages/rsx/src/lib.rs b/packages/rsx/src/lib.rs index 0f344b1fe..5caebdbef 100644 --- a/packages/rsx/src/lib.rs +++ b/packages/rsx/src/lib.rs @@ -37,9 +37,6 @@ use syn::{ #[derive(Default, Debug)] pub struct CallBody { pub roots: Vec, - - // set this after - pub inline_cx: bool, } impl Parse for CallBody { @@ -56,10 +53,7 @@ impl Parse for CallBody { roots.push(node); } - Ok(Self { - roots, - inline_cx: false, - }) + Ok(Self { roots }) } } @@ -68,20 +62,29 @@ impl ToTokens for CallBody { fn to_tokens(&self, out_tokens: &mut TokenStream2) { let body = TemplateRenderer { roots: &self.roots }; - if self.inline_cx { - out_tokens.append_all(quote! { - Some({ - let __cx = cx; - #body - }) + out_tokens.append_all(quote! { + ::dioxus::core::LazyNodes::new( move | __cx: &::dioxus::core::ScopeState| -> ::dioxus::core::VNode { + #body }) - } else { - out_tokens.append_all(quote! { - ::dioxus::core::LazyNodes::new( move | __cx: &::dioxus::core::ScopeState| -> ::dioxus::core::VNode { - #body - }) + }) + } +} + +#[derive(Default, Debug)] +pub struct RenderCallBody(pub CallBody); + +impl ToTokens for RenderCallBody { + fn to_tokens(&self, out_tokens: &mut TokenStream2) { + let body = TemplateRenderer { + roots: &self.0.roots, + }; + + out_tokens.append_all(quote! { + Some({ + let __cx = cx; + #body }) - } + }) } }