From 936e4d0f3a29469f3614ca1082fb94ff8f5e5dd2 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 31 Oct 2021 18:09:31 +0000 Subject: [PATCH] impl-Trait on in-crate types in a diff mod from the type definition --- c-bindings-gen/src/main.rs | 12 +++++++++--- c-bindings-gen/src/types.rs | 6 +++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index d490a89..ea624e5 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -592,7 +592,7 @@ fn writeln_opaque(w: &mut W, ident: &syn::Ident, struct_name: // If we directly read the original type by its original name, cbindgen hits // https://github.com/eqrion/cbindgen/issues/286 Thus, instead, we import it as a temporary // name and then reference it by that name, which works around the issue. - write!(w, "\nuse {}::{} as native{}Import;\ntype native{} = native{}Import", types.module_path, ident, ident, ident, ident).unwrap(); + write!(w, "\nuse {}::{} as native{}Import;\npub(crate) type native{} = native{}Import", types.module_path, ident, ident, ident, ident).unwrap(); maybe_write_generics(w, &generics, &types, true); writeln!(w, ";\n").unwrap(); writeln!(extra_headers, "struct native{}Opaque;\ntypedef struct native{}Opaque LDKnative{};", ident, ident, ident).unwrap(); @@ -614,7 +614,7 @@ fn writeln_opaque(w: &mut W, ident: &syn::Ident, struct_name: writeln!(w, "#[no_mangle]\npub extern \"C\" fn {}_free(this_obj: {}) {{ }}", struct_name, struct_name).unwrap(); writeln!(w, "#[allow(unused)]").unwrap(); writeln!(w, "/// Used only if an object of this type is returned as a trait impl by a method").unwrap(); - writeln!(w, "extern \"C\" fn {}_free_void(this_ptr: *mut c_void) {{", struct_name).unwrap(); + writeln!(w, "pub(crate) extern \"C\" fn {}_free_void(this_ptr: *mut c_void) {{", struct_name).unwrap(); writeln!(w, "\tunsafe {{ let _ = Box::from_raw(this_ptr as *mut native{}); }}\n}}", struct_name).unwrap(); writeln!(w, "#[allow(unused)]").unwrap(); writeln!(w, "impl {} {{", struct_name).unwrap(); @@ -829,7 +829,13 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ // properly. This way we can call this method from deep in the // type-conversion logic without actually knowing the concrete native type. if !resolved_path.starts_with(types.module_path) { - writeln!(w, "use {} as native{};", resolved_path, ident).unwrap(); + if !first_seg_is_stdlib(resolved_path.split("::").next().unwrap()) { + writeln!(w, "use crate::{}::native{} as native{};", resolved_path.rsplitn(2, "::").skip(1).next().unwrap(), ident, ident).unwrap(); + writeln!(w, "use crate::{};", resolved_path).unwrap(); + writeln!(w, "use crate::{}_free_void;", resolved_path).unwrap(); + } else { + writeln!(w, "use {} as native{};", resolved_path, ident).unwrap(); + } } writeln!(w, "impl From for crate::{} {{", ident, full_trait_path).unwrap(); writeln!(w, "\tfn from(obj: native{}) -> Self {{", ident).unwrap(); diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index 224b3ec..5d36ed5 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -46,6 +46,10 @@ pub fn get_single_remaining_path_seg<'a, I: Iterator> } else { None } } +pub fn first_seg_is_stdlib(first_seg_str: &str) -> bool { + first_seg_str == "std" || first_seg_str == "core" || first_seg_str == "alloc" +} + pub fn single_ident_generic_path_to_ident(p: &syn::Path) -> Option<&syn::Ident> { if p.segments.len() == 1 { Some(&p.segments.iter().next().unwrap().ident) @@ -600,7 +604,7 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr } } else if let Some(_) = self.priv_modules.get(&first_seg.ident) { Some(format!("{}::{}{}", self.module_path, first_seg.ident, remaining)) - } else if first_seg_str == "std" || first_seg_str == "core" || self.dependencies.contains(&first_seg.ident) { + } else if first_seg_is_stdlib(&first_seg_str) || self.dependencies.contains(&first_seg.ident) { Some(first_seg_str + &remaining) } else { None } } -- 2.30.2