@@ -130,14 +130,34 @@ impl SwiftBridgeModule {
130
130
convert_ffi_variants_to_rust. push ( convert_ffi_variant_to_rust) ;
131
131
}
132
132
133
- // TODO:
134
- // Parse any derives that the user has specified and combine those with our auto derives.
135
- let automatic_derives = if shared_enum. has_one_or_more_variants_with_data ( ) {
133
+ // Auto derives
134
+ let mut derives = if shared_enum. has_one_or_more_variants_with_data ( ) {
136
135
vec ! [ ]
137
136
} else {
138
137
vec ! [ quote! { Copy } , quote! { Clone } ]
139
138
} ;
140
139
140
+ // User derives
141
+ let mut derive_impl_ffi_bridges = vec ! [ ] ;
142
+
143
+ // We currently only allow derive(Debug) on non data carrying enums in order
144
+ // to prevent a potential memory safety issue.
145
+ // https://github.com/chinedufn/swift-bridge/pull/194#discussion_r1134386788
146
+ if shared_enum. derive . debug && !shared_enum. has_one_or_more_variants_with_data ( ) {
147
+ derives. push ( quote ! { :: std:: fmt:: Debug } ) ;
148
+
149
+ // __swift_bridge__$SomeEnum$Debug
150
+ let export_name = format ! ( "{}$Debug" , shared_enum. ffi_name_string( ) ) ;
151
+ // __swift_bridge__SomeEnum_Debug
152
+ let fn_name = format_ident ! ( "{}_Debug" , enum_ffi_name) ;
153
+ derive_impl_ffi_bridges. push ( quote ! {
154
+ #[ export_name = #export_name]
155
+ pub extern "C" fn #fn_name( this: #enum_ffi_name) -> * mut swift_bridge:: string:: RustString {
156
+ swift_bridge:: string:: RustString ( format!( "{:?}" , this. into_rust_repr( ) ) ) . box_into_raw( )
157
+ }
158
+ } ) ;
159
+ }
160
+
141
161
let vec_support = if shared_enum. has_one_or_more_variants_with_data ( ) {
142
162
// Enums with variants that contain data are not yet supported.
143
163
quote ! { }
@@ -146,7 +166,7 @@ impl SwiftBridgeModule {
146
166
} ;
147
167
148
168
let definition = quote ! {
149
- #[ derive( #( #automatic_derives ) , * ) ]
169
+ #[ derive( #( #derives ) , * ) ]
150
170
pub enum #enum_name {
151
171
#( #enum_variants) , *
152
172
}
@@ -217,6 +237,8 @@ impl SwiftBridgeModule {
217
237
}
218
238
219
239
#vec_support
240
+
241
+ #( #derive_impl_ffi_bridges) , *
220
242
} ;
221
243
222
244
Some ( definition)
0 commit comments