Skip to content

Commit 1b870a3

Browse files
authored
Merge pull request #5 from TheBlueMatt/main
Add docs and fix handling of `Option<Trait>`
2 parents 246459d + 1711355 commit 1b870a3

File tree

207 files changed

+7351
-1690
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

207 files changed

+7351
-1690
lines changed

bindingstypes.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class TypeInfo:
2-
def __init__(self, is_native_primitive, rust_obj, java_ty, java_fn_ty_arg, java_hu_ty, c_ty, is_const, passed_as_ptr, is_ptr, var_name, arr_len, arr_access, subty=None):
2+
def __init__(self, is_native_primitive, rust_obj, java_ty, java_fn_ty_arg, java_hu_ty, c_ty, is_const, passed_as_ptr, is_ptr, nonnull_ptr, var_name, arr_len, arr_access, subty=None):
33
self.is_native_primitive = is_native_primitive
44
self.rust_obj = rust_obj
55
self.java_ty = java_ty
@@ -9,6 +9,7 @@ def __init__(self, is_native_primitive, rust_obj, java_ty, java_fn_ty_arg, java_
99
self.is_const = is_const
1010
self.passed_as_ptr = passed_as_ptr
1111
self.is_ptr = is_ptr
12+
self.nonnull_ptr = nonnull_ptr
1213
self.var_name = var_name
1314
self.arr_len = arr_len
1415
self.arr_access = arr_access
@@ -66,11 +67,12 @@ def __init__(self, ty_info, arg_name, arg_conv, arg_conv_name, arg_conv_cleanup,
6667
self.from_hu_conv = from_hu_conv
6768

6869
class TraitMethInfo:
69-
def __init__(self, fn_name, self_is_const, ret_ty_info, args_ty):
70+
def __init__(self, fn_name, self_is_const, ret_ty_info, args_ty, docs):
7071
self.fn_name = fn_name
7172
self.self_is_const = self_is_const
7273
self.ret_ty_info = ret_ty_info
7374
self.args_ty = args_ty
75+
self.docs = docs
7476

7577
class ComplexEnumVariantInfo:
7678
def __init__(self, var_name, fields):

gen_type_mapping.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,24 @@ def map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, holds_re
414414
to_hu_conv_name = ty_info.var_name + "_hu_conv",
415415
from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr & ~1", "this.ptrs_to.add(" + ty_info.var_name + ")"))
416416
elif ty_info.rust_obj in self.trait_structs:
417+
if ty_info.nonnull_ptr:
418+
arg_conv = ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)(((uint64_t)" + ty_info.var_name + ") & ~1);"
419+
arg_conv_name = ty_info.var_name + "_conv"
420+
else:
421+
# We map Option<Trait> as *mut Trait, which we can differentiate from &Trait by the NONNULL_PTR annotation.
422+
# We handle the Option<Trait> case here.
423+
arg_conv = ty_info.rust_obj + " *" + ty_info.var_name + "_conv_ptr = NULL;\n"
424+
arg_conv += "if (" + ty_info.var_name + " != 0) {\n"
425+
arg_conv += "\t" + ty_info.rust_obj + " " + ty_info.var_name + "_conv;\n"
426+
arg_conv += "\t" + ty_info.var_name + "_conv = *(" + ty_info.rust_obj + "*)(((uint64_t)" + ty_info.var_name + ") & ~1);"
427+
arg_conv += self.consts.trait_struct_inc_refcnt(ty_info).replace("\n", "\n\t")
428+
arg_conv += "\n\t" + ty_info.var_name + "_conv_ptr = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n"
429+
arg_conv += "\t*" + ty_info.var_name + "_conv_ptr = " + ty_info.var_name + "_conv;\n"
430+
arg_conv += "}"
431+
arg_conv_name = ty_info.var_name + "_conv_ptr"
417432
if ty_info.rust_obj.replace("LDK", "") + "_clone" in self.clone_fns:
418433
return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
419-
arg_conv = ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)" + ty_info.var_name + ";",
420-
arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
434+
arg_conv = arg_conv, arg_conv_name = arg_conv_name, arg_conv_cleanup = None,
421435
ret_conv = (ty_info.rust_obj + " *" + ty_info.var_name + "_clone = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n" +
422436
"*" + ty_info.var_name + "_clone = " + ty_info.rust_obj.replace("LDK", "") + "_clone(", ");"),
423437
ret_conv_name = "(long)" + ty_info.var_name + "_clone",
@@ -426,8 +440,7 @@ def map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, holds_re
426440
from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr", "this.ptrs_to.add(" + ty_info.var_name + ")"))
427441
else:
428442
return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
429-
arg_conv = ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)" + ty_info.var_name + ";",
430-
arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
443+
arg_conv = arg_conv, arg_conv_name = arg_conv_name, arg_conv_cleanup = None,
431444
ret_conv = ("long ret_" + ty_info.var_name + " = (long)", ";"), ret_conv_name = "ret_" + ty_info.var_name,
432445
to_hu_conv = ty_info.java_hu_ty + " ret_hu_conv = new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");\nret_hu_conv.ptrs_to.add(this);",
433446
to_hu_conv_name = "ret_hu_conv",

0 commit comments

Comments
 (0)