Skip to content

Commit c125681

Browse files
committed
attempt
1 parent 1ef5f54 commit c125681

File tree

1 file changed

+29
-21
lines changed
  • packages/yew-macro/src/hook

1 file changed

+29
-21
lines changed

packages/yew-macro/src/hook/mod.rs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -146,38 +146,46 @@ pub fn hook_impl(hook: HookFn) -> syn::Result<TokenStream> {
146146
let inner_fn_ident = Ident::new("inner_fn", Span::mixed_site());
147147
let input_args = hook_sig.input_args();
148148

149-
// there might be some overridden lifetimes in the return type.
150149
let inner_fn_rt = match &sig.output {
151150
ReturnType::Default => None,
152151
ReturnType::Type(rarrow, _) => Some(quote! { #rarrow #output_type }),
153152
};
154153

155-
let inner_fn = quote! { fn #inner_fn_ident #generics (#ctx_ident: &mut ::yew::functional::HookContext, #inputs) #inner_fn_rt #where_clause #block };
154+
let output_is_impl_trait = matches!(hook_sig.output_type, Type::ImplTrait(_));
156155

157-
let inner_type_impl = if hook_sig.needs_boxing {
158-
let with_output = !matches!(hook_sig.output_type, Type::ImplTrait(_),);
159-
let inner_fn_rt = with_output.then_some(&inner_fn_rt);
160-
let output_type = with_output.then_some(&output_type);
156+
let inner_fn = if output_is_impl_trait {
157+
quote! {}
158+
} else {
159+
quote! { fn #inner_fn_ident #generics (#ctx_ident: &mut ::yew::functional::HookContext, #inputs) #inner_fn_rt #where_clause #block }
160+
};
161161

162+
let inner_type_impl = if hook_sig.needs_boxing {
162163
let hook_lifetime = &hook_sig.hook_lifetime;
163-
let hook_lifetime_plus = quote! { #hook_lifetime + };
164-
165164
let boxed_inner_ident = Ident::new("boxed_inner", Span::mixed_site());
166-
let boxed_fn_type = quote! { ::std::boxed::Box<dyn #hook_lifetime_plus ::std::ops::FnOnce(&mut ::yew::functional::HookContext) #inner_fn_rt> };
167-
168-
let as_boxed_fn = with_output.then(|| quote! { as #boxed_fn_type });
169165

170-
let generic_types = generics.type_params().map(|t| &t.ident);
166+
if output_is_impl_trait {
167+
quote! {
168+
let #boxed_inner_ident = ::std::boxed::Box::new(
169+
move |#ctx_ident: &mut ::yew::functional::HookContext| #block
170+
);
171171

172-
// We need boxing implementation for `impl Trait` arguments.
173-
quote! {
174-
let #boxed_inner_ident = ::std::boxed::Box::new(
175-
move |#ctx_ident: &mut ::yew::functional::HookContext| #inner_fn_rt {
176-
#inner_fn_ident :: <#(#generic_types,)*> (#ctx_ident, #(#input_args,)*)
177-
}
178-
) #as_boxed_fn;
179-
180-
::yew::functional::BoxedHook::<#hook_lifetime, #output_type>::new(#boxed_inner_ident)
172+
::yew::functional::BoxedHook::<#hook_lifetime,>::new(#boxed_inner_ident)
173+
}
174+
} else {
175+
let hook_lifetime_plus = quote! { #hook_lifetime + };
176+
let boxed_fn_type = quote! { ::std::boxed::Box<dyn #hook_lifetime_plus ::std::ops::FnOnce(&mut ::yew::functional::HookContext) #inner_fn_rt> };
177+
let as_boxed_fn = quote! { as #boxed_fn_type };
178+
let generic_types = generics.type_params().map(|t| &t.ident);
179+
180+
quote! {
181+
let #boxed_inner_ident = ::std::boxed::Box::new(
182+
move |#ctx_ident: &mut ::yew::functional::HookContext| #inner_fn_rt {
183+
#inner_fn_ident :: <#(#generic_types,)*> (#ctx_ident, #(#input_args,)*)
184+
}
185+
) #as_boxed_fn;
186+
187+
::yew::functional::BoxedHook::<#hook_lifetime, #output_type>::new(#boxed_inner_ident)
188+
}
181189
}
182190
} else {
183191
let input_types = hook_sig.input_types();

0 commit comments

Comments
 (0)