From 5707a3831c6c36f14e7fb4b8dd083451cc64b2c0 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 31 Oct 2021 18:07:47 +0000 Subject: [PATCH] Support printing lifetime generics on an impl-Trait block --- c-bindings-gen/src/blocks.rs | 18 +++++++++++++++++- c-bindings-gen/src/main.rs | 4 +++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/c-bindings-gen/src/blocks.rs b/c-bindings-gen/src/blocks.rs index e48a98a..5713578 100644 --- a/c-bindings-gen/src/blocks.rs +++ b/c-bindings-gen/src/blocks.rs @@ -751,4 +751,20 @@ pub fn maybe_write_generics(w: &mut W, generics: &syn::Generi } } - +pub fn maybe_write_lifetime_generics(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(); + } +} diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index 9d6f9cf..d490a89 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -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); -- 2.39.5