]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Write out real generics when calling trait methods
authorMatt Corallo <git@bluematt.me>
Thu, 25 Apr 2024 20:08:27 +0000 (20:08 +0000)
committerMatt Corallo <git@bluematt.me>
Sun, 12 May 2024 14:34:00 +0000 (14:34 +0000)
This avoids issues where a trait has a generic which can't be
resolved.

c-bindings-gen/src/blocks.rs
c-bindings-gen/src/main.rs

index d01da1c71559d8965b13d745abf15b4699ae3a07..dc160d2ef0f2ce0f3dd2f58d4e390de49c048a7f 100644 (file)
@@ -780,14 +780,18 @@ pub fn write_method_call_params<W: std::io::Write>(w: &mut W, sig: &syn::Signatu
 /// Prints concrete generic parameters for a struct/trait/function, including the less-than and
 /// greater-than symbols, if any generic parameters are defined.
 pub fn maybe_write_generics<W: std::io::Write>(w: &mut W, generics: &syn::Generics, generics_impld: &syn::PathArguments, types: &TypeResolver, concrete_lifetimes: bool) {
-       maybe_write_generics_intern(w, generics, generics_impld, types, concrete_lifetimes, false);
+       maybe_write_generics_intern(w, generics, Some(generics_impld), types, concrete_lifetimes, false);
 }
 
 pub fn maybe_write_non_lifetime_generics<W: std::io::Write>(w: &mut W, generics: &syn::Generics, generics_impld: &syn::PathArguments, types: &TypeResolver) {
-       maybe_write_generics_intern(w, generics, generics_impld, types, false, true);
+       maybe_write_generics_intern(w, generics, Some(generics_impld), types, false, true);
 }
 
-fn maybe_write_generics_intern<W: std::io::Write>(w: &mut W, generics: &syn::Generics, generics_impld: &syn::PathArguments, types: &TypeResolver, concrete_lifetimes: bool, dummy_lifetimes: bool) {
+pub fn maybe_write_type_non_lifetime_generics<W: std::io::Write>(w: &mut W, generics: &syn::Generics, types: &TypeResolver) {
+       maybe_write_generics_intern(w, generics, None, types, false, true);
+}
+
+fn maybe_write_generics_intern<W: std::io::Write>(w: &mut W, generics: &syn::Generics, generics_impld: Option<&syn::PathArguments>, types: &TypeResolver, concrete_lifetimes: bool, dummy_lifetimes: bool) {
        let mut gen_types = GenericTypes::new(None);
        assert!(gen_types.learn_generics(generics, types));
        if generics.params.is_empty() { return; }
@@ -821,7 +825,7 @@ fn maybe_write_generics_intern<W: std::io::Write>(w: &mut W, generics: &syn::Gen
                                if types.understood_c_type(&syn::parse_quote!(#type_ident), Some(&gen_types)) {
                                        types.write_c_type_in_generic_param(&mut out, &syn::parse_quote!(#type_ident), Some(&gen_types), false);
                                } else {
-                                       if let syn::PathArguments::AngleBracketed(args) = generics_impld {
+                                       if let Some(syn::PathArguments::AngleBracketed(args)) = generics_impld {
                                                if let syn::GenericArgument::Type(ty) = &args.args[idx] {
                                                        types.write_c_type_in_generic_param(&mut out, &ty, Some(&gen_types), false);
                                                }
index 358e30beead1b34e5ee04f1bda1dbac56cf1b11d..fd4ea8f4f95705f8e14f4668e4284052d2de69c7 100644 (file)
@@ -1201,23 +1201,13 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
                                                                                }
                                                                        }
 
-                                                                       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();