]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Correct printing generic methods in traits
authorMatt Corallo <git@bluematt.me>
Wed, 22 Sep 2021 18:39:46 +0000 (18:39 +0000)
committerMatt Corallo <git@bluematt.me>
Thu, 23 Sep 2021 17:37:19 +0000 (17:37 +0000)
If a trait method is generic (and not the object itself), we
previously would print the concrete resolved types in the
`impl nativeTrait for TraitStruct` block instead of the generic
names, leading to compilation failures.

c-bindings-gen/src/main.rs

index a114c2289c921d8156009942485ac453673c8709..1572b313582db2e2275b23b99ca3ba77a900b3cb 100644 (file)
@@ -402,8 +402,11 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
                                                }
                                                let mut meth_gen_types = gen_types.push_ctx();
                                                assert!(meth_gen_types.learn_generics(&m.sig.generics, $type_resolver));
+                                               // Note that we do *not* use the method generics when printing "native"
+                                               // rust parts - if the method is generic, we need to print a generic
+                                               // method.
                                                write!(w, "\tfn {}", m.sig.ident).unwrap();
-                                               $type_resolver.write_rust_generic_param(w, Some(&meth_gen_types), m.sig.generics.params.iter());
+                                               $type_resolver.write_rust_generic_param(w, Some(&gen_types), m.sig.generics.params.iter());
                                                write!(w, "(").unwrap();
                                                for inp in m.sig.inputs.iter() {
                                                        match inp {
@@ -431,7 +434,7 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
                                                                                }
                                                                                _ => unimplemented!(),
                                                                        }
-                                                                       $type_resolver.write_rust_type(w, Some(&meth_gen_types), &*arg.ty);
+                                                                       $type_resolver.write_rust_type(w, Some(&gen_types), &*arg.ty);
                                                                }
                                                        }
                                                }
@@ -439,7 +442,7 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
                                                match &m.sig.output {
                                                        syn::ReturnType::Type(_, rtype) => {
                                                                write!(w, " -> ").unwrap();
-                                                               $type_resolver.write_rust_type(w, Some(&meth_gen_types), &*rtype)
+                                                               $type_resolver.write_rust_type(w, Some(&gen_types), &*rtype)
                                                        },
                                                        _ => {},
                                                }