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
//!
//! 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`.
//!
/// Internal namespace.
mod private
{
use crate::*;
use crate::IterTrait;
// use iter_tools::IterTrait;
/// 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::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`.
pub fn unwrap( self ) -> syn::Generics
{
self.generics
}
/// Parses a string to a `GenericsWithWhere`, specifically designed to handle generics syntax with where clauses effectively.
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 }
}
}
/// 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`.
///
/// # Examples
///
///
/// # use syn::{Generics, parse_quote};
///
/// let mut generics_a : syn::Generics = parse_quote!{ < T : Clone, U : Default > };
/// generics_a.where_clause = parse_quote!{ where T : Default };
/// let mut generics_b : syn::Generics = parse_quote!{ < V : core::fmt::Debug > };
/// generics_b.where_clause = parse_quote!{ where V : Sized };
/// let got = generic_params::merge( &generics_a, &generics_b );
///
/// let mut exp : syn::Generics = parse_quote!
/// {
/// < T : Clone, U : Default, V : core::fmt::Debug >
/// };
/// exp.where_clause = parse_quote!
/// {
/// where
/// T : Default,
/// V : Sized
/// };
///
/// assert_eq!( got, exp );
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
// result.params.extend( a.params.iter().chain( b.params.iter() ) );
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.
///
/// This function simplifies the generics to include only the names of the type parameters,
/// lifetimes, and const parameters, without any of their associated bounds or default values.
/// The resulting `Generics` will have an empty where clause.
///
/// # Arguments
///
/// * `generics` - The `Generics` instance from which to extract parameter names.
///
/// # Returns
///
/// Returns a new `Generics` instance containing only the names of the parameters.
///
/// # Examples
///
/// ```rust
/// # use macro_tools::syn::parse_quote;
///
/// let mut generics : syn::Generics = parse_quote!{ < T : Clone + Default, U, 'a, const N : usize > };
/// generics.where_clause = parse_quote!{ where T: core::fmt::Debug };
/// // let generics : Generics = parse_quote!{ < T : Clone + Default, U, 'a, const N : usize > where T: core::fmt::Debug };
/// let simplified_generics = macro_tools::generic_params::only_names( &generics );
///
/// assert_eq!( simplified_generics.params.len(), 4 ); // Contains T, U, 'a, and N
/// assert!( simplified_generics.where_clause.is_none() ); // Where clause is removed
/// ```
pub fn only_names( generics : &syn::Generics ) -> syn::Generics
{
// use syn::{ Generics, GenericParam, LifetimeDef, TypeParam, ConstParam };
use syn::{ Generics, GenericParam, LifetimeParam, TypeParam, ConstParam };
let result = Generics
{
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(),
where_clause : None,
lt_token : generics.lt_token,
gt_token : generics.gt_token,
};
result
}
/// Extracts the names of type parameters, lifetimes, and const parameters from the given `Generics`.
///
/// This function returns an iterator over the names of the parameters in the `Generics`,
/// which can be useful for generating code that requires just the names of the parameters
/// without their associated bounds or default values.
///
/// # Arguments
///
/// * `generics` - The `Generics` instance from which to extract parameter names.
///
/// # Returns
///
/// Returns an iterator over the names of the parameters.
///
/// # Examples
///
/// ```rust
/// # use macro_tools::syn::parse_quote;
///
/// let generics : syn::Generics = parse_quote!
/// {
/// < T : Clone + Default, U, 'a, const N : usize >
/// };
/// let names : Vec< _ > = macro_tools::generic_params::names( &generics ).collect();
///
/// assert_eq!( names, vec!
/// [
/// &syn::Ident::new( "T", proc_macro2::Span::call_site() ),
/// &syn::Ident::new( "U", proc_macro2::Span::call_site() ),
/// &syn::Ident::new( "a", proc_macro2::Span::call_site() ),
/// &syn::Ident::new( "N", proc_macro2::Span::call_site() )
/// ]);
/// ```
pub fn names< 'a >( generics : &'a syn::Generics )
-> impl IterTrait< 'a, &'a syn::Ident >
// -> std::iter::Map
// <
// syn::punctuated::Iter< 'a, syn::GenericParam >,
// impl FnMut( &'a syn::GenericParam ) -> &'a syn::Ident + 'a,
// >
{
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,
/// specifically focusing on different requirements for `impl` blocks and type definitions.
///
/// This function prepares three versions of the generics:
/// - One preserving the full structure for `impl` declarations.
/// - One simplified for type definitions, removing bounds and defaults from type and const parameters, retaining only identifiers.
/// - One for the where clauses, if present, ensuring they are correctly punctuated.
///
/// This helps in situations where you need different representations of generics for implementing traits,
/// defining types, or specifying trait bounds and conditions.
///
/// This function is similar to `syn::Generics::split_for_impl`, which also splits generics into components
/// suitable for `impl` blocks and type definitions. However, `split_for_impl` wraps the tokens in `<>`, which
/// can reduce the flexibility of the results. The `decompose` function provides more control over the output
/// by not wrapping the tokens, allowing for more precise usage in macros and other contexts.
/// Additionally, `decompose` returns an extra component with the generics including defaults, which is often
/// in demand for certain macro or code generation tasks.
///
/// # Examples
///
/// ```rust
/// let code : syn::Generics = syn::parse_quote!{ <'a, T, const N : usize, U : Trait1> };
/// let ( generics_with_defaults, generics_for_impl, generics_for_ty, generics_where ) = macro_tools::generic_params::decompose( &code );
///
/// // Use in a macro for generating code
/// macro_tools::qt!
/// {
/// impl < #generics_for_impl > MyTrait for Struct1 < #generics_for_ty >
/// where
/// #generics_where
/// {
/// // implementation details...
/// }
/// };
/// ```
///
/// # Arguments
///
/// * `generics` - A reference to the `syn::Generics` to be decomposed.
///
/// # Returns
///
/// Returns a tuple containing:
/// - `syn::punctuated::Punctuated<syn::GenericParam, syn::token::Comma>`: Original generics with defaults, used where full specification is needed.
/// - `syn::punctuated::Punctuated<syn::GenericParam, syn::token::Comma>`: Generics for `impl` blocks, retaining bounds but no defaults.
/// - `syn::punctuated::Punctuated<syn::GenericParam, syn::token::Comma>`: Simplified generics for type definitions, only identifiers.
/// - `syn::punctuated::Punctuated<syn::WherePredicate, syn::token::Comma>`: Where clauses, properly punctuated for use in where conditions.
///
/// # Differences from `syn::Generics::split_for_impl`
///
/// While both `decompose` and `split_for_impl` functions split generics into components for `impl` blocks, type definitions, and where clauses,
/// there are key differences:
/// - `split_for_impl` wraps the generics in `<>`, which can be limiting when you need to use the generics in a different context or format.
/// - `decompose` provides raw punctuated generic parameters, offering greater flexibility and control over the output format.
/// - `decompose` returns an extra component with the generics including defaults, which is often needed for certain macro or code generation tasks.
///
/// # Example of function signature using `decompose`
///
/// ```rust
/// use macro_tools::{ syn, proc_macro2, qt };
///
/// fn generate_unit
/// (
/// item_name : &syn::Ident,
/// generics_with_defaults : syn::punctuated::Punctuated< syn::GenericParam, syn::token::Comma >,
/// generics_impl : syn::punctuated::Punctuated< syn::GenericParam, syn::token::Comma >,
/// generics_ty : syn::punctuated::Punctuated< syn::GenericParam, syn::token::Comma >,
/// generics_where: syn::punctuated::Punctuated< syn::WherePredicate, syn::token::Comma >,
/// )
/// -> proc_macro2::TokenStream
/// {
/// qt!
/// {
/// #[ automatically_derived ]
/// impl< #generics_impl > From< i32 > for #item_name< #generics_ty >
/// where
/// #generics_where
/// {
/// #[ inline ]
/// fn from( src : i32 ) -> Self
/// {
/// Wrap( src )
/// }
/// }
/// }
/// }
/// ```
///
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();
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
for param in &generics.params
{
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 );
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 );
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 );
generics_for_impl.push_punct( syn::token::Comma::default() );
let ty_param = syn::GenericParam::Type( syn::TypeParam
{
attrs : vec![],
ident : const_param.ident.clone(),
colon_token : None,
bounds : syn::punctuated::Punctuated::new(),
eq_token : None,
default : None,
});
generics_for_ty.push_value( ty_param );
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() ) );
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 );
generics_for_ty.push_punct( syn::token::Comma::default() );
}
}
}
// 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();
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 private::
{
merge,
only_names,
names,
decompose,
};
}
/// Orphan namespace of the module.
#[ allow( unused_imports ) ]
pub mod orphan
{
use super::*;
#[ doc( inline ) ]
pub use exposed::*;
#[ doc( inline ) ]
pub use private::
{
GenericsWithWhere,
};
}
/// Exposed namespace of the module.
#[ allow( unused_imports ) ]
pub mod exposed
{
use super::*;
pub use super::super::generic_params;
#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use super::
{
prelude::*,
};
}
/// Prelude to use essentials: `use my_module::prelude::*`.
#[ allow( unused_imports ) ]
pub mod prelude
{
use super::*;
}