@@ -22,14 +22,46 @@ use macro_magic::mm_core::ForeignPath;
2222use proc_macro2:: TokenStream as TokenStream2 ;
2323use quote:: { quote, ToTokens } ;
2424use std:: collections:: HashSet ;
25- use syn:: { parse2, parse_quote, spanned:: Spanned , Ident , ImplItem , ItemImpl , Path , Result , Token } ;
25+ use syn:: {
26+ parse2, parse_quote, spanned:: Spanned , token, Ident , ImplItem , ItemImpl , Path , Result , Token ,
27+ } ;
2628
27- #[ derive( Parse ) ]
29+ mod keyword {
30+ syn:: custom_keyword!( inject_runtime_type) ;
31+ syn:: custom_keyword!( no_aggregated_types) ;
32+ }
33+
34+ #[ derive( derive_syn_parse:: Parse , PartialEq , Eq ) ]
35+ pub enum PalletAttrType {
36+ #[ peek( keyword:: inject_runtime_type, name = "inject_runtime_type" ) ]
37+ RuntimeType ( keyword:: inject_runtime_type ) ,
38+ }
39+
40+ #[ derive( derive_syn_parse:: Parse ) ]
41+ pub struct PalletAttr {
42+ _pound : Token ! [ #] ,
43+ #[ bracket]
44+ _bracket : token:: Bracket ,
45+ #[ inside( _bracket) ]
46+ typ : PalletAttrType ,
47+ }
48+
49+ fn get_first_item_pallet_attr < Attr > ( item : & syn:: ImplItemType ) -> syn:: Result < Option < Attr > >
50+ where
51+ Attr : syn:: parse:: Parse ,
52+ {
53+ item. attrs . get ( 0 ) . map ( |a| syn:: parse2 ( a. into_token_stream ( ) ) ) . transpose ( )
54+ }
55+
56+ #[ derive( Parse , Debug ) ]
2857pub struct DeriveImplAttrArgs {
2958 pub default_impl_path : Path ,
3059 _as : Option < Token ! [ as ] > ,
3160 #[ parse_if( _as. is_some( ) ) ]
3261 pub disambiguation_path : Option < Path > ,
62+ _comma : Option < Token ! [ , ] > ,
63+ #[ parse_if( _comma. is_some( ) ) ]
64+ pub no_aggregated_types : Option < keyword:: no_aggregated_types > ,
3365}
3466
3567impl ForeignPath for DeriveImplAttrArgs {
@@ -43,6 +75,8 @@ impl ToTokens for DeriveImplAttrArgs {
4375 tokens. extend ( self . default_impl_path . to_token_stream ( ) ) ;
4476 tokens. extend ( self . _as . to_token_stream ( ) ) ;
4577 tokens. extend ( self . disambiguation_path . to_token_stream ( ) ) ;
78+ tokens. extend ( self . _comma . to_token_stream ( ) ) ;
79+ tokens. extend ( self . no_aggregated_types . to_token_stream ( ) ) ;
4680 }
4781}
4882
@@ -78,6 +112,7 @@ fn combine_impls(
78112 foreign_impl : ItemImpl ,
79113 default_impl_path : Path ,
80114 disambiguation_path : Path ,
115+ inject_runtime_types : bool ,
81116) -> ItemImpl {
82117 let ( existing_local_keys, existing_unsupported_items) : ( HashSet < ImplItem > , HashSet < ImplItem > ) =
83118 local_impl
@@ -96,7 +131,20 @@ fn combine_impls(
96131 // do not copy colliding items that have an ident
97132 return None
98133 }
99- if matches ! ( item, ImplItem :: Type ( _) ) {
134+ if let ImplItem :: Type ( typ) = item. clone ( ) {
135+ let mut typ = typ. clone ( ) ;
136+ if let Ok ( Some ( PalletAttr { typ : PalletAttrType :: RuntimeType ( _) , .. } ) ) =
137+ get_first_item_pallet_attr :: < PalletAttr > ( & mut typ)
138+ {
139+ let item: ImplItem = if inject_runtime_types {
140+ parse_quote ! {
141+ type #ident = #ident;
142+ }
143+ } else {
144+ item
145+ } ;
146+ return Some ( item)
147+ }
100148 // modify and insert uncolliding type items
101149 let modified_item: ImplItem = parse_quote ! {
102150 type #ident = <#default_impl_path as #disambiguation_path>:: #ident;
@@ -132,6 +180,7 @@ pub fn derive_impl(
132180 foreign_tokens : TokenStream2 ,
133181 local_tokens : TokenStream2 ,
134182 disambiguation_path : Option < Path > ,
183+ no_aggregated_types : Option < keyword:: no_aggregated_types > ,
135184) -> Result < TokenStream2 > {
136185 let local_impl = parse2 :: < ItemImpl > ( local_tokens) ?;
137186 let foreign_impl = parse2 :: < ItemImpl > ( foreign_tokens) ?;
@@ -151,8 +200,13 @@ pub fn derive_impl(
151200 } ;
152201
153202 // generate the combined impl
154- let combined_impl =
155- combine_impls ( local_impl, foreign_impl, default_impl_path, disambiguation_path) ;
203+ let combined_impl = combine_impls (
204+ local_impl,
205+ foreign_impl,
206+ default_impl_path,
207+ disambiguation_path,
208+ no_aggregated_types. is_none ( ) ,
209+ ) ;
156210
157211 Ok ( quote ! ( #combined_impl) )
158212}
0 commit comments