Update C/C++ demo to latest LDK API
[ldk-c-bindings] / c-bindings-gen / src / main.rs
index b72adca348e7056a5ee46500dc2ab3954f4db75b..fd4ea8f4f95705f8e14f4668e4284052d2de69c7 100644 (file)
@@ -1101,7 +1101,9 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
                                                        },
                                                        ("Sync", _, _) => {}, ("Send", _, _) => {},
                                                        ("std::marker::Sync", _, _) => {}, ("std::marker::Send", _, _) => {},
-                                                       ("core::fmt::Debug", _, _) => {},
+                                                       ("core::fmt::Debug", _, _) => {
+                                                               writeln!(w, "\t\tdebug_str: {}_debug_str_void,", ident).unwrap();
+                                                       },
                                                        (s, t, _) => {
                                                                if let Some(supertrait_obj) = types.crate_types.traits.get(s) {
                                                                        macro_rules! write_impl_fields {
@@ -1183,28 +1185,29 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
                                                                        write_method_var_decl_body(w, &$trait_meth.sig, "", &mut trait_resolver, Some(&meth_gen_types), false);
                                                                        let mut takes_self = false;
                                                                        for inp in $m.sig.inputs.iter() {
-                                                                               if let syn::FnArg::Receiver(_) = inp {
-                                                                                       takes_self = true;
+                                                                               match inp {
+                                                                                       syn::FnArg::Receiver(_) => {
+                                                                                               takes_self = true;
+                                                                                               break;
+                                                                                       },
+                                                                                       syn::FnArg::Typed(ty) => {
+                                                                                               if let syn::Pat::Ident(id) = &*ty.pat {
+                                                                                                       if format!("{}", id.ident) == "self" {
+                                                                                                               takes_self = true;
+                                                                                                               break;
+                                                                                                       }
+                                                                                               }
+                                                                                       }
                                                                                }
                                                                        }
 
-                                                                       let mut t_gen_args = String::new();
-                                                                       for (idx, _) in $trait.generics.params.iter().enumerate() {
-                                                                               if idx != 0 { t_gen_args += ", " };
-                                                                               t_gen_args += "_"
-                                                                       }
-                                                                       // rustc doesn't like <_> if the _ is actually a lifetime, so
-                                                                       // if all the parameters are lifetimes just skip it.
-                                                                       let mut nonlifetime_param = false;
-                                                                       for param in $trait.generics.params.iter() {
-                                                                               if let syn::GenericParam::Lifetime(_) = param {}
-                                                                               else { nonlifetime_param = true; }
-                                                                       }
-                                                                       if !nonlifetime_param { t_gen_args = String::new(); }
+                                                                       let mut t_gen_args_vec = Vec::new();
+                                                                       maybe_write_type_non_lifetime_generics(&mut t_gen_args_vec, &$trait.generics, &trait_resolver);
+                                                                       let t_gen_args = String::from_utf8(t_gen_args_vec).unwrap();
                                                                        if takes_self {
-                                                                               write!(w, "<native{} as {}<{}>>::{}(unsafe {{ &mut *(this_arg as *mut native{}) }}, ", ident, $trait_path, t_gen_args, $m.sig.ident, ident).unwrap();
+                                                                               write!(w, "<native{} as {}{}>::{}(unsafe {{ &mut *(this_arg as *mut native{}) }}, ", ident, $trait_path, t_gen_args, $m.sig.ident, ident).unwrap();
                                                                        } else {
-                                                                               write!(w, "<native{} as {}<{}>>::{}(", ident, $trait_path, t_gen_args, $m.sig.ident).unwrap();
+                                                                               write!(w, "<native{} as {}{}>::{}(", ident, $trait_path, t_gen_args, $m.sig.ident).unwrap();
                                                                        }
 
                                                                        let mut real_type = "".to_string();
@@ -1298,8 +1301,8 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
                                                write!(w, "#[must_use]\n#[no_mangle]\npub extern \"C\" fn {}_default() -> {} {{\n", ident, ident).unwrap();
                                                write!(w, "\t{} {{ inner: ObjOps::heap_alloc(Default::default()), is_owned: true }}\n", ident).unwrap();
                                                write!(w, "}}\n").unwrap();
-                                       } else if path_matches_nongeneric(&trait_path.1, &["core", "cmp", "PartialEq"]) {
-                                       } else if path_matches_nongeneric(&trait_path.1, &["core", "cmp", "Eq"]) {
+                                       } else if full_trait_path_opt.as_ref().map(|s| s.as_str()) == Some("core::cmp::PartialEq") {
+                                       } else if full_trait_path_opt.as_ref().map(|s| s.as_str()) == Some("core::cmp::Eq") {
                                                writeln!(w, "/// Checks if two {}s contain equal inner contents.", ident).unwrap();
                                                writeln!(w, "/// This ignores pointers and is_owned flags and looks at the values in fields.").unwrap();
                                                if types.c_type_has_inner_from_path(&resolved_path) {
@@ -1325,7 +1328,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
                                                types.write_from_c_conversion_suffix(w, &ref_type, Some(&gen_types));
 
                                                writeln!(w, " {{ true }} else {{ false }}\n}}").unwrap();
-                                       } else if path_matches_nongeneric(&trait_path.1, &["core", "hash", "Hash"]) {
+                                       } else if full_trait_path_opt.as_ref().map(|s| s.as_str()) == Some("core::hash::Hash") {
                                                writeln!(w, "/// Generates a non-cryptographic 64-bit hash of the {}.", ident).unwrap();
                                                write!(w, "#[no_mangle]\npub extern \"C\" fn {}_hash(o: &{}) -> u64 {{\n", ident, ident).unwrap();
                                                if types.c_type_has_inner_from_path(&resolved_path) {
@@ -1345,8 +1348,8 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
                                                types.write_from_c_conversion_suffix(w, &ref_type, Some(&gen_types));
                                                writeln!(w, ", &mut hasher);").unwrap();
                                                writeln!(w, "\tcore::hash::Hasher::finish(&hasher)\n}}").unwrap();
-                                       } else if (path_matches_nongeneric(&trait_path.1, &["core", "clone", "Clone"]) || path_matches_nongeneric(&trait_path.1, &["Clone"])) &&
-                                                       types.c_type_has_inner_from_path(&resolved_path) {
+                                       } else if (full_trait_path_opt.as_ref().map(|s| s.as_str()) == Some("core::clone::Clone") || path_matches_nongeneric(&trait_path.1, &["Clone"])) &&
+                                       types.c_type_has_inner_from_path(&resolved_path) {
                                                writeln!(w, "impl Clone for {} {{", ident).unwrap();
                                                writeln!(w, "\tfn clone(&self) -> Self {{").unwrap();
                                                writeln!(w, "\t\tSelf {{").unwrap();
@@ -1399,6 +1402,12 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
 
                                                        writeln!(w, "\t}}.into()\n}}").unwrap();
                                                }
+                                       } else if full_trait_path_opt.as_ref().map(|s| s.as_str()) == Some("core::fmt::Debug") {
+                                               writeln!(w, "/// Get a string which allows debug introspection of a {} object", ident).unwrap();
+                                               writeln!(w, "pub extern \"C\" fn {}_debug_str_void(o: *const c_void) -> Str {{", ident).unwrap();
+
+                                               write!(w, "\talloc::format!(\"{{:?}}\", unsafe {{ o as *const crate::{} }}).into()", resolved_path).unwrap();
+                                               writeln!(w, "}}").unwrap();
                                        } else if path_matches_nongeneric(&trait_path.1, &["Display"]) {
                                                writeln!(w, "#[no_mangle]").unwrap();
                                                writeln!(w, "/// Get the string representation of a {} object", ident).unwrap();
@@ -1448,10 +1457,23 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
                                                                        let mut takes_mut_self = false;
                                                                        let mut takes_owned_self = false;
                                                                        for inp in m.sig.inputs.iter() {
-                                                                               if let syn::FnArg::Receiver(r) = inp {
-                                                                                       takes_self = true;
-                                                                                       if r.mutability.is_some() { takes_mut_self = true; }
-                                                                                       if r.reference.is_none() { takes_owned_self = true; }
+                                                                               match inp {
+                                                                                       syn::FnArg::Receiver(r) => {
+                                                                                               takes_self = true;
+                                                                                               if r.mutability.is_some() { takes_mut_self = true; }
+                                                                                               if r.reference.is_none() { takes_owned_self = true; }
+                                                                                               break;
+                                                                                       },
+                                                                                       syn::FnArg::Typed(ty) => {
+                                                                                               if let syn::Pat::Ident(id) = &*ty.pat {
+                                                                                                       if format!("{}", id.ident) == "self" {
+                                                                                                               takes_self = true;
+                                                                                                               if id.mutability.is_some() { takes_mut_self = true; }
+                                                                                                               if id.by_ref.is_none() { takes_owned_self = true; }
+                                                                                                               break;
+                                                                                                       }
+                                                                                               }
+                                                                                       }
                                                                                }
                                                                        }
                                                                        if !takes_mut_self && !takes_self {