macro_tools 0.85.0

Tools for writing procedural macroses.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
//!
//! Functions and structures to handle and manipulate generic parameters using the `syn` crate. It's designed to support macro-driven code generation by simplifying, merging, extracting, and decomposing `syn ::Generics`.
//!

// Sub-modules
pub mod classification;
pub mod filter;
pub mod combine;

/// Define a private namespace for all its items.
mod private
{
  use crate :: *;

  /// A `GenericsWithWhere` struct to handle the parsing of Rust generics with an explicit `where` clause.
  ///
  /// This wrapper addresses the limitation in the `syn` crate where parsing `Generics` directly from a `ParseStream`
  /// does not automatically handle associated `where` clauses. By integrating `where` clause parsing into the
  /// `GenericsWithWhere`, this struct provides a seamless way to capture both the generics and their constraints
  /// in scenarios where the `where` clause is crucial for type constraints and bounds in Rust macros and code generation.
  ///
  /// Usage :
  ///
  /// ```
  /// let parsed_generics: macro_tools ::generic_params ::GenericsWithWhere
  /// = syn ::parse_str( "< T: Clone, U: Default = Default1 > where T: Default" ).unwrap();
  /// assert!( parsed_generics.generics.params.len() == 2 );
  /// assert!( parsed_generics.generics.where_clause.is_some() );
  /// ```
  ///
  #[ derive( Debug ) ]
  pub struct GenericsWithWhere
  {
  /// Syn's generics parameters.
  pub generics: syn ::Generics,
 }

  impl GenericsWithWhere 
  {
  /// Unwraps the `GenericsWithWhere` to retrieve the inner `syn ::Generics`.
  #[ must_use ]
  pub fn unwrap(self) -> syn ::Generics
  {
   self.generics
 }

  /// Parses a string to a `GenericsWithWhere`, specifically designed to handle generics syntax with where clauses effectively.
  ///
  /// This function provides a convenient way to parse generic parameters and their associated
  /// `where` clauses from a string slice, returning a `GenericsWithWhere` instance.
  ///
  /// # Arguments
  ///
  /// * `s` - The string slice containing the generics and optional `where` clause (e.g., `"< T: Debug > where T: Default"`).
  ///
  /// # Returns
  ///
  /// Returns a `syn ::Result` which is `Ok(GenericsWithWhere)` on successful parsing,
  /// or `Err(syn ::Error)` if the input string does not conform to valid Rust generics syntax.
  ///
  /// # Errors
  ///
  /// Returns a `syn ::Error` if the input string `s` cannot be parsed as valid Rust generics
  /// or a `where` clause.
  ///
  /// # Examples
  ///
  /// ```rust
  /// use macro_tools ::generic_params ::GenericsWithWhere;
  ///
  /// let parsed = GenericsWithWhere ::parse_from_str( "< T: Clone, U: Default = Default1 > where T: Default" ).unwrap();
  /// assert!( parsed.generics.params.len() == 2 );
  /// assert!( parsed.generics.where_clause.is_some() );
  ///
  /// let parsed_no_where = GenericsWithWhere ::parse_from_str( "< T >" ).unwrap();
  /// assert!( parsed_no_where.generics.params.len() == 1 );
  /// assert!( parsed_no_where.generics.where_clause.is_none() );
  ///
  /// let parsed_only_where = GenericsWithWhere ::parse_from_str( "where T: Debug" ).unwrap();
  /// assert!( parsed_only_where.generics.params.is_empty() );
  /// assert!( parsed_only_where.generics.where_clause.is_some() );
  /// ```
  pub fn parse_from_str(s: &str) -> syn ::Result< GenericsWithWhere >
  {
   syn ::parse_str :: < GenericsWithWhere >(s)
 }
 }

  impl syn ::parse ::Parse for GenericsWithWhere 
  {
  fn parse(input: syn ::parse ::ParseStream< '_ >) -> syn ::Result< Self > 
  {
   let generics: syn ::Generics = input.parse()?;
   let where_clause: Option< syn ::WhereClause > = input.parse()?;

   let mut generics_clone = generics.clone();
   generics_clone.where_clause = where_clause;

   Ok(GenericsWithWhere {
  generics: generics_clone,
 })
 }
 }

