Define a macro for constructing a TypeResolver for a given module
[ldk-c-bindings] / c-bindings-gen / src / main.rs
index bfadca0ed8dc415be471efa712a620ac21c3fedf..5bc27eb9b7317301d2bf0151854a2a50ab613d2e 100644 (file)
@@ -212,6 +212,18 @@ macro_rules! walk_supertraits { ($t: expr, $types: expr, ($( $($pat: pat)|* => $
        }
 } } }
 
+macro_rules! get_module_type_resolver {
+       ($module: expr, $crate_libs: expr, $crate_types: expr) => { {
+               let module: &str = &$module;
+               let mut module_iter = module.rsplitn(2, "::");
+               module_iter.next().unwrap();
+               let module = module_iter.next().unwrap();
+               let imports = ImportResolver::new(module.splitn(2, "::").next().unwrap(), &$crate_types.lib_ast.dependencies,
+                               module, &$crate_types.lib_ast.modules.get(module).unwrap().items);
+               TypeResolver::new(module, imports, $crate_types)
+       } }
+}
+
 /// Prints a C-mapped trait object containing a void pointer and a jump table for each function in
 /// the original trait.
 /// Implements the native Rust trait and relevant parent traits for the new C-mapped trait.
@@ -329,6 +341,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.
@@ -494,14 +513,16 @@ 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, "::");
-                               module_iter.next().unwrap();
-                               let supertrait_module = module_iter.next().unwrap();
-                               let imports = ImportResolver::new(supertrait_module.splitn(2, "::").next().unwrap(), &types.crate_types.lib_ast.dependencies,
-                                       supertrait_module, &types.crate_types.lib_ast.modules.get(supertrait_module).unwrap().items);
-                               let resolver = TypeResolver::new(&supertrait_module, imports, types.crate_types);
+                               let resolver = get_module_type_resolver!(s, types.crate_libs, types.crate_types);
                                writeln!(w, "impl {} for {} {{", s, trait_name).unwrap();
                                impl_trait_for_c!(supertrait, format!(".{}", i), &resolver);
                                writeln!(w, "}}").unwrap();
@@ -827,6 +848,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();