Support mapping core::fmt::Debug as a supertrait
[ldk-c-bindings] / c-bindings-gen / src / main.rs
index ce69294928af16714f3e7b645d34cbc2faa9f510..9cd32feb8f7bfe6d1ebeee93e076cfa25c0bf82f 100644 (file)
@@ -163,7 +163,6 @@ fn write_trait_impl_field_assign<W: std::io::Write>(w: &mut W, trait_path: &str,
 
 /// Write out the impl block for a defined trait struct which has a supertrait
 fn do_write_impl_trait<W: std::io::Write>(w: &mut W, trait_path: &str, _trait_name: &syn::Ident, for_obj: &str) {
-eprintln!("{}", trait_path);
        match trait_path {
                "lightning::util::ser::Writeable" => {
                        writeln!(w, "impl {} for {} {{", trait_path, for_obj).unwrap();
@@ -330,6 +329,13 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
                                Some(format!("\t/**\n\t * {}\n\t * {}\n\t */\n", hash_docs_a, hash_docs_b))));
                },
                ("Send", _) => {}, ("Sync", _) => {},
+               ("std::fmt::Debug", _)|("core::fmt::Debug", _) => {
+                       let debug_docs = "Return a human-readable \"debug\" string describing this object";
+                       writeln!(w, "\t/// {}", debug_docs).unwrap();
+                       writeln!(w, "\tpub debug_str: extern \"C\" fn (this_arg: *const c_void) -> crate::c_types::Str,").unwrap();
+                       generated_fields.push(("debug_str".to_owned(), None,
+                               Some(format!("\t/**\n\t * {}\n\t */\n", debug_docs))));
+               },
                (s, i) => {
                        // TODO: Both of the below should expose supertrait methods in C++, but doing so is
                        // nontrivial.
@@ -495,6 +501,13 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
                        writeln!(w, "\t\t{}_clone(self)", trait_name).unwrap();
                        writeln!(w, "\t}}\n}}").unwrap();
                },
+               ("std::fmt::Debug", _)|("core::fmt::Debug", _) => {
+                       writeln!(w, "impl core::fmt::Debug for {} {{", trait_name).unwrap();
+                       writeln!(w, "\tfn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {{").unwrap();
+                       writeln!(w, "\t\tf.write_str((self.debug_str)(self.this_arg).into_str())").unwrap();
+                       writeln!(w, "\t}}").unwrap();
+                       writeln!(w, "}}").unwrap();
+               },
                (s, i) => {
                        if let Some(supertrait) = types.crate_types.traits.get(s) {
                                let mut module_iter = s.rsplitn(2, "::");
@@ -619,20 +632,19 @@ fn writeln_struct<'a, 'b, W: std::io::Write>(w: &mut W, s: &'a syn::ItemStruct,
                                }
 
                                if let Some(ident) = &field.ident {
-                                       let ref_type = syn::Type::Reference(syn::TypeReference {
-                                               and_token: syn::Token!(&)(Span::call_site()), lifetime: None, mutability: None,
-                                               elem: Box::new(field.ty.clone()) });
-                                       if types.understood_c_type(&ref_type, Some(&gen_types)) {
-                                               writeln_arg_docs(w, &field.attrs, "", types, Some(&gen_types), vec![].drain(..), Some(&ref_type));
-                                               write!(w, "#[no_mangle]\npub extern \"C\" fn {}_get_{}(this_ptr: &{}) -> ", struct_name, ident, struct_name).unwrap();
-                                               types.write_c_type(w, &ref_type, Some(&gen_types), true);
-                                               write!(w, " {{\n\tlet mut inner_val = &mut this_ptr.get_native_mut_ref().{};\n\t", ident).unwrap();
-                                               let local_var = types.write_to_c_conversion_new_var(w, &format_ident!("inner_val"), &ref_type, Some(&gen_types), true);
-                                               if local_var { write!(w, "\n\t").unwrap(); }
-                                               types.write_to_c_conversion_inline_prefix(w, &ref_type, Some(&gen_types), true);
-                                               write!(w, "inner_val").unwrap();
-                                               types.write_to_c_conversion_inline_suffix(w, &ref_type, Some(&gen_types), true);
-                                               writeln!(w, "\n}}").unwrap();
+                                       if let Some(ref_type) = types.create_ownable_reference(&field.ty, Some(&gen_types)) {
+                                               if types.understood_c_type(&ref_type, Some(&gen_types)) {
+                                                       writeln_arg_docs(w, &field.attrs, "", types, Some(&gen_types), vec![].drain(..), Some(&ref_type));
+                                                       write!(w, "#[no_mangle]\npub extern \"C\" fn {}_get_{}(this_ptr: &{}) -> ", struct_name, ident, struct_name).unwrap();
+                                                       types.write_c_type(w, &ref_type, Some(&gen_types), true);
+                                                       write!(w, " {{\n\tlet mut inner_val = &mut this_ptr.get_native_mut_ref().{};\n\t", ident).unwrap();
+                                                       let local_var = types.write_to_c_conversion_new_var(w, &format_ident!("inner_val"), &ref_type, Some(&gen_types), true);
+                                                       if local_var { write!(w, "\n\t").unwrap(); }
+                                                       types.write_to_c_conversion_inline_prefix(w, &ref_type, Some(&gen_types), true);
+                                                       write!(w, "inner_val").unwrap();
+                                                       types.write_to_c_conversion_inline_suffix(w, &ref_type, Some(&gen_types), true);
+                                                       writeln!(w, "\n}}").unwrap();
+                                               }
                                        }
 
                                        if types.understood_c_type(&field.ty, Some(&gen_types)) {
@@ -829,6 +841,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
                                                        },
                                                        ("Sync", _) => {}, ("Send", _) => {},
                                                        ("std::marker::Sync", _) => {}, ("std::marker::Send", _) => {},
+                                                       ("core::fmt::Debug", _) => {},
                                                        (s, t) => {
                                                                if let Some(supertrait_obj) = types.crate_types.traits.get(s) {
                                                                        writeln!(w, "\t\t{}: crate::{} {{", t, s).unwrap();