44cloneable_types = set ()
55detected_cloneable_types = set ()
66
7+
78class ConversionHelper :
89 trait_structs = set ()
910
1011 @classmethod
11- def prepare_swift_to_native_arguments (cls , argument_types , is_trait_callback = False , force_pass_instance = False , is_free_method = False ):
12+ def prepare_swift_to_native_arguments (cls , argument_types , is_trait_callback = False , force_pass_instance = False , is_free_method = False ):
1213 swift_arguments = []
1314 native_arguments = []
1415 pointer_wrapping_prefix = ''
@@ -43,8 +44,6 @@ def prepare_swift_to_native_arguments(cls, argument_types, is_trait_callback = F
4344 passed_argument_name = 'self'
4445 static_eligible = False
4546
46-
47-
4847 if current_argument_details .is_ptr :
4948 passed_argument_name = argument_name + 'Pointer'
5049 if argument_name == 'init' :
@@ -87,25 +86,23 @@ def prepare_swift_to_native_arguments(cls, argument_types, is_trait_callback = F
8786
8887 initialization_target = f'{ argument_name } Unwrapped.cOpaqueStruct!'
8988
90- if current_argument_details .rust_obj is not None and current_argument_details .rust_obj .startswith (
91- 'LDK' ) and current_argument_details .swift_type .startswith ('[' ):
92- # TODO: figure out why this never gets hit, or rather why I originally wrote this
93- initialization_target = f'Bindings.new_{ current_argument_details .rust_obj } Wrapper(array: { argument_name } Unwrapped).cOpaqueStruct!'
94- is_pointer_to_array = True
89+ if current_argument_details .rust_obj is not None and current_argument_details .rust_obj .startswith ('LDK' ) and current_argument_details .swift_type .startswith ('[' ):
90+ # TODO: figure out why this never gets hit, or rather why I originally wrote this
91+ initialization_target = f'Bindings.new_{ current_argument_details .rust_obj } Wrapper(array: { argument_name } Unwrapped).cOpaqueStruct!'
92+ is_pointer_to_array = True
9593
9694 native_call_prep += f'''
9795 var { passed_argument_name } : UnsafeMutablePointer<{ current_argument_details .rust_obj } >? = nil
9896 if let { argument_name } Unwrapped = { argument_name } {{
9997 { passed_argument_name } = UnsafeMutablePointer<{ current_argument_details .rust_obj } >.allocate(capacity: 1)
10098 { passed_argument_name } !.initialize(to: { initialization_target } )
10199 }}
102- '''
103- # print('optional argument:', argument_name, passed_argument_name)
100+ ''' # print('optional argument:', argument_name, passed_argument_name)
104101 else :
105102 wrapper_return_prefix = '' if pointer_wrapping_depth == 0 else 'return '
106103 pointer_wrapping_prefix += f'{ wrapper_return_prefix } withUnsafe{ mutability_infix } Pointer(to: { reference_prefix } { argument_name } .cOpaqueStruct!) {{ ({ passed_argument_name } : Unsafe{ mutability_infix } Pointer<{ current_argument_details .rust_obj } >) in\n '
107104 pointer_wrapping_suffix += '\n }'
108- # native_call_prep += current_prep
105+ # native_call_prep += current_prep
109106 elif is_cloneable :
110107 if not is_free_method :
111108 # clone_infix = '.clone()'
@@ -119,12 +116,13 @@ def prepare_swift_to_native_arguments(cls, argument_types, is_trait_callback = F
119116 pass
120117 elif current_argument_details .rust_obj .startswith ('LDK' ) and not current_argument_details .swift_type .startswith ('[' ) and not is_free_method :
121118 non_cloneable_argument_indices_passed_by_ownership .append (argument_index )
122- if current_argument_details .rust_obj is not None and ('Option' in current_argument_details .rust_obj or 'Tuple' in current_argument_details .rust_obj or 'Result' in current_argument_details .rust_obj ) and not current_argument_details .rust_obj .startswith ('[' ):
119+ if current_argument_details .rust_obj is not None and (
120+ 'Option' in current_argument_details .rust_obj or 'Tuple' in current_argument_details .rust_obj or 'Result' in current_argument_details .rust_obj ) and not current_argument_details .rust_obj .startswith (
121+ '[' ):
123122 # TODO: figure out potentially undetected cloneable types
124123 # print('Potentially undetected cloneable type?', current_argument_details.rust_obj)
125124 pass
126125
127-
128126 if is_trait_callback and current_argument_details .is_const :
129127 if current_argument_details .swift_type .startswith ('[' ) or current_argument_details .swift_type == 'String' :
130128 mutabilityIndicatorSuffix = '?'
@@ -139,15 +137,12 @@ def prepare_swift_to_native_arguments(cls, argument_types, is_trait_callback = F
139137 swift_argument_type = swift_argument_type .replace ('[LDKResult_' , '[LDKCResult_' ).replace ('[LDKTuple_' , '[LDKCTuple_' )
140138 swift_arguments .append (f'{ argument_name } : { swift_argument_type } { mutabilityIndicatorSuffix } ' )
141139
142-
143-
144140 # native_arguments.append(f'{passed_argument_name}')
145141 if current_argument_details .rust_obj == 'LDK' + swift_argument_type and not current_argument_details .is_ptr :
146142 native_arguments .append (f'{ passed_argument_name } { clone_infix } .cOpaqueStruct!' )
147143 elif current_argument_details .rust_obj == 'LDKC' + swift_argument_type and not current_argument_details .is_ptr :
148144 native_arguments .append (f'{ passed_argument_name } { clone_infix } .cOpaqueStruct!' )
149- elif current_argument_details .rust_obj is not None and current_argument_details .rust_obj .startswith (
150- 'LDK' ) and swift_argument_type .startswith ('[' ) and not is_pointer_to_array :
145+ elif current_argument_details .rust_obj is not None and current_argument_details .rust_obj .startswith ('LDK' ) and swift_argument_type .startswith ('[' ) and not is_pointer_to_array :
151146 # if current_argument_details.swift_type == '[UInt8]' and not current_argument_details.swift_raw_type.startswith('LDKC'):
152147 if current_argument_details .rust_obj in pointer_iterating_vector_types :
153148 # TODO: expand beyond 1-dimensional array support (as of writing only affects Route.swift)
@@ -172,8 +167,7 @@ def prepare_swift_to_native_arguments(cls, argument_types, is_trait_callback = F
172167 native_arguments .append (f'{ passed_argument_name } Wrapper.cOpaqueStruct!' )
173168 else :
174169 native_arguments .append (f'Bindings.new_{ current_argument_details .rust_obj } (array: { passed_argument_name } )' )
175- elif current_argument_details .rust_obj is not None and current_argument_details .rust_obj .startswith (
176- 'LDK' ) and current_argument_details .is_unary_tuple :
170+ elif current_argument_details .rust_obj is not None and current_argument_details .rust_obj .startswith ('LDK' ) and current_argument_details .is_unary_tuple :
177171 native_arguments .append (f'Bindings.new_{ current_argument_details .rust_obj } (array: { passed_argument_name } )' )
178172 elif swift_argument_type == 'String' :
179173 if is_trait_callback and current_argument_details .swift_raw_type == 'UnsafePointer<Int8>' :
@@ -200,15 +194,8 @@ def prepare_swift_to_native_arguments(cls, argument_types, is_trait_callback = F
200194
201195 full_syntax = pointer_wrapping_prefix + ', ' .join (native_arguments ) + pointer_wrapping_suffix
202196 # print(full_syntax)
203- return {
204- "swift_arguments" : swift_arguments ,
205- "native_arguments" : native_arguments ,
206- "native_call_prefix" : pointer_wrapping_prefix ,
207- "native_call_suffix" : pointer_wrapping_suffix ,
208- "native_call_prep" : native_call_prep ,
209- "static_eligible" : static_eligible ,
210- "non_cloneable_argument_indices_passed_by_ownership" : non_cloneable_argument_indices_passed_by_ownership
211- }
197+ return {"swift_arguments" : swift_arguments , "native_arguments" : native_arguments , "native_call_prefix" : pointer_wrapping_prefix , "native_call_suffix" : pointer_wrapping_suffix ,
198+ "native_call_prep" : native_call_prep , "static_eligible" : static_eligible , "non_cloneable_argument_indices_passed_by_ownership" : non_cloneable_argument_indices_passed_by_ownership }
212199
213200 # the arguments that we receive from a native lambda, before they get passed on to a human consumer
214201 # Traits -> NATIVE_CALLBACKS -> SWIFT_CALLBACK_PREP, basically
@@ -230,7 +217,7 @@ def prepare_native_to_swift_callback_arguments(cls, argument_types):
230217
231218 received_var_name = current_argument_details .var_name
232219
233- if received_var_name == 'init' : # illegal in swift
220+ if received_var_name == 'init' : # illegal in swift
234221 received_var_name = 'initValue'
235222 passed_var_name = received_var_name
236223 swift_callback_argument_value = received_var_name
@@ -264,7 +251,7 @@ def prepare_native_to_swift_callback_arguments(cls, argument_types):
264251 # Bindings array converters cannot yet convert LDK types to wrapped types
265252 published_swift_type = TypeParsingRegeces .WRAPPER_TYPE_ARRAY_BRACKET_REGEX .sub ('[LDK' , published_swift_type )
266253 received_raw_type = current_argument_details .rust_obj
267- # swift_callback_argument_value = f'{received_raw_type}(pointer: {received_var_name})'
254+ # swift_callback_argument_value = f'{received_raw_type}(pointer: {received_var_name})'
268255
269256 if received_raw_type is None :
270257 received_raw_type = inferred_raw_swift_type
@@ -278,9 +265,9 @@ def prepare_native_to_swift_callback_arguments(cls, argument_types):
278265 if current_argument_details .swift_type .startswith ('[' ) or current_argument_details .swift_type == 'String' :
279266 is_optional = True
280267 received_raw_type += '?' # TODO: figure out when tf it actually becomes nullable!
281- published_swift_type += '?' # the swift thing is obviously nullible, too
268+ published_swift_type += '?' # the swift thing is obviously nullible, too
282269
283- if is_unsafe_pointer : # always true
270+ if is_unsafe_pointer : # always true
284271 received_var_name += 'Pointer'
285272 if is_optional :
286273 if inferred_raw_swift_type == 'UnsafePointer<Int8>' :
@@ -314,15 +301,11 @@ def prepare_native_to_swift_callback_arguments(cls, argument_types):
314301 # native_arguments.insert(0, '')
315302 pass
316303
317- return {
318- 'raw_native_argument_list' : native_arguments ,
319- 'swift_callback_arguments' : swift_callback_arguments ,
320- 'public_swift_argument_list' : public_swift_argument_list ,
321- 'swift_callback_prep' : swift_callback_prep
322- }
304+ return {'raw_native_argument_list' : native_arguments , 'swift_callback_arguments' : swift_callback_arguments , 'public_swift_argument_list' : public_swift_argument_list ,
305+ 'swift_callback_prep' : swift_callback_prep }
323306
324307 @classmethod
325- def prepare_return_value (cls , return_type , is_clone_method = False , is_trait_instantiator = False , is_raw_property_getter = False ):
308+ def prepare_return_value (cls , return_type , is_clone_method = False , is_trait_instantiator = False , is_raw_property_getter = False ):
326309 rust_return_type = return_type .rust_obj
327310 return_prefix = ''
328311 return_suffix = ''
@@ -352,9 +335,9 @@ def prepare_return_value(cls, return_type, is_clone_method = False, is_trait_ins
352335 elif rust_return_type == 'LDKC' + return_type_string and not is_clone_method :
353336 return_prefix = f'{ swift_return_instantiation_type } (pointer: '
354337 return_suffix = ')'
355- # if is_trait_instantiator:
356- # only applies to tuples, but is never hit
357- # return_suffix = ', anchor: self)'
338+ # if is_trait_instantiator:
339+ # only applies to tuples, but is never hit
340+ # return_suffix = ', anchor: self)'
358341 elif return_type_string == 'String' :
359342 return_prefix = 'Bindings.LDKStr_to_string(nativeType: '
360343 return_suffix = ')'
@@ -366,10 +349,8 @@ def prepare_return_value(cls, return_type, is_clone_method = False, is_trait_ins
366349 pass
367350
368351 if TypeParsingRegeces .WRAPPER_TYPE_ARRAY_BRACKET_REGEX .search (return_type_string ):
352+ # replace the last [ with [LDK (in case
369353 return_type_string = TypeParsingRegeces .WRAPPER_TYPE_ARRAY_BRACKET_REGEX .sub ('[LDK' , return_type_string )
354+ return_type_string = return_type_string .replace ('LDKResult_' , 'LDKCResult_' ).replace ('LDKTuple_' , 'LDKCTuple_' ).replace ('LDKVec_' , 'LDKCVec_' )
370355
371- return {
372- 'prefix' : return_prefix ,
373- 'suffix' : return_suffix ,
374- 'swift_type' : return_type_string
375- }
356+ return {'prefix' : return_prefix , 'suffix' : return_suffix , 'swift_type' : return_type_string }
0 commit comments