11use crate :: expand:: display_namespaced;
22use crate :: syntax:: instantiate:: NamedImplKey ;
33use crate :: syntax:: types:: ConditionalImpl ;
4- use crate :: syntax:: { Lifetimes , NamedType , Type , Types } ;
4+ use crate :: syntax:: { Lifetimes , Type , Types } ;
55use proc_macro2:: TokenStream ;
6- use quote:: ToTokens ;
6+ use quote:: { quote_spanned , ToTokens } ;
77use syn:: { Lifetime , Token } ;
88
99pub ( crate ) struct ResolvedGenericType < ' a > {
@@ -26,7 +26,7 @@ pub(crate) fn split_for_impl<'a>(
2626 let impl_generics = if let Some ( explicit_impl) = conditional_impl. explicit_impl {
2727 & explicit_impl. impl_generics
2828 } else {
29- types . resolve ( local_type ( key. inner ) ) . generics
29+ get_impl_generics ( key. inner , types )
3030 } ;
3131 let ty_generics = ResolvedGenericType {
3232 ty : key. inner ,
@@ -61,21 +61,57 @@ impl<'a> ToTokens for ResolvedGenericType<'a> {
6161 }
6262 }
6363 }
64+ Type :: RustBox ( ty1) => {
65+ let span = ty1. name . span ( ) ;
66+ let inner = ResolvedGenericType {
67+ ty : & ty1. inner ,
68+ explicit_impl : self . explicit_impl ,
69+ types : self . types ,
70+ } ;
71+ tokens. extend ( quote_spanned ! { span=>
72+ :: cxx:: alloc:: boxed:: Box <#inner>
73+ } ) ;
74+ }
6475 _ => unreachable ! ( "syntax/check.rs should reject other types" ) ,
6576 }
6677 }
6778}
6879
69- pub ( crate ) fn local_type ( ty : & Type ) -> & NamedType {
80+ fn get_impl_generics < ' a > ( ty : & Type , types : & ' a Types < ' a > ) -> & ' a Lifetimes {
7081 match ty {
71- Type :: Ident ( named_type) => named_type,
82+ Type :: Ident ( named_type) => types. resolve ( named_type) . generics ,
83+ Type :: RustBox ( ty1) => get_impl_generics ( & ty1. inner , types) ,
84+ _ => unreachable ! ( "syntax/check.rs should reject other types" ) ,
85+ }
86+ }
87+
88+ pub ( crate ) fn format_for_prevent_unwind_label ( ty : & Type ) -> TokenStream {
89+ match ty {
90+ Type :: Ident ( named_type) => {
91+ let span = named_type. rust . span ( ) ;
92+ let rust_name = named_type. rust . to_string ( ) ;
93+ quote_spanned ! { span=>
94+ :: cxx:: core:: concat!( :: cxx:: core:: module_path!( ) , "::" , #rust_name)
95+ }
96+ }
97+ Type :: RustBox ( ty1) => {
98+ let span = ty1. name . span ( ) ;
99+ let inner = format_for_prevent_unwind_label ( & ty1. inner ) ;
100+ quote_spanned ! { span=>
101+ :: cxx:: core:: concat!( "Box<" , #inner, ">" )
102+ }
103+ }
72104 _ => unreachable ! ( "syntax/check.rs should reject other types" ) ,
73105 }
74106}
75107
76108pub ( crate ) fn concise_rust_name ( ty : & Type ) -> String {
77109 match ty {
78110 Type :: Ident ( named_type) => named_type. rust . to_string ( ) ,
111+ Type :: RustBox ( ty1) => {
112+ let inner = concise_rust_name ( & ty1. inner ) ;
113+ format ! ( "Box<{inner}>" )
114+ }
79115 _ => unreachable ! ( "syntax/check.rs should reject other types" ) ,
80116 }
81117}
@@ -86,6 +122,10 @@ pub(crate) fn concise_cxx_name(ty: &Type, types: &Types) -> String {
86122 let res = types. resolve ( & named_type. rust ) ;
87123 display_namespaced ( res. name ) . to_string ( )
88124 }
125+ Type :: RustBox ( ty1) => {
126+ let inner = concise_cxx_name ( & ty1. inner , types) ;
127+ format ! ( "rust::Box<{inner}>" )
128+ }
89129 _ => unreachable ! ( "syntax/check.rs should reject other types" ) ,
90130 }
91131}
0 commit comments