mirror of
https://github.com/yewstack/yew.git
synced 2025-12-08 21:26:25 +00:00
Add generic type hints to boxed hooks (#3633)
When using complex type constructs in a boxed hook, the compiler could get confused and request a type hint on the call to the inner function. This adds all generic types of the outer hook function as explicit arguments to the call of the inner function. * Add generic types to boxed hooks * Create failing test --------- Co-authored-by: Michael Meyer <ichmed95@gmail.com>
This commit is contained in:
parent
95c29cc684
commit
e9739fc9ce
@ -144,11 +144,13 @@ pub fn hook_impl(hook: HookFn) -> syn::Result<TokenStream> {
|
||||
|
||||
let as_boxed_fn = with_output.then(|| quote! { as #boxed_fn_type });
|
||||
|
||||
let generic_types = generics.type_params().map(|t| &t.ident);
|
||||
|
||||
// We need boxing implementation for `impl Trait` arguments.
|
||||
quote! {
|
||||
let #boxed_inner_ident = ::std::boxed::Box::new(
|
||||
move |#ctx_ident: &mut ::yew::functional::HookContext| #inner_fn_rt {
|
||||
#inner_fn_ident (#ctx_ident, #(#input_args,)*)
|
||||
#inner_fn_ident :: <#(#generic_types,)*> (#ctx_ident, #(#input_args,)*)
|
||||
}
|
||||
) #as_boxed_fn;
|
||||
|
||||
|
||||
22
packages/yew-macro/tests/hook_attr/hook-trait-item-pass.rs
Normal file
22
packages/yew-macro/tests/hook_attr/hook-trait-item-pass.rs
Normal file
@ -0,0 +1,22 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
pub struct QueryState<T> {
|
||||
p: PhantomData<T>
|
||||
}
|
||||
|
||||
pub trait MyTrait {
|
||||
type Associated;
|
||||
}
|
||||
|
||||
|
||||
#[yew::hook]
|
||||
pub fn use_query_state<Props>(
|
||||
selector: impl yew::html::IntoPropValue<bool>,
|
||||
) -> QueryState<Props::Associated>
|
||||
where
|
||||
Props: MyTrait,
|
||||
{
|
||||
QueryState::<Props::Associated> { p: PhantomData::<Props::Associated> }
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
Loading…
x
Reference in New Issue
Block a user