@@ -40,7 +40,7 @@ struct StructDescForDeserialize<Attrs, Field> {
4040 attrs : Attrs ,
4141 fields : Vec < Field > ,
4242 constraint_trait : syn:: Path ,
43- constraint_lifetime : syn:: Lifetime ,
43+ constraint_lifetimes : ( syn:: Lifetime , syn :: Lifetime ) ,
4444
4545 generics : syn:: Generics ,
4646}
@@ -70,14 +70,14 @@ where
7070 . map ( Field :: from_field)
7171 . collect :: < Result < _ , _ > > ( ) ?;
7272
73- let constraint_lifetime = generate_unique_lifetime_for_impl ( & input. generics ) ;
73+ let constraint_lifetimes = generate_pair_of_unique_lifetimes_for_impl ( & input. generics ) ;
7474
7575 Ok ( Self {
7676 name : input. ident . clone ( ) ,
7777 attrs,
7878 fields,
7979 constraint_trait,
80- constraint_lifetime ,
80+ constraint_lifetimes ,
8181 generics : input. generics . clone ( ) ,
8282 } )
8383 }
8686 & self . attrs
8787 }
8888
89- fn constraint_lifetime ( & self ) -> & syn:: Lifetime {
90- & self . constraint_lifetime
89+ fn constraint_lifetimes ( & self ) -> & ( syn:: Lifetime , syn :: Lifetime ) {
90+ & self . constraint_lifetimes
9191 }
9292
9393 fn fields ( & self ) -> & [ Field ] {
9999 trait_ : syn:: Path ,
100100 items : impl IntoIterator < Item = syn:: ImplItem > ,
101101 ) -> syn:: ItemImpl {
102- let constraint_lifetime = & self . constraint_lifetime ;
102+ let ( constraint_lifetime, _ ) = self . constraint_lifetimes ( ) ;
103103 let ( _, ty_generics, _) = self . generics . split_for_impl ( ) ;
104104 let impl_generics = & self . generics . params ;
105105
@@ -108,7 +108,7 @@ where
108108 let predicates = generate_lifetime_constraints_for_impl (
109109 & self . generics ,
110110 self . constraint_trait . clone ( ) ,
111- & self . constraint_lifetime ,
111+ constraint_lifetime,
112112 )
113113 . chain ( generate_default_constraints ( & self . fields ) ) ;
114114 let trait_: syn:: Path = parse_quote ! ( #macro_internal:: #trait_) ;
@@ -168,16 +168,31 @@ fn generate_lifetime_constraints_for_impl<'a>(
168168 lifetime_constraints. chain ( type_constraints)
169169}
170170
171- /// Generates a new lifetime parameter , with a different name to any of the
171+ /// Generates a pair of new lifetime parameters , with a different name to any of the
172172/// existing generic lifetimes.
173- fn generate_unique_lifetime_for_impl ( generics : & syn:: Generics ) -> syn:: Lifetime {
173+ fn generate_pair_of_unique_lifetimes_for_impl (
174+ generics : & syn:: Generics ,
175+ ) -> ( syn:: Lifetime , syn:: Lifetime ) {
174176 let mut constraint_lifetime_name = "'lifetime" . to_string ( ) ;
175- while generics
176- . lifetimes ( )
177- . any ( |l| l. lifetime . to_string ( ) == constraint_lifetime_name)
178- {
177+ fn lifetime_occupied ( generics : & syn:: Generics , lifetime_name : & str ) -> bool {
178+ generics
179+ . lifetimes ( )
180+ . any ( |l| l. lifetime . to_string ( ) == lifetime_name)
181+ }
182+
183+ while lifetime_occupied ( generics, & constraint_lifetime_name) {
184+ // Extend the lifetime name with another underscore.
185+ constraint_lifetime_name += "_" ;
186+ }
187+
188+ let lifetime1 = syn:: Lifetime :: new ( & constraint_lifetime_name, Span :: call_site ( ) ) ;
189+ constraint_lifetime_name += "_" ;
190+
191+ while lifetime_occupied ( generics, & constraint_lifetime_name) {
179192 // Extend the lifetime name with another underscore.
180193 constraint_lifetime_name += "_" ;
181194 }
182- syn:: Lifetime :: new ( & constraint_lifetime_name, Span :: call_site ( ) )
195+ let lifetime2 = syn:: Lifetime :: new ( & constraint_lifetime_name, Span :: call_site ( ) ) ;
196+
197+ ( lifetime1, lifetime2)
183198}
0 commit comments