  impl quote ::ToTokens for GenericsWithWhere 
  {
  fn to_tokens(&self, tokens: &mut proc_macro2 ::TokenStream) 
  {
   self.generics.to_tokens(tokens);
 }
 }

  impl From< GenericsWithWhere > for syn ::Generics 
  {
  fn from(g: GenericsWithWhere) -> Self 
  {
   g.generics
 }
 }

  impl From< syn ::Generics > for GenericsWithWhere 
  {
  fn from(generics: syn ::Generics) -> Self 
  {
   GenericsWithWhere { generics }
 }
 }

  /// A wrapper around a reference to `syn ::Generics` to provide convenient helper methods
  /// for generating token streams related to generic parameters.
  ///
  /// This is particularly useful in procedural macros for constructing parts of function
  /// signatures, type paths, and where clauses that involve generics.
  #[ derive( Debug, Clone, Copy ) ]
  pub struct GenericsRef< 'a >
  {
  syn_generics: &'a syn ::Generics,
 }

  impl< 'a > GenericsRef< 'a >
  {
  /// Creates a new `GenericsRef` from a reference to `syn ::Generics`.
  #[ must_use ]
  pub fn new_borrowed(syn_generics: &'a syn ::Generics) -> Self
  {
   Self { syn_generics }
 }

  /// Creates a new `GenericsRef` from a reference to `syn ::Generics`. Alias for `new_borrowed`.
  #[ must_use ]
  pub fn new(syn_generics: &'a syn ::Generics) -> Self
  {
   Self ::new_borrowed(syn_generics)
 }

  /// Returns the `impl_generics` part (e.g., `< T: Trait, 'b, const C: usize >`)
  /// as a `TokenStream` if generics are present, otherwise an empty `TokenStream`.
  ///
  /// This is suitable for use in `impl < #impl_generics > Struct ...` contexts.
  /// It includes bounds and lifetimes.
  #[ must_use ]
  pub fn impl_generics_tokens_if_any( &self ) -> proc_macro2 ::TokenStream
  {
   if self.syn_generics.params.is_empty() 
   {
  return quote ::quote! {};
 }
   let (impl_g, _, _) = self.syn_generics.split_for_impl();
   quote ::quote! { #impl_g }
 }

  /// Returns the `ty_generics` part (e.g., `< T, 'b, C >`) as a `TokenStream`
  /// if generics are present, otherwise an empty `TokenStream`.
  ///
  /// This is suitable for use in type paths like `Struct :: < #ty_generics >`.
  /// It includes only the identifiers of the generic parameters (types, lifetimes, consts).
  #[ must_use ]
  pub fn ty_generics_tokens_if_any( &self ) -> proc_macro2 ::TokenStream
  {
   if self.syn_generics.params.is_empty() 
   {
  return quote ::quote! {};
 }
   let (_, ty_g, _) = self.syn_generics.split_for_impl();
   quote ::quote! { #ty_g }
 }

  /// Returns the `where_clause` (e.g., `where T: Trait`) as a `TokenStream`
  /// if a where clause is present in the original generics, otherwise an empty `TokenStream`.
  #[ must_use ]
  pub fn where_clause_tokens_if_any( &self ) -> proc_macro2 ::TokenStream
  {
   let (_, _, where_clause) = self.syn_generics.split_for_impl();
   quote ::quote! { #where_clause }
 }

  /// Returns a token stream representing a path to a type, including its generic arguments
  /// if present (e.g., `MyType :: < T, U >`). If no generics are present, it returns
  /// just the `base_ident`.
  ///
  /// # Arguments
  ///
  /// * `base_ident` : The identifier of the base type (e.g., `MyType`).
  #[ must_use ]
  pub fn type_path_tokens_if_any(&self, base_ident: &syn ::Ident) -> proc_macro2 ::TokenStream
  {
   if self.syn_generics.params.is_empty() 
   {
  quote ::quote! { #base_ident }
 }
  else
  {
  let (_, ty_g, _) = self.syn_generics.split_for_impl();
  quote ::quote! { #base_ident #ty_g }
 }
  }

  /// Get classification of the generics.
  ///
  /// This method analyzes the generic parameters and returns a classification
  /// containing information about the types of parameters present.
  ///
  /// # Example
  ///
  /// ```
  /// use macro_tools ::generic_params :: { GenericsRef, classify_generics };
  /// use syn ::parse_quote;
  ///
  /// let generics: syn ::Generics = parse_quote! { < 'a, T, const N: usize > };
  /// let generics_ref = GenericsRef ::new(&generics);
  /// let classification = generics_ref.classification();
  ///
  /// assert!(classification.has_mixed);
  /// assert_eq!(classification.lifetimes.len(), 1);
  /// assert_eq!(classification.types.len(), 1);
  /// assert_eq!(classification.consts.len(), 1);
  /// ```
  #[ must_use ]
  pub fn classification( &self ) -> super ::classification ::GenericsClassification< 'a >
  {
   super ::classification ::classify_generics(self.syn_generics)
 }
  
  /// Get impl generics without lifetimes.
  ///
  /// This method returns the impl generics token stream with lifetime parameters filtered out,
  /// keeping only type and const parameters.
  ///
  /// # Example
  ///
  /// ```
  /// use macro_tools ::generic_params ::GenericsRef;
  /// use syn ::parse_quote;
  ///
  /// let generics: syn ::Generics = parse_quote! { < 'a, T: Clone, const N: usize > };
  /// let generics_ref = GenericsRef ::new(&generics);
  /// let impl_no_lifetimes = generics_ref.impl_generics_no_lifetimes();
  ///
  /// // Result will be: < T: Clone, const N: usize >
  /// ```
  #[ must_use ]
  pub fn impl_generics_no_lifetimes( &self ) -> proc_macro2 ::TokenStream
  {
   let filtered = super ::filter ::filter_params(&self.syn_generics.params, super ::filter ::filter_non_lifetimes);
   if filtered.is_empty() 
   {
  quote ::quote! {}
 } else {
  quote ::quote! { < #filtered > }
 }
 }
  
  /// Get type generics without lifetimes.
  ///
  /// This method returns the type generics token stream with lifetime parameters filtered out,
  /// keeping only type and const parameters (simplified for type usage).
  ///
  /// # Example
  ///
  /// ```
  /// use macro_tools ::generic_params ::GenericsRef;
  /// use syn ::parse_quote;
  ///
  /// let generics: syn ::Generics = parse_quote! { < 'a, T, const N: usize > };
  /// let generics_ref = GenericsRef ::new(&generics);
  /// let ty_no_lifetimes = generics_ref.ty_generics_no_lifetimes();
  ///
  /// // Result will be: < T, N >
  /// ```
  #[ must_use ]
  pub fn ty_generics_no_lifetimes( &self ) -> proc_macro2 ::TokenStream
  {
   let (_, _, ty_params, _) = crate::generic_params::decompose(self.syn_generics);
   let filtered = super ::filter ::filter_params(&ty_params, super ::filter ::filter_non_lifetimes);
   if filtered.is_empty() 
   {
  quote ::quote! {}
 } else {
  quote ::quote! { < #filtered > }
 }
 }
  
  /// Check if generics contain only lifetime parameters.
  ///
  /// # Example
  ///
  /// ```
  /// use macro_tools ::generic_params ::GenericsRef;
  /// use syn ::parse_quote;
  ///
  /// let generics: syn ::Generics = parse_quote! { < 'a, 'b > };
  /// let generics_ref = GenericsRef ::new(&generics);
  /// assert!(generics_ref.has_only_lifetimes());
  ///
  /// let generics2: syn ::Generics = parse_quote! { < 'a, T > };
  /// let generics_ref2 = GenericsRef ::new(&generics2);
  /// assert!(!generics_ref2.has_only_lifetimes());
  /// ```
  #[ must_use ]
  pub fn has_only_lifetimes( &self ) -> bool
  {
   self.classification().has_only_lifetimes
 }
  
  /// Check if generics contain only type parameters.
  ///
  /// # Example
  ///
  /// ```
  /// use macro_tools ::generic_params ::GenericsRef;
  /// use syn ::parse_quote;
  ///
  /// let generics: syn ::Generics = parse_quote! { < T, U > };
  /// let generics_ref = GenericsRef ::new(&generics);
  /// assert!(generics_ref.has_only_types());
  ///
  /// let generics2: syn ::Generics = parse_quote! { < T, const N: usize > };
  /// let generics_ref2 = GenericsRef ::new(&generics2);
  /// assert!(!generics_ref2.has_only_types());
  /// ```
  #[ must_use ]
  pub fn has_only_types( &self ) -> bool
  {
   self.classification().has_only_types
 }
  
  /// Check if generics contain only const parameters.
  ///
  /// # Example
  ///
  /// ```
  /// use macro_tools ::generic_params ::GenericsRef;
  /// use syn ::parse_quote;
  ///
  /// let generics: syn ::Generics = parse_quote! { < const N: usize, const M: i32 > };
  /// let generics_ref = GenericsRef ::new(&generics);
  /// assert!(generics_ref.has_only_consts());
  /// ```
  #[ must_use ]
  pub fn has_only_consts( &self ) -> bool
  {
   self.classification().has_only_consts
 }
  
  /// Get type path without lifetime parameters.
  ///
  /// This method returns a token stream representing a path to a type with
  /// lifetime parameters filtered out from the generic arguments.
  ///
  /// # Arguments
  ///
  /// * `base_ident` - The identifier of the base type
  ///
  /// # Example
  ///
  /// ```
  /// use macro_tools ::generic_params ::GenericsRef;
  /// use syn :: { parse_quote, Ident };
  /// use quote ::format_ident;
  ///
  /// let generics: syn ::Generics = parse_quote! { < 'a, T, const N: usize > };
  /// let generics_ref = GenericsRef ::new(&generics);
  /// let base = format_ident!("MyType");
  /// let path = generics_ref.type_path_no_lifetimes(&base);
  ///
  /// // Result will be: MyType :: < T, N >
  /// ```
  #[ must_use ]
  pub fn type_path_no_lifetimes(&self, base_ident: &syn ::Ident) -> proc_macro2 ::TokenStream
  {
   let ty_no_lifetimes = self.ty_generics_no_lifetimes();
   if self.syn_generics.params.is_empty() || 
  self.syn_generics.params.iter().all(|p| matches!(p, syn ::GenericParam ::Lifetime(_))) {
  quote ::quote! { #base_ident }
 } else {
  quote ::quote! { #base_ident #ty_no_lifetimes }
 }
 }
}
}




// Function implementations moved outside private module
/// Merges two `syn ::Generics` instances into a new one.
///
/// This function takes two references to `syn ::Generics` and combines their
/// type parameters and where clauses into a new `syn ::Generics` instance. If
/// both instances have where clauses, the predicates of these clauses are merged
/// into a single where clause.
///
/// # Arguments
///
/// * `a` - A reference to the first `syn ::Generics` instance.
/// * `b` - A reference to the second `syn ::Generics` instance.
///
/// # Returns
///
/// Returns a new `syn ::Generics` instance containing the merged type parameters
/// and where clauses from `a` and `b`.
#[ must_use ]
#[ allow( clippy ::default_trait_access ) ]
pub fn merge(a: &syn ::Generics, b: &syn ::Generics) -> syn ::Generics
{
  let mut result = syn ::Generics {
    params: Default ::default(),
    where_clause: None,
    lt_token: Some(syn ::token ::Lt ::default()),
    gt_token: Some(syn ::token ::Gt ::default()),
  };

  // Merge params
  for param in &a.params
  {
    result.params.push(param.clone());
  }
  for param in &b.params
  {
    result.params.push(param.clone());
  }

  // Merge where clauses
  result.where_clause =  match (&a.where_clause, &b.where_clause)
  {
    (Some(a_clause), Some(b_clause)) =>
    {
      let mut merged_where_clause = syn ::WhereClause {
        where_token: a_clause.where_token,
        predicates: a_clause.predicates.clone(),
      };
      for predicate in &b_clause.predicates
      {
        merged_where_clause.predicates.push(predicate.clone());
      }
      Some(merged_where_clause)
    }
    (Some(a_clause), None) => Some(a_clause.clone()),
    (None, Some(b_clause)) => Some(b_clause.clone()),
    _ => None,
  };

  result
}

/// Extracts parameter names from the given `Generics`,
/// dropping bounds, defaults, and the where clause.
#[ allow( clippy ::default_trait_access ) ]
#[ must_use ]
pub fn only_names(generics: &syn ::Generics) -> syn ::Generics
{
  use syn :: { Generics, GenericParam, LifetimeParam, TypeParam, ConstParam };

  let params = generics
    .params
    .iter()
    .map(|param| match param {
      GenericParam::Type(TypeParam { ident, .. }) => GenericParam::Type(TypeParam {
        attrs: Vec::new(),
        ident: ident.clone(),
        colon_token: None,
        bounds: Default::default(),
        eq_token: None,
        default: None,
      }),
      GenericParam::Lifetime(LifetimeParam { lifetime, .. }) => GenericParam::Lifetime(LifetimeParam {
        attrs: Vec::new(),
        lifetime: lifetime.clone(),
        colon_token: None,
        bounds: Default::default(),
      }),
      GenericParam::Const(ConstParam { ident, ty, .. }) => GenericParam::Const(ConstParam {
        attrs: Vec::new(),
        const_token: Default::default(),
        ident: ident.clone(),
        colon_token: Default::default(),
        ty: ty.clone(),
        eq_token: Default::default(),
        default: None,
      }),
    })
    .collect();

  Generics {
    params,
    where_clause: None,
    lt_token: generics.lt_token,
    gt_token: generics.gt_token,
  }
}

/// Extracts the names of type parameters, lifetimes, and const parameters from the given `Generics`.
pub fn names(generics: &syn ::Generics) -> impl Iterator< Item = &syn ::Ident >
{
  generics.params.iter().map( |param| match param
  {
    syn ::GenericParam ::Type(type_param) => &type_param.ident,
    syn ::GenericParam ::Lifetime(lifetime_def) => &lifetime_def.lifetime.ident,
    syn ::GenericParam ::Const(const_param) => &const_param.ident,
  } )
}

/// Decomposes `syn ::Generics` into components suitable for different usage contexts in Rust implementations.
#[ allow( clippy ::type_complexity ) ]
#[ allow( clippy ::too_many_lines ) ]
#[ must_use ]
pub fn decompose(
  generics: &syn ::Generics,
) -> (
  syn ::punctuated ::Punctuated< syn ::GenericParam, syn ::token ::Comma >,
  syn ::punctuated ::Punctuated< syn ::GenericParam, syn ::token ::Comma >,
  syn ::punctuated ::Punctuated< syn ::GenericParam, syn ::token ::Comma >,
  syn ::punctuated ::Punctuated< syn ::WherePredicate, syn ::token ::Comma >,
) {
  let mut generics_with_defaults = generics.params.clone();
  crate::punctuated ::ensure_trailing_comma(&mut generics_with_defaults);

  let mut generics_for_impl = syn ::punctuated ::Punctuated ::new();
  let mut generics_for_ty = syn ::punctuated ::Punctuated ::new();

  // Process each generic parameter
  let params_count = generics.params.len();
  for (idx, param) in generics.params.iter().enumerate()
  {
    let is_last = idx == params_count - 1;
    match param
    {
      syn ::GenericParam ::Type(type_param) =>
      {
        // Retain bounds for generics_for_impl, remove defaults
        let impl_param = syn ::GenericParam ::Type(syn ::TypeParam {
          attrs: vec![],
          ident: type_param.ident.clone(),
          colon_token: type_param.colon_token,
          bounds: type_param.bounds.clone(),
          eq_token: None, // Remove default token
          default: None,  // Remove default value
        });
        generics_for_impl.push_value(impl_param);
        if !is_last
        {
          generics_for_impl.push_punct(syn ::token ::Comma ::default());
        }

        // Simplify for generics_for_ty by removing all except identifiers
        let ty_param = syn ::GenericParam ::Type(syn ::TypeParam {
          attrs: vec![],
          ident: type_param.ident.clone(),
          colon_token: None,
          bounds: syn ::punctuated ::Punctuated ::new(),
          eq_token: None,
          default: None,
        });
        generics_for_ty.push_value(ty_param);
        if !is_last
        {
          generics_for_ty.push_punct(syn ::token ::Comma ::default());
        }
      }
      syn ::GenericParam ::Const(const_param) =>
      {
        // Simplify const parameters by removing all details except the identifier
        let impl_param = syn ::GenericParam ::Const(syn ::ConstParam {
          attrs: vec![],
          const_token: const_param.const_token,
          ident: const_param.ident.clone(),
          colon_token: const_param.colon_token,
          ty: const_param.ty.clone(),
          eq_token: None,
          default: None,
        });
        generics_for_impl.push_value(impl_param);
        if !is_last
        {
          generics_for_impl.push_punct(syn ::token ::Comma ::default());
        }

        let ty_param = syn ::GenericParam ::Const(syn ::ConstParam {
          attrs: vec![],
          const_token: const_param.const_token,
          ident: const_param.ident.clone(),
          colon_token: const_param.colon_token,
          ty: const_param.ty.clone(),
          eq_token: None,
          default: None,
        });
        generics_for_ty.push_value(ty_param);
        if !is_last
        {
          generics_for_ty.push_punct(syn ::token ::Comma ::default());
        }
      }
      syn ::GenericParam ::Lifetime(lifetime_param) =>
      {
        // Lifetimes are added as-is to generics_for_impl and without bounds to generics_for_ty
        generics_for_impl.push_value(syn ::GenericParam ::Lifetime(lifetime_param.clone()));
        if !is_last
        {
          generics_for_impl.push_punct(syn ::token ::Comma ::default());
        }

        let ty_param = syn ::GenericParam ::Lifetime(syn ::LifetimeParam {
          attrs: vec![],
          lifetime: lifetime_param.lifetime.clone(),
          colon_token: None,
          bounds: syn ::punctuated ::Punctuated ::new(),
        });
        generics_for_ty.push_value(ty_param);
        if !is_last
        {
          generics_for_ty.push_punct(syn ::token ::Comma ::default());
        }
      }
    }
  }

  // Remove any trailing punctuation from impl and ty generics to prevent trailing commas
  while generics_for_impl.trailing_punct()
  {
    generics_for_impl.pop_punct();
  }
  while generics_for_ty.trailing_punct()
  {
    generics_for_ty.pop_punct();
  }

  // Clone where predicates if present, ensuring they end with a comma
  let generics_where =  if let Some(where_clause) = &generics.where_clause
  {
    let mut predicates = where_clause.predicates.clone();
    crate::punctuated ::ensure_trailing_comma(&mut predicates);
    predicates
  } else {
    syn ::punctuated ::Punctuated ::new()
  };

  (generics_with_defaults, generics_for_impl, generics_for_ty, generics_where)
}

#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use own :: *;

#[ allow( unused_imports ) ]
/// Own namespace of the module.
pub mod own 
{

  use super :: *;

  #[ doc( inline ) ]
  pub use orphan :: *;
  #[ doc( inline ) ]
  pub use crate::generic_params::private :: {
  GenericsRef, GenericsWithWhere,
 };

  // Re-export the moved functions
  pub use super::{ merge, only_names, names, decompose };


  // Classification utilities
  #[ doc( inline ) ]
  pub use crate::generic_params::classification :: {
  GenericsClassification, classify_generics,
  DecomposedClassified, decompose_classified,
 };

  // Filter utilities
  #[ doc( inline ) ]
  pub use crate::generic_params::filter :: {
  filter_params,
  filter_lifetimes, filter_types, filter_consts, filter_non_lifetimes,
 };

  // Combination utilities
  #[ doc( inline ) ]
  pub use crate::generic_params::combine :: {
  merge_params_ordered, params_with_additional, params_from_components,
 };
}

/// Orphan namespace of the module.
#[ allow( unused_imports ) ]
pub mod orphan 
{

  use super :: *;
  #[ doc( inline ) ]
  pub use exposed :: *;
}

/// Exposed namespace of the module.
#[ allow( unused_imports ) ]
pub mod exposed 
{

  use super :: *;
  pub use crate::generic_params;

  #[ doc( inline ) ]
  pub use prelude :: *;
}

/// Prelude to use essentials: `use my_module ::prelude :: *`.
#[ allow( unused_imports ) ]
pub mod prelude
{
  use super :: *;
}