if (ty_info.rust_obj.replace("LDK", "") + "_clone") in self.clone_fns:
ret_conv = (ret_conv[0] + ty_info.rust_obj.replace("LDK", "") + "_clone(&", ");")
else:
- ret_conv = (ret_conv[0], "; // Warning: We likely need to clone here, but no clone is available for " + ty_info.rust_obj)
+ ret_conv = (ret_conv[0], ";\n// Warning: We likely need to clone here, but no clone is available, so we just do it for Java instances")
+ ret_conv = (ret_conv[0], ret_conv[1] + "" + self.consts.trait_struct_inc_refcnt(ty_info).replace(ty_info.var_name + "_conv", "(*" + ty_info.var_name + "_ret)"))
if not is_free:
needs_full_clone = not is_free and (not ty_info.is_ptr and not holds_ref or ty_info.requires_clone == True) and ty_info.requires_clone != False
if needs_full_clone and (ty_info.rust_obj.replace("LDK", "") + "_clone") in self.clone_fns:
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);",
to_hu_conv_name = "ret_hu_conv",
from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr", "this.ptrs_to.add(" + ty_info.var_name + ")"))
- needs_full_clone = not is_free and ((not ty_info.is_ptr and not holds_ref) or ty_info.requires_clone == True) and ty_info.requires_clone != False
+ needs_full_clone = not is_free and (not ty_info.is_ptr or ty_info.requires_clone == True) and ty_info.requires_clone != False
if needs_full_clone:
if "res" in ty_info.var_name: # XXX: This is a stupid hack
needs_full_clone = False
# underlying unlike Vecs, and it gives Java more freedom.
base_conv = base_conv + "\nFREE((void*)" + ty_info.var_name + ");"
if ty_info.rust_obj in self.complex_enums:
+ if needs_full_clone and (ty_info.rust_obj.replace("LDK", "") + "_clone") not in self.clone_fns:
+ # We really need a full clone here, but for now we just implement
+ # a manual clone explicitly for Option<Trait>s
+ if ty_info.rust_obj.startswith("LDKCOption"):
+ optional_ty = ty_info.rust_obj[11:-1]
+ if "LDK" + optional_ty in self.trait_structs:
+ base_conv += "\nif (" + ty_info.var_name + "_conv.tag == " + ty_info.rust_obj + "_Some) {"
+ base_conv += "\n\t// Manually implement clone for Java trait instances"
+ optional_ty_info = self.java_c_types("LDK" + optional_ty + " " + ty_info.var_name, None)
+ base_conv += self.consts.trait_struct_inc_refcnt(optional_ty_info).\
+ replace("\n", "\n\t").replace(ty_info.var_name + "_conv", ty_info.var_name + "_conv.some")
+ base_conv += "\n}"
ret_conv = ("uint64_t " + ty_info.var_name + "_ref = ((uint64_t)&", ") | 1;")
if not holds_ref:
ret_conv = (ty_info.rust_obj + " *" + ty_info.var_name + "_copy = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n", "")
elif ty_info.is_ptr:
assert(not is_free)
if ty_info.rust_obj in self.complex_enums:
+ ret_conv = ("uint64_t ret_" + ty_info.var_name + " = (uint64_t)", " | 1; // Warning: We should clone here!")
+ if ty_info.rust_obj.replace("LDK", "") + "_clone" in self.clone_fns:
+ ret_conv_pfx = ty_info.rust_obj + " *ret_" + ty_info.var_name + " = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + " ret conversion\");\n"
+ ret_conv_pfx += "*ret_" + ty_info.var_name + " = " + ty_info.rust_obj.replace("LDK", "") + "_clone("
+ ret_conv = (ret_conv_pfx, ");")
return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
arg_conv = ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)" + ty_info.var_name + ";",
arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
- ret_conv = ("uint64_t ret_" + ty_info.var_name + " = (uint64_t)", ";"), ret_conv_name = "ret_" + ty_info.var_name,
+ ret_conv = ret_conv, ret_conv_name = "(uint64_t)ret_" + ty_info.var_name,
to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_hu_conv = " + ty_info.java_hu_ty + ".constr_from_ptr(" + ty_info.var_name + ");",
to_hu_conv_name = ty_info.var_name + "_hu_conv",
from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr & ~1", "this.ptrs_to.add(" + ty_info.var_name + ")"))