]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Support serialization impls defined in a diff module from the type
authorMatt Corallo <git@bluematt.me>
Mon, 26 Aug 2024 18:49:30 +0000 (18:49 +0000)
committerMatt Corallo <git@bluematt.me>
Mon, 26 Aug 2024 23:31:36 +0000 (23:31 +0000)
c-bindings-gen/src/main.rs

index dce1f876ceb1020025f42a7a30a127ca5decf656..85036a20c55de6ba0f2638d1ada51e192178bf08 100644 (file)
@@ -62,18 +62,22 @@ fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path
        if let Some(t) = types.maybe_resolve_path(&trait_path, Some(generics)) {
                let for_obj;
                let full_obj_path;
+               let native_path;
                let mut has_inner = false;
                if let syn::Type::Path(ref p) = for_ty {
                        let resolved_path = types.resolve_path(&p.path, Some(generics));
                        for_obj = format!("{}", p.path.segments.last().unwrap().ident);
                        full_obj_path = format!("crate::{}", resolved_path);
                        has_inner = types.c_type_has_inner_from_path(&resolved_path);
+                       let (path, name) = full_obj_path.rsplit_once("::").unwrap();
+                       native_path = path.to_string() + "::native" + name;
                } else {
                        // We assume that anything that isn't a Path is somehow a generic that ends up in our
                        // derived-types module.
                        let mut for_obj_vec = Vec::new();
                        types.write_c_type(&mut for_obj_vec, for_ty, Some(generics), false);
                        full_obj_path = String::from_utf8(for_obj_vec).unwrap();
+                       native_path = full_obj_path.clone();
                        if !full_obj_path.starts_with(TypeResolver::generated_container_path()) { return; }
                        for_obj = full_obj_path[TypeResolver::generated_container_path().len() + 2..].into();
                }
@@ -98,7 +102,7 @@ fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path
                                writeln!(w, "#[allow(unused)]").unwrap();
                                writeln!(w, "pub(crate) extern \"C\" fn {}_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {{", for_obj).unwrap();
                                if has_inner {
-                                       writeln!(w, "\tcrate::c_types::serialize_obj(unsafe {{ &*(obj as *const native{}) }})", for_obj).unwrap();
+                                       writeln!(w, "\tcrate::c_types::serialize_obj(unsafe {{ &*(obj as *const {}) }})", native_path).unwrap();
                                } else {
                                        writeln!(w, "\t{}_write(unsafe {{ &*(obj as *const {}) }})", for_obj, for_obj).unwrap();
                                }