]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Support printing lifetime generics on an impl-Trait block
authorMatt Corallo <git@bluematt.me>
Sun, 31 Oct 2021 18:07:47 +0000 (18:07 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 2 Nov 2021 16:54:45 +0000 (16:54 +0000)
c-bindings-gen/src/blocks.rs
c-bindings-gen/src/main.rs

index e48a98a5c5a14787f489096eabd8d283267d33ec..57135789b71079f97961f1aa3e020acfcf64f4bf 100644 (file)
@@ -751,4 +751,20 @@ pub fn maybe_write_generics<W: std::io::Write>(w: &mut W, generics: &syn::Generi
        }
 }
 
-
+pub fn maybe_write_lifetime_generics<W: std::io::Write>(w: &mut W, generics: &syn::Generics, types: &TypeResolver) {
+       let mut gen_types = GenericTypes::new(None);
+       assert!(gen_types.learn_generics(generics, types));
+       if generics.params.iter().any(|param| if let syn::GenericParam::Lifetime(_) = param { true } else { false }) {
+               write!(w, "<").unwrap();
+               for (idx, generic) in generics.params.iter().enumerate() {
+                       match generic {
+                               syn::GenericParam::Type(_) => {},
+                               syn::GenericParam::Lifetime(lt) => {
+                                       write!(w, "{}'{}", if idx != 0 { ", " } else { "" }, lt.lifetime.ident).unwrap();
+                               },
+                               _ => unimplemented!(),
+                       }
+               }
+               write!(w, ">").unwrap();
+       }
+}
index 9d6f9cfeadad7717594a5d878d19e2a0ac2a1af2..d490a8901d363b53b7f83481de7c9844aeb6e20a 100644 (file)
@@ -559,7 +559,9 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
        // Finally, implement the original Rust trait for the newly created mapped trait.
        writeln!(w, "\nuse {}::{} as rust{};", types.module_path, t.ident, trait_name).unwrap();
        if implementable {
-               write!(w, "impl rust{}", t.ident).unwrap();
+               write!(w, "impl").unwrap();
+               maybe_write_lifetime_generics(w, &t.generics, types);
+               write!(w, " rust{}", t.ident).unwrap();
                maybe_write_generics(w, &t.generics, types, false);
                writeln!(w, " for {} {{", trait_name).unwrap();
                impl_trait_for_c!(t, "", types);