@@ -94,12 +94,15 @@ fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path
94
94
writeln ! ( w, ")" ) . unwrap ( ) ;
95
95
96
96
writeln ! ( w, "}}" ) . unwrap ( ) ;
97
+
98
+ writeln ! ( w, "#[allow(unused)]" ) . unwrap ( ) ;
99
+ writeln ! ( w, "pub(crate) extern \" C\" fn {}_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {{" , for_obj) . unwrap ( ) ;
97
100
if has_inner {
98
- writeln ! ( w, "#[no_mangle]" ) . unwrap ( ) ;
99
- writeln ! ( w, "pub(crate) extern \" C\" fn {}_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {{" , for_obj) . unwrap ( ) ;
100
101
writeln ! ( w, "\t crate::c_types::serialize_obj(unsafe {{ &*(obj as *const native{}) }})" , for_obj) . unwrap ( ) ;
101
- writeln ! ( w, "}}" ) . unwrap ( ) ;
102
+ } else {
103
+ writeln ! ( w, "\t {}_write(unsafe {{ &*(obj as *const {}) }})" , for_obj, for_obj) . unwrap ( ) ;
102
104
}
105
+ writeln ! ( w, "}}" ) . unwrap ( ) ;
103
106
} ,
104
107
"lightning::util::ser::Readable" |"lightning::util::ser::ReadableArgs" |"lightning::util::ser::MaybeReadable" => {
105
108
// Create the Result<Object, DecodeError> syn::Type
@@ -554,6 +557,7 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
554
557
writeln ! ( w, "unsafe impl Send for {} {{}}" , trait_name) . unwrap ( ) ;
555
558
writeln ! ( w, "unsafe impl Sync for {} {{}}" , trait_name) . unwrap ( ) ;
556
559
560
+ writeln ! ( w, "#[allow(unused)]" ) . unwrap ( ) ;
557
561
writeln ! ( w, "pub(crate) fn {}_clone_fields(orig: &{}) -> {} {{" , trait_name, trait_name, trait_name) . unwrap ( ) ;
558
562
writeln ! ( w, "\t {} {{" , trait_name) . unwrap ( ) ;
559
563
writeln ! ( w, "\t \t this_arg: orig.this_arg," ) . unwrap ( ) ;
@@ -1023,10 +1027,14 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
1023
1027
if is_type_unconstructable ( & resolved_path) {
1024
1028
writeln ! ( w, "\t \t unreachable!();" ) . unwrap ( ) ;
1025
1029
} else {
1026
- writeln ! ( w, "\t \t let mut rust_obj = {} {{ inner: ObjOps::heap_alloc(obj), is_owned: true }};" , ident) . unwrap ( ) ;
1027
- writeln ! ( w, "\t \t let mut ret = {}_as_{}(&rust_obj);" , ident, trait_obj. ident) . unwrap ( ) ;
1028
- writeln ! ( w, "\t \t // We want to free rust_obj when ret gets drop()'d, not rust_obj, so wipe rust_obj's pointer and set ret's free() fn" ) . unwrap ( ) ;
1029
- writeln ! ( w, "\t \t rust_obj.inner = core::ptr::null_mut();" ) . unwrap ( ) ;
1030
+ types. write_to_c_conversion_new_var ( w, & format_ident ! ( "obj" ) , & * i. self_ty , Some ( & gen_types) , false ) ;
1031
+ write ! ( w, "\t \t let rust_obj = " ) . unwrap ( ) ;
1032
+ types. write_to_c_conversion_inline_prefix ( w, & * i. self_ty , Some ( & gen_types) , false ) ;
1033
+ write ! ( w, "obj" ) . unwrap ( ) ;
1034
+ types. write_to_c_conversion_inline_suffix ( w, & * i. self_ty , Some ( & gen_types) , false ) ;
1035
+ writeln ! ( w, ";\n \t \t let mut ret = {}_as_{}(&rust_obj);" , ident, trait_obj. ident) . unwrap ( ) ;
1036
+ writeln ! ( w, "\t \t // We want to free rust_obj when ret gets drop()'d, not rust_obj, so forget it and set ret's free() fn" ) . unwrap ( ) ;
1037
+ writeln ! ( w, "\t \t core::mem::forget(rust_obj);" ) . unwrap ( ) ;
1030
1038
writeln ! ( w, "\t \t ret.free = Some({}_free_void);" , ident) . unwrap ( ) ;
1031
1039
writeln ! ( w, "\t \t ret" ) . unwrap ( ) ;
1032
1040
}
@@ -1041,7 +1049,11 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
1041
1049
writeln ! ( w, "/// This copies the `inner` pointer in this_arg and thus the returned {} must be freed before this_arg is" , trait_obj. ident) . unwrap ( ) ;
1042
1050
write ! ( w, "#[no_mangle]\n pub extern \" C\" fn {}_as_{}(this_arg: &{}) -> crate::{} {{\n " , ident, trait_obj. ident, ident, full_trait_path) . unwrap ( ) ;
1043
1051
writeln ! ( w, "\t crate::{} {{" , full_trait_path) . unwrap ( ) ;
1044
- writeln ! ( w, "\t \t this_arg: unsafe {{ ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void }}," ) . unwrap ( ) ;
1052
+ if types. c_type_has_inner_from_path ( & resolved_path) {
1053
+ writeln ! ( w, "\t \t this_arg: unsafe {{ ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void }}," ) . unwrap ( ) ;
1054
+ } else {
1055
+ writeln ! ( w, "\t \t this_arg: unsafe {{ ObjOps::untweak_ptr(this_arg as *const {} as *mut {}) as *mut c_void }}," , ident, ident) . unwrap ( ) ;
1056
+ }
1045
1057
writeln ! ( w, "\t \t free: None," ) . unwrap ( ) ;
1046
1058
1047
1059
macro_rules! write_meth {
@@ -1345,7 +1357,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
1345
1357
writeln ! ( w, "#[allow(unused)]" ) . unwrap ( ) ;
1346
1358
writeln ! ( w, "/// Used only if an object of this type is returned as a trait impl by a method" ) . unwrap ( ) ;
1347
1359
writeln ! ( w, "pub(crate) extern \" C\" fn {}_clone_void(this_ptr: *const c_void) -> *mut c_void {{" , ident) . unwrap ( ) ;
1348
- writeln ! ( w, "\t Box::into_raw(Box::new(unsafe {{ (*(this_ptr as *mut native{})).clone() }})) as *mut c_void" , ident) . unwrap ( ) ;
1360
+ writeln ! ( w, "\t Box::into_raw(Box::new(unsafe {{ (*(this_ptr as *const native{})).clone() }})) as *mut c_void" , ident) . unwrap ( ) ;
1349
1361
writeln ! ( w, "}}" ) . unwrap ( ) ;
1350
1362
writeln ! ( w, "#[no_mangle]" ) . unwrap ( ) ;
1351
1363
writeln ! ( w, "/// Creates a copy of the {}" , ident) . unwrap ( ) ;
@@ -1936,7 +1948,18 @@ fn writeln_enum<'a, 'b, W: std::io::Write>(w: &mut W, e: &'a syn::ItemEnum, type
1936
1948
writeln ! ( w, "pub extern \" C\" fn {}_clone(orig: &{}) -> {} {{" , e. ident, e. ident, e. ident) . unwrap ( ) ;
1937
1949
writeln ! ( w, "\t orig.clone()" ) . unwrap ( ) ;
1938
1950
writeln ! ( w, "}}" ) . unwrap ( ) ;
1951
+ writeln ! ( w, "#[allow(unused)]" ) . unwrap ( ) ;
1952
+ writeln ! ( w, "/// Used only if an object of this type is returned as a trait impl by a method" ) . unwrap ( ) ;
1953
+ writeln ! ( w, "pub(crate) extern \" C\" fn {}_clone_void(this_ptr: *const c_void) -> *mut c_void {{" , e. ident) . unwrap ( ) ;
1954
+ writeln ! ( w, "\t Box::into_raw(Box::new(unsafe {{ (*(this_ptr as *const {})).clone() }})) as *mut c_void" , e. ident) . unwrap ( ) ;
1955
+ writeln ! ( w, "}}" ) . unwrap ( ) ;
1939
1956
}
1957
+
1958
+ writeln ! ( w, "#[allow(unused)]" ) . unwrap ( ) ;
1959
+ writeln ! ( w, "/// Used only if an object of this type is returned as a trait impl by a method" ) . unwrap ( ) ;
1960
+ writeln ! ( w, "pub(crate) extern \" C\" fn {}_free_void(this_ptr: *mut c_void) {{" , e. ident) . unwrap ( ) ;
1961
+ writeln ! ( w, "\t let _ = unsafe {{ Box::from_raw(this_ptr as *mut {}) }};\n }}" , e. ident) . unwrap ( ) ;
1962
+
1940
1963
w. write_all ( & constr) . unwrap ( ) ;
1941
1964
write_cpp_wrapper ( cpp_headers, & format ! ( "{}" , e. ident) , needs_free, None ) ;
1942
1965
}
0 commit comments