[bindings] Use resolved, not local ident in generic mangling
[rust-lightning] / c-bindings-gen / src / main.rs
index d927d2ae71c816af9f92ee824861103715117d0f..02521503bb12d523c955363d45e87051be35f43b 100644 (file)
@@ -559,7 +559,8 @@ fn writeln_opaque<W: std::io::Write>(w: &mut W, ident: &syn::Ident, struct_name:
                writeln!(w, "impl Clone for {} {{", struct_name).unwrap();
                writeln!(w, "\tfn clone(&self) -> Self {{").unwrap();
                writeln!(w, "\t\tSelf {{").unwrap();
-               writeln!(w, "\t\t\tinner: Box::into_raw(Box::new(unsafe {{ &*self.inner }}.clone())),").unwrap();
+               writeln!(w, "\t\t\tinner: if self.inner.is_null() {{ std::ptr::null_mut() }} else {{").unwrap();
+               writeln!(w, "\t\t\t\tBox::into_raw(Box::new(unsafe {{ &*self.inner }}.clone())) }},").unwrap();
                writeln!(w, "\t\t\tis_owned: true,").unwrap();
                writeln!(w, "\t\t}}\n\t}}\n}}").unwrap();
                writeln!(w, "#[allow(unused)]").unwrap();
@@ -569,7 +570,7 @@ fn writeln_opaque<W: std::io::Write>(w: &mut W, ident: &syn::Ident, struct_name:
                writeln!(w, "}}").unwrap();
                writeln!(w, "#[no_mangle]").unwrap();
                writeln!(w, "pub extern \"C\" fn {}_clone(orig: &{}) -> {} {{", struct_name, struct_name, struct_name).unwrap();
-               writeln!(w, "\t{} {{ inner: Box::into_raw(Box::new(unsafe {{ &*orig.inner }}.clone())), is_owned: true }}", struct_name).unwrap();
+               writeln!(w, "\torig.clone()").unwrap();
                writeln!(w, "}}").unwrap();
        }
 
@@ -587,7 +588,7 @@ fn declare_struct<'a, 'b>(s: &'a syn::ItemStruct, types: &mut TypeResolver<'b, '
                }
        }
 
-       types.struct_imported(&s.ident, format!("{}", s.ident));
+       types.struct_imported(&s.ident);
        true
 }
 
@@ -600,7 +601,6 @@ fn writeln_struct<'a, 'b, W: std::io::Write>(w: &mut W, s: &'a syn::ItemStruct,
        let struct_name = &format!("{}", s.ident);
        writeln_opaque(w, &s.ident, struct_name, &s.generics, &s.attrs, types, extra_headers, cpp_headers);
 
-       eprintln!("exporting fields for {}", struct_name);
        if let syn::Fields::Named(fields) = &s.fields {
                let mut gen_types = GenericTypes::new();
                assert!(gen_types.learn_generics(&s.generics, types));