@@ -130,14 +130,34 @@ impl SwiftBridgeModule {
130130 convert_ffi_variants_to_rust. push ( convert_ffi_variant_to_rust) ;
131131 }
132132
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 ( ) {
136135 vec ! [ ]
137136 } else {
138137 vec ! [ quote! { Copy } , quote! { Clone } ]
139138 } ;
140139
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+
141161 let vec_support = if shared_enum. has_one_or_more_variants_with_data ( ) {
142162 // Enums with variants that contain data are not yet supported.
143163 quote ! { }
@@ -146,7 +166,7 @@ impl SwiftBridgeModule {
146166 } ;
147167
148168 let definition = quote ! {
149- #[ derive( #( #automatic_derives ) , * ) ]
169+ #[ derive( #( #derives ) , * ) ]
150170 pub enum #enum_name {
151171 #( #enum_variants) , *
152172 }
@@ -217,6 +237,8 @@ impl SwiftBridgeModule {
217237 }
218238
219239 #vec_support
240+
241+ #( #derive_impl_ffi_bridges) , *
220242 } ;
221243
222244 Some ( definition)
0 commit comments