Skip to content

Commit 91967f0

Browse files
committed
Handle LDKStr traits and nested array return types
1 parent 53c596a commit 91967f0

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

bindings/LDK/Bindings.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4539,10 +4539,10 @@ withUnsafePointer(to: htlc.cOpaqueStruct!) { (htlcPointer: UnsafePointer<LDKHTLC
45394539
return UnsafePointer<UInt8>(dataMutablePointer)
45404540
}
45414541

4542-
public class func new_LDKStr(string: String) -> LDKStr {
4543-
let nativeType = Self.string_to_unsafe_uint8_pointer(string: string)
4544-
return LDKStr(chars: nativeType, len: UInt(string.count), chars_is_owned: false)
4545-
}
4542+
public class func new_LDKStr(string: String, chars_is_owned: Bool = false) -> LDKStr {
4543+
let nativeType = Self.string_to_unsafe_uint8_pointer(string: string)
4544+
return LDKStr(chars: nativeType, len: UInt(string.count), chars_is_owned: chars_is_owned)
4545+
}
45464546

45474547
public class func createInvoiceFromChannelManager(channelManager: ChannelManager, keysManager: KeysInterface, network: LDKCurrency, amountMsat: UInt64?, description: String) -> Result_InvoiceSignOrCreationErrorZ {
45484548
let nativeKeysManager = keysManager.cOpaqueStruct!

src/conversion_helper.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,28 +372,37 @@ def prepare_return_value(cls, return_type, is_clone_method=False, is_trait_insta
372372
if rust_return_type is not None and rust_return_type.startswith('LDK') and return_type_string.startswith('['):
373373
return_prefix = f'Bindings.{rust_return_type}_to_array(nativeType: '
374374
return_suffix = ')'
375-
map_suffix = ''
375+
individual_map_suffix = ''
376376
if (rust_return_type.startswith('LDKCVec_') or rust_return_type == 'LDKTransaction') and is_raw_property_getter:
377377
return_suffix = ', deallocate: false)'
378-
map_suffix = '.dangle()'
378+
individual_map_suffix = '.dangle()'
379379

380380
if TypeParsingRegeces.WRAPPER_TYPE_ARRAY_BRACKET_REGEX.search(return_type_string) and not is_trait_callback:
381381
# replace the last [ with [LDK (in case
382382
constructor_type = return_type_string.lstrip('[').rstrip(']')
383383
# native_return_type_string = TypeParsingRegeces.WRAPPER_TYPE_ARRAY_BRACKET_REGEX.sub('[LDK', return_type_string)
384384
# native_return_type_string = return_type_string.replace('LDKResult_', 'LDKCResult_').replace('LDKTuple_', 'LDKCTuple_').replace('LDKVec_', 'LDKCVec_')
385385

386+
wrapper_depth = int((len(return_type_string) - len(constructor_type)) / 2)
387+
386388
if constructor_type == 'Txid':
387389
return_suffix += f'''
388390
.map {{ (bytes) in
389391
Bindings.LDKThirtyTwoBytes_to_array(nativeType: bytes)
390392
}}
391393
'''
392394
else:
395+
396+
map_prefix = f'''.map {{ (cOpaqueStruct) in
397+
cOpaqueStruct''' * (wrapper_depth-1)
398+
399+
map_suffix = '}' * wrapper_depth
400+
393401
return_suffix += f'''
402+
{map_prefix}
394403
.map {{ (cOpaqueStruct) in
395-
{constructor_type}(pointer: cOpaqueStruct){map_suffix}
396-
}}
404+
{constructor_type}(pointer: cOpaqueStruct){individual_map_suffix}
405+
{map_suffix}
397406
'''
398407

399408

src/generators/trait_generator.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ def generate_trait(self, struct_name, struct_details):
176176
swift_default_return = 'return 0'
177177
if swift_return_type.startswith('Bool'):
178178
swift_default_return = 'return false'
179+
if swift_raw_return_type == 'String' and swift_return_type == 'String':
180+
return_conversion_prefix = f'Bindings.new_LDKStr(string: '
181+
return_conversion_suffix = ', chars_is_owned: true)'
182+
swift_raw_return_type = 'LDKStr'
183+
swift_default_return = ''
184+
is_default_return_error = True
179185
if swift_return_type.startswith('UnsafeMutableRawPointer'):
180186
swift_raw_return_type += '?'
181187
swift_default_return = 'return UnsafeMutableRawPointer(bitPattern: 0)!'

templates/BindingsTemplate.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,10 @@ public class Bindings {
346346
return UnsafePointer<UInt8>(dataMutablePointer)
347347
}
348348

349-
public class func new_LDKStr(string: String) -> LDKStr {
350-
let nativeType = Self.string_to_unsafe_uint8_pointer(string: string)
351-
return LDKStr(chars: nativeType, len: UInt(string.count), chars_is_owned: false)
352-
}
349+
public class func new_LDKStr(string: String, chars_is_owned: Bool = false) -> LDKStr {
350+
let nativeType = Self.string_to_unsafe_uint8_pointer(string: string)
351+
return LDKStr(chars: nativeType, len: UInt(string.count), chars_is_owned: chars_is_owned)
352+
}
353353

354354
public class func createInvoiceFromChannelManager(channelManager: ChannelManager, keysManager: KeysInterface, network: LDKCurrency, amountMsat: UInt64?, description: String) -> Result_InvoiceSignOrCreationErrorZ {
355355
let nativeKeysManager = keysManager.cOpaqueStruct!

0 commit comments

Comments
 (0)