@@ -41,8 +41,8 @@ fn expand(ffi: Module, apis: &[Api], types: &Types) -> TokenStream {
41
41
for api in apis {
42
42
match api {
43
43
Api :: Include ( _) | Api :: RustType ( _) | Api :: Impl ( _) => { }
44
- Api :: Struct ( strct) => expanded. extend ( expand_struct ( strct) ) ,
45
- Api :: Enum ( enm) => expanded. extend ( expand_enum ( enm) ) ,
44
+ Api :: Struct ( strct) => expanded. extend ( expand_struct ( namespace , strct) ) ,
45
+ Api :: Enum ( enm) => expanded. extend ( expand_enum ( namespace , enm) ) ,
46
46
Api :: CxxType ( ety) => {
47
47
let ident = & ety. ident ;
48
48
if !types. structs . contains_key ( ident) && !types. enums . contains_key ( ident) {
@@ -125,37 +125,46 @@ fn expand(ffi: Module, apis: &[Api], types: &Types) -> TokenStream {
125
125
}
126
126
}
127
127
128
- fn expand_struct ( strct : & Struct ) -> TokenStream {
128
+ fn expand_struct ( namespace : & Namespace , strct : & Struct ) -> TokenStream {
129
129
let ident = & strct. ident ;
130
130
let doc = & strct. doc ;
131
131
let derives = & strct. derives ;
132
+ let type_id = type_id ( namespace, ident) ;
132
133
let fields = strct. fields . iter ( ) . map ( |field| {
133
134
// This span on the pub makes "private type in public interface" errors
134
135
// appear in the right place.
135
136
let vis = Token ! [ pub ] ( field. ident . span ( ) ) ;
136
137
quote ! ( #vis #field)
137
138
} ) ;
139
+
138
140
quote ! {
139
141
#doc
140
142
#[ derive( #( #derives) , * ) ]
141
143
#[ repr( C ) ]
142
144
pub struct #ident {
143
145
#( #fields, ) *
144
146
}
147
+
148
+ unsafe impl :: cxx:: ExternType for #ident {
149
+ type Id = #type_id;
150
+ type Kind = :: cxx:: kind:: Trivial ;
151
+ }
145
152
}
146
153
}
147
154
148
- fn expand_enum ( enm : & Enum ) -> TokenStream {
155
+ fn expand_enum ( namespace : & Namespace , enm : & Enum ) -> TokenStream {
149
156
let ident = & enm. ident ;
150
157
let doc = & enm. doc ;
151
158
let repr = enm. repr ;
159
+ let type_id = type_id ( namespace, ident) ;
152
160
let variants = enm. variants . iter ( ) . map ( |variant| {
153
161
let variant_ident = & variant. ident ;
154
162
let discriminant = & variant. discriminant ;
155
163
Some ( quote ! {
156
164
pub const #variant_ident: Self = #ident { repr: #discriminant } ;
157
165
} )
158
166
} ) ;
167
+
159
168
quote ! {
160
169
#doc
161
170
#[ derive( Copy , Clone , PartialEq , Eq ) ]
@@ -168,6 +177,11 @@ fn expand_enum(enm: &Enum) -> TokenStream {
168
177
impl #ident {
169
178
#( #variants) *
170
179
}
180
+
181
+ unsafe impl :: cxx:: ExternType for #ident {
182
+ type Id = #type_id;
183
+ type Kind = :: cxx:: kind:: Trivial ;
184
+ }
171
185
}
172
186
}
173
187
0 commit comments