@@ -62,6 +62,23 @@ fn derive_struct(input: &syn::DeriveInput) -> Result<proc_macro2::TokenStream, s
6262 "Lens implementations can only be derived from CamelCase types" ,
6363 ) ) ;
6464 } ;
65+ let ( impl_generics, ty_generics, where_clause) = input. generics . split_for_impl ( ) ;
66+
67+ let mut lens_ty_idents = Vec :: new ( ) ;
68+ let mut phantom_decls = Vec :: new ( ) ;
69+ let mut phantom_inits = Vec :: new ( ) ;
70+
71+ for gp in input. generics . params . iter ( ) {
72+ if let GenericParam :: Type ( TypeParam { ident, .. } ) = gp {
73+ lens_ty_idents. push ( quote ! { #ident} ) ;
74+ phantom_decls. push ( quote ! { std:: marker:: PhantomData <* const #ident>} ) ;
75+ phantom_inits. push ( quote ! { std:: marker:: PhantomData } ) ;
76+ }
77+ }
78+
79+ let lens_ty_generics = quote ! {
80+ <#( #lens_ty_idents) , * >
81+ } ;
6582
6683 // Define lens types for each field
6784 let defs = fields. iter ( ) . filter ( |f| !f. attrs . ignore ) . map ( |f| {
@@ -70,14 +87,20 @@ fn derive_struct(input: &syn::DeriveInput) -> Result<proc_macro2::TokenStream, s
7087 "Lens for the field `{}` on [`{1}`](super::{1})" ,
7188 field_name, ty
7289 ) ;
90+
7391 quote ! {
7492 #[ doc = #docs]
7593 #[ allow( non_camel_case_types) ]
7694 #[ derive( Debug , Copy , Clone ) ]
77- pub struct #field_name;
95+ pub struct #field_name#lens_ty_generics( #( #phantom_decls) , * ) ;
96+
97+ impl #lens_ty_generics #field_name#lens_ty_generics{
98+ pub const fn new( ) ->Self {
99+ Self ( #( #phantom_inits) , * )
100+ }
101+ }
78102 }
79103 } ) ;
80- let ( impl_generics, ty_generics, where_clause) = input. generics . split_for_impl ( ) ;
81104
82105 let used_params: HashSet < String > = input
83106 . generics
@@ -107,7 +130,8 @@ fn derive_struct(input: &syn::DeriveInput) -> Result<proc_macro2::TokenStream, s
107130 let field_ty = & f. ty ;
108131
109132 quote ! {
110- impl #impl_generics druid:: Lens <#ty#ty_generics, #field_ty> for #twizzled_name:: #field_name #where_clause {
133+
134+ impl #impl_generics druid:: Lens <#ty#ty_generics, #field_ty> for #twizzled_name:: #field_name#lens_ty_generics #where_clause {
111135 fn with<#val_ty_par, #func_ty_par: FnOnce ( & #field_ty) -> #val_ty_par>( & self , data: & #ty#ty_generics, f: #func_ty_par) -> #val_ty_par {
112136 f( & data. #field_name)
113137 }
@@ -125,7 +149,7 @@ fn derive_struct(input: &syn::DeriveInput) -> Result<proc_macro2::TokenStream, s
125149
126150 quote ! {
127151 /// Lens for the corresponding field
128- pub const #lens_field_name: #twizzled_name:: #field_name = #twizzled_name:: #field_name;
152+ pub const #lens_field_name: #twizzled_name:: #field_name#lens_ty_generics = #twizzled_name:: #field_name:: new ( ) ;
129153 }
130154 } ) ;
131155
0 commit comments