Skip to content

Commit

Permalink
macro_tools : more functions to manipulate generics, small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Wandalen committed Apr 16, 2024
1 parent 8467990 commit b483d28
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 51 deletions.
1 change: 0 additions & 1 deletion module/core/macro_tools/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,3 @@ pub mod exposed
pub mod prelude
{
}

57 changes: 56 additions & 1 deletion module/core/macro_tools/src/generic_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,60 @@
pub( crate ) mod private
{

/// A trait for converting a reference to an existing type into a `syn::AngleBracketedGenericArguments`.
///
/// This trait provides a mechanism to transform various types that represent generic parameters,
/// such as `syn::Generics`, into a uniform `syn::AngleBracketedGenericArguments`. This is particularly
/// useful when working with Rust syntax trees in procedural macros, allowing for the manipulation
/// and merging of generic parameters from different syntactic elements.
pub trait IntoGenericsArgs
{
/// Converts a reference of the implementing type into `syn::AngleBracketedGenericArguments`.
///
/// This method should handle the conversion logic necessary to transform the implementing
/// type's generic parameter representations into the structured format required by
/// `syn::AngleBracketedGenericArguments`, which is commonly used to represent generic parameters
/// enclosed in angle brackets.
///
/// # Returns
/// A new instance of `syn::AngleBracketedGenericArguments` representing the generic parameters
/// of the original type.
fn into_generics_args( &self ) -> syn::AngleBracketedGenericArguments;
}

impl IntoGenericsArgs for syn::Generics
{
fn into_generics_args( &self ) -> syn::AngleBracketedGenericArguments
{
let args = self.params.iter().map( | param |
{
match param
{
syn::GenericParam::Type( ty ) => syn::GenericArgument::Type( syn::Type::Path( syn::TypePath
{
qself: None,
path: ty.ident.clone().into(),
})),
syn::GenericParam::Lifetime( lifetime ) => syn::GenericArgument::Lifetime( lifetime.lifetime.clone() ),
syn::GenericParam::Const( const_param ) => syn::GenericArgument::Const( syn::Expr::Path( syn::ExprPath
{
attrs: vec![],
qself: None,
path: const_param.ident.clone().into(),
})),
}
}).collect();

syn::AngleBracketedGenericArguments
{
colon2_token: None,
lt_token: syn::token::Lt::default(),
args,
gt_token: syn::token::Gt::default(),
}
}
}

}

#[ doc( inline ) ]
Expand Down Expand Up @@ -35,13 +89,14 @@ pub mod orphan
#[ allow( unused_imports ) ]
pub use super::private::
{
IntoGenericsArgs,
};
}

/// Exposed namespace of the module.
pub mod exposed
{
pub use super::protected as generics;
pub use super::protected as generics_args;
#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use super::
Expand Down
5 changes: 3 additions & 2 deletions module/core/macro_tools/src/generic_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ pub( crate ) mod private
/// let mut generics : syn::Generics = parse_quote!{ < T : Clone + Default, U, 'a, const N : usize > };
/// generics.where_clause = parse_quote!{ where T: std::fmt::Debug };
/// // let generics : Generics = parse_quote!{ < T : Clone + Default, U, 'a, const N : usize > where T: std::fmt::Debug };
/// let simplified_generics = macro_tools::generics::params_names( &generics );
/// let simplified_generics = macro_tools::generic_params::params_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
Expand Down Expand Up @@ -307,6 +307,7 @@ pub mod protected
};
}

