From c4f2096c26abaa92505602604ca6e71c36c76817 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 26 Aug 2024 19:15:57 +0000 Subject: [PATCH] Handle writing Rust types for single-impl traits Single-impl traits which are marked no-export (like `AChannelManager` in LDK) are mapped as the singular implementing type, rather than a full-blown trait. Here we handle this case when writing Rust objects (generally used in writing generic bounds). --- c-bindings-gen/src/types.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index 51efa24..b8c8b4b 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -1983,11 +1983,19 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { // the original crate. write!(w, "{}", self.real_rust_type_mapping(&resolved)).unwrap(); } else { + if let Some(trait_impls) = self.crate_types.traits_impld.get(&resolved) { + if self.crate_types.traits.get(&resolved).is_none() && trait_impls.len() == 1 { + write!(w, "crate::{}", trait_impls[0]).unwrap(); + return; + } + } write!(w, "crate::{}", resolved).unwrap(); } } - if let syn::PathArguments::AngleBracketed(args) = &path.segments.iter().last().unwrap().arguments { - self.write_rust_generic_arg(w, generics_resolver, args.args.iter(), with_ref_lifetime); + if !generated_crate_ref { + if let syn::PathArguments::AngleBracketed(args) = &path.segments.iter().last().unwrap().arguments { + self.write_rust_generic_arg(w, generics_resolver, args.args.iter(), with_ref_lifetime); + } } } else { if path.leading_colon.is_some() { @@ -2051,7 +2059,11 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { } if let Some(resolved_ty) = self.maybe_resolve_path(&p.path, generics) { generate_crate_ref |= self.maybe_resolve_path(&p.path, None).as_ref() != Some(&resolved_ty); - if self.crate_types.traits.get(&resolved_ty).is_none() { generate_crate_ref = false; } + let mut is_trait = self.crate_types.traits.get(&resolved_ty).is_some(); + is_trait |= self.crate_types.traits_impld.get(&resolved_ty).is_some(); + if !is_trait { + generate_crate_ref = false; + } } self.write_rust_path(w, generics, &p.path, with_ref_lifetime, generate_crate_ref); }, -- 2.39.5