@@ -74,6 +74,17 @@ def is_instance_type(cls, swift_type: str, raw_rust_type: str, include_options_a
7474 def is_array_type (cls , swift_type : str ):
7575 return swift_type .startswith ('[' )
7676
77+ @classmethod
78+ def is_type_cloneable (cls , raw_rust_type : str ):
79+ cloneability_lookup = 'x-uncloneable'
80+ individual_cloneability_lookup = None
81+ if raw_rust_type is not None :
82+ cloneability_lookup = raw_rust_type
83+ if cloneability_lookup .startswith ('LDK' ):
84+ cloneability_lookup = cloneability_lookup [len ('LDK' ):]
85+ is_cloneable = cloneability_lookup in cloneable_types
86+ return is_cloneable
87+
7788 @classmethod
7889 def prepare_swift_to_native_arguments (cls , argument_types , is_trait_callback = False , force_pass_instance = False , is_free_method = False , is_returned_value_freeable = False , unwrap_complex_arrays = True , array_unwrapping_preparation_only = False , is_trait_default_redirect = False ):
7990 swift_arguments = []
@@ -309,7 +320,7 @@ def prepare_swift_to_native_arguments(cls, argument_types, is_trait_callback=Fal
309320 force_unwrap_suffix = '!'
310321 native_arguments .append (f'Bindings.string_to_unsafe_int8_pointer(string: { passed_argument_name } { force_unwrap_suffix } )' )
311322 else :
312- native_arguments .append (f'Bindings.new_LDKStr(string: { passed_argument_name } )' )
323+ native_arguments .append (f'Bindings.new_LDKStr(string: { passed_argument_name } , chars_is_owned: true )' )
313324 elif current_argument_details .rust_obj is None and current_argument_details .arr_len is not None and current_argument_details .arr_len .isnumeric ():
314325 if current_argument_details .is_const :
315326 force_unwrap_suffix = ''
@@ -372,7 +383,16 @@ def prepare_native_to_swift_callback_arguments(cls, argument_types, array_unwrap
372383 swift_local_conversion_suffix = ')'
373384 # TODO: see if `current_argument_details.pass_by_ref` condition is necessary
374385 if current_argument_details .passed_as_ptr and current_argument_details .pass_by_ref :
386+ # this is useful for traits, but some types may not have cloneability support
375387 swift_local_conversion_suffix = ').dangle()'
388+ is_cloneable = ConversionHelper .is_type_cloneable (received_raw_type )
389+ if is_cloneable :
390+ swift_local_conversion_suffix += '.clone()'
391+ else :
392+ print (f"trait callback uncloneable type danger: { received_raw_type } / { published_swift_type } " )
393+ elif current_argument_details .passed_as_ptr or current_argument_details .pass_by_ref :
394+ descriptor = 'passed_as_ptr' if current_argument_details .passed_as_ptr else 'pass_by_ref'
395+ print (f'{ descriptor } : { received_raw_type } / { published_swift_type } ' )
376396 elif received_raw_type .startswith ('LDKCOption_' ) and received_raw_type == 'LDKC' + published_swift_type :
377397 swift_local_conversion_prefix = f'{ published_swift_type } (pointer: '
378398 swift_local_conversion_suffix = ')'
0 commit comments