// xxx : external attr instead of internal?
/// Orphan namespace of the module.
pub mod orphan
{
Expand All @@ -324,7 +325,7 @@ pub mod orphan
/// Exposed namespace of the module.
pub mod exposed
{
pub use super::protected as generics;
pub use super::protected as generics_params;
#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use super::
Expand Down
5 changes: 5 additions & 0 deletions module/core/macro_tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub mod diag;
#[ cfg( feature = "enabled" ) ]
pub mod generic_analyze;
#[ cfg( feature = "enabled" ) ]
pub mod generic_args;
#[ cfg( feature = "enabled" ) ]
pub mod generic_params;
#[ cfg( feature = "enabled" ) ]
pub mod name;
Expand Down Expand Up @@ -56,6 +58,7 @@ pub mod protected
container_kind::orphan::*,
diag::orphan::*,
generic_analyze::orphan::*,
generic_args::orphan::*,
generic_params::orphan::*,
name::orphan::*,
quantifier::orphan::*,
Expand Down Expand Up @@ -95,6 +98,7 @@ pub mod exposed
container_kind::exposed::*,
diag::exposed::*,
generic_analyze::exposed::*,
generic_args::exposed::*,
generic_params::exposed::*,
name::exposed::*,
quantifier::exposed::*,
Expand Down Expand Up @@ -163,6 +167,7 @@ pub mod prelude
container_kind::prelude::*,
diag::prelude::*,
generic_analyze::prelude::*,
generic_args::prelude::*,
generic_params::prelude::*,
name::prelude::*,
quantifier::prelude::*,
Expand Down
189 changes: 148 additions & 41 deletions module/core/macro_tools/tests/inc/generic_args.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,148 @@

use super::*;

//

#[ test ]
fn assumptions()
{

// let code : syn::ItemStruct = syn::parse_quote!
// {
// pub struct Struct1Former
// <
// Definition = Struct1FormerDefinition< (), Struct1, former::ReturnPreformed >,
// >
// {}
// };
// tree_print!( code );

// let mut a : syn::Generics = parse_quote!
// {
// < 'a, T >
// };
// let mut b : syn::AngleBracketedGenericArguments = parse_quote!
// {
// < (), Struct1, former::ReturnPreformed >
// };
// let got = generics::merge( &a.into(), &b.into() );
// // let got = definition_extra_generics;

// let mut _got : syn::Generics = parse_quote!
// {
// < Struct1, former::ReturnPreformed >
// };

// let mut _got : syn::Generics = parse_quote!
// {
// < (), Struct1, former::ReturnPreformed >
// };

}
//
// use super::*;
//
// //
//
// #[ test ]
// fn assumptions()
// {
//
// // let code : syn::ItemStruct = syn::parse_quote!
// // {
// // pub struct Struct1Former
// // <
// // Definition = Struct1FormerDefinition< (), Struct1, former::ReturnPreformed >,
// // >
// // {}
// // };
// // tree_print!( code );
//
// // let mut a : syn::Generics = parse_quote!
// // {
// // < 'a, T >
// // };
// // let mut b : syn::IntoGenericsArgs = parse_quote!
// // {
// // < (), Struct1, former::ReturnPreformed >
// // };
// // let got = generics::merge( &a.into(), &b.into() );
// // // let got = definition_extra_generics;
//
// // let mut _got : syn::Generics = parse_quote!
// // {
// // < Struct1, former::ReturnPreformed >
// // };
//
// // let mut _got : syn::Generics = parse_quote!
// // {
// // < (), Struct1, former::ReturnPreformed >
// // };
//
// }
//
// //
//
// #[ test ]
// fn into_generics_args_empty_generics()
// {
// use syn::{ Generics, IntoGenericsArgs };
//
// let generics = Generics::default();
// let exp = IntoGenericsArgs::default();
// let got = generics.into_generics_args();
// assert_eq!( exp, got, "Failed into_generics_args_empty_generics: expected {:?}, got {:?}", exp, got );
// }
//
// //
//
// #[ test ]
// fn into_generics_args_single_type_parameter()
// {
// use syn::{ Generics, GenericParam, IntoGenericsArgs, GenericArgument, Type, TypePath, Ident };
//
// let generics = Generics
// {
// params: vec![ GenericParam::Type( syn::TypeParam { ident: Ident::new( "T", proc_macro2::Span::call_site() ), ..Default::default() } ) ].into(),
// ..Default::default()
// };
// let exp = IntoGenericsArgs
// {
// args: vec![ GenericArgument::Type( Type::Path( TypePath { qself: None, path: "T".into() } ) ) ],
// ..Default::default()
// };
// let got = generics.into_generics_args();
// assert_eq!( exp, got, "Failed into_generics_args_single_type_parameter: expected {:?}, got {:?}", exp, got );
// }
//
// //
//
// #[ test ]
// fn into_generics_args_single_lifetime_parameter()
// {
// use syn::{ Generics, GenericParam, IntoGenericsArgs, GenericArgument, Lifetime };
//
// let generics = Generics
// {
// params: vec![ GenericParam::Lifetime( syn::LifetimeDef { lifetime: Lifetime::new( "'a", proc_macro2::Span::call_site() ), ..Default::default() } ) ].into(),
// ..Default::default()
// };
// let exp = IntoGenericsArgs
// {
// args: vec![ GenericArgument::Lifetime( Lifetime::new( "'a", proc_macro2::Span::call_site() ) ) ],
// ..Default::default()
// };
// let got = generics.into_generics_args();
// assert_eq!( exp, got, "Failed into_generics_args_single_lifetime_parameter: expected {:?}, got {:?}", exp, got );
// }
//
// //
//
// #[ test ]
// fn into_generics_args_single_const_parameter()
// {
// use syn::{ Generics, GenericParam, IntoGenericsArgs, GenericArgument, Expr, ExprPath, Ident };
//
// let generics = Generics
// {
// params: vec![ GenericParam::Const( syn::ConstParam { ident: Ident::new( "N", proc_macro2::Span::call_site() ), ..Default::default() } ) ].into(),
// ..Default::default()
// };
// let exp = IntoGenericsArgs
// {
// args: vec![ GenericArgument::Const( Expr::Path( ExprPath { attrs: vec![], qself: None, path: "N".into() } ) ) ],
// ..Default::default()
// };
// let got = generics.into_generics_args();
// assert_eq!( exp, got, "Failed into_generics_args_single_const_parameter: expected {:?}, got {:?}", exp, got );
// }
//
// //
//
// #[ test ]
// fn into_generics_args_mixed_parameters()
// {
// use syn::{ Generics, GenericParam, IntoGenericsArgs, GenericArgument, Type, TypePath, Lifetime, Expr, ExprPath, Ident };
//
// let generics = Generics
// {
// params : vec!
// [
// GenericParam::Type( syn::TypeParam { ident: Ident::new( "T", proc_macro2::Span::call_site() ), ..Default::default() } ),
// GenericParam::Lifetime( syn::LifetimeDef { lifetime: Lifetime::new( "'a", proc_macro2::Span::call_site() ), ..Default::default() } ),
// GenericParam::Const( syn::ConstParam { ident: Ident::new( "N", proc_macro2::Span::call_site() ), ..Default::default() } )
// ].into(),
// ..Default::default()
// };
// let exp = IntoGenericsArgs
// {
// args : vec!
// [
// GenericArgument::Type( Type::Path( TypePath { qself: None, path: "T".into() } ) ),
// GenericArgument::Lifetime( Lifetime::new( "'a", proc_macro2::Span::call_site() ) ),
// GenericArgument::Const( Expr::Path( ExprPath { attrs: vec![], qself: None, path: "N".into() } ) )
// ],
// ..Default::default()
// };
// let got = generics.into_generics_args();
// assert_eq!( exp, got, "Failed into_generics_args_mixed_parameters: expected {:?}, got {:?}", exp, got );
// }
12 changes: 6 additions & 6 deletions module/core/macro_tools/tests/inc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ mod if_enabled
use the_module::exposed::*;

mod attr;
mod basic;
mod generic_args;
mod generic_params;
mod quantifier;
mod syntax;
mod tokens;
// mod basic;
// mod generic_args;
// mod generic_params;
// mod quantifier;
// mod syntax;
// mod tokens;

}

0 comments on commit b483d28

Please sign in to comment.