[yew-devel] [PATCH yew-widget-toolkit 2/3] pwt-macros: builder: allow multiple generic types in callback variant
Dominik Csapak
d.csapak at proxmox.com
Wed Dec 10 11:48:49 CET 2025
if there is a callback type with multiple generic variants, e.g.
Foo<A,B> we currently can't use it since we only expect one type
in the macro.
Extend the parser to add multiple types for the generic part.
So we can now write for example:
#[builder_cb(IntoEventCallback, into_event_callback, A, B)]
foo: Callback<A, B>,
This way it's compatible with the current way we write it.
Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
pwt-macros/src/builder.rs | 19 +++++++++++++++----
1 file changed, 15 insertions(+), 4 deletions(-)
diff --git a/pwt-macros/src/builder.rs b/pwt-macros/src/builder.rs
index 89f791e..ee28bbf 100644
--- a/pwt-macros/src/builder.rs
+++ b/pwt-macros/src/builder.rs
@@ -85,7 +85,7 @@ impl Parse for FieldOptions {
struct CallbackOptions {
into_trait: syn::Type,
into_fn: syn::Ident,
- inner_type: syn::Type,
+ inner_types: Vec<syn::Type>,
}
impl Parse for CallbackOptions {
@@ -105,16 +105,27 @@ impl Parse for CallbackOptions {
.parse()
.map_err(|err| Error::new(err.span(), format!("expected into function: {err}")))?;
+ let mut inner_types = Vec::new();
+
parse_comma(&content).map_err(|err| Error::new(err.span(), "missing inner type"))?;
let inner_type: syn::Type = content.parse().map_err(|err| {
Error::new(err.span(), format!("expected inner callback type: {err}"))
})?;
+ inner_types.push(inner_type);
+
+ while parse_comma(&content).is_ok() {
+ let inner_type: syn::Type = content.parse().map_err(|err| {
+ Error::new(err.span(), format!("expected inner callback type: {err}"))
+ })?;
+ inner_types.push(inner_type);
+ }
+
Ok(Self {
into_trait,
into_fn,
- inner_type,
+ inner_types,
})
}
}
@@ -229,9 +240,9 @@ fn derive_builder(builder: DeriveInput) -> Result<proc_macro2::TokenStream> {
let options = syn::parse2::<CallbackOptions>(tokens)?;
let into_fn = options.into_fn;
let into_trait = options.into_trait;
- let inner_type = options.inner_type;
+ let inner_types = options.inner_types;
(
- quote_spanned! { attr_span => impl #into_trait<#inner_type>},
+ quote_spanned! { attr_span => impl #into_trait<#(#inner_types),*>},
quote_spanned! { attr_span => #field_ident.#into_fn()},
)
}
--
2.47.3
More information about the yew-devel
mailing list