Skip to content

Commit 5ae8d9f

Browse files
committed
support lifetimes
1 parent 0cf7893 commit 5ae8d9f

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

pyo3-macros-backend/src/intopyobject.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,19 +257,25 @@ impl<'a> Container<'a> {
257257
}
258258
}
259259

260+
// if there is a `'py` lifetime, we treat is as the `Python<'py>` lifetime
261+
fn verify_and_get_lifetime(generics: &syn::Generics) -> Option<&syn::LifetimeParam> {
262+
let mut lifetimes = generics.lifetimes();
263+
lifetimes.find(|l| l.lifetime.ident == "py")
264+
}
265+
260266
pub fn build_derive_into_pyobject(tokens: &DeriveInput) -> Result<TokenStream> {
261267
let options = ContainerOptions::from_attrs(&tokens.attrs)?;
262268
let ctx = &Ctx::new(&options.krate, None);
263269
let Ctx { pyo3_path, .. } = &ctx;
264270

265271
let mut trait_generics = tokens.generics.clone();
266272
let generics = &tokens.generics;
267-
// let lt_param = if let Some(lt) = verify_and_get_lifetime(generics)? {
268-
// lt.clone()
269-
// } else {
270-
trait_generics.params.push(parse_quote!('py));
271-
// parse_quote!('py)
272-
// };
273+
let lt_param = if let Some(lt) = verify_and_get_lifetime(generics) {
274+
lt.clone()
275+
} else {
276+
trait_generics.params.push(parse_quote!('py));
277+
parse_quote!('py)
278+
};
273279
let mut where_clause: syn::WhereClause = parse_quote!(where);
274280
for param in generics.type_params() {
275281
let gen_ident = &param.ident;
@@ -305,12 +311,12 @@ pub fn build_derive_into_pyobject(tokens: &DeriveInput) -> Result<TokenStream> {
305311
let ident = &tokens.ident;
306312
Ok(quote!(
307313
#[automatically_derived]
308-
impl #trait_generics #pyo3_path::conversion::IntoPyObject<'py> for #ident #generics #where_clause {
314+
impl #trait_generics #pyo3_path::conversion::IntoPyObject<#lt_param> for #ident #generics #where_clause {
309315
type Target = #target;
310316
type Output = #output;
311317
type Error = #error;
312318

313-
fn into_pyobject(self, py: #pyo3_path::Python<'py>) -> ::std::result::Result<Self::Output, Self::Error> {
319+
fn into_pyobject(self, py: #pyo3_path::Python<#lt_param>) -> ::std::result::Result<Self::Output, Self::Error> {
314320
#body
315321
}
316322
}

0 commit comments

Comments
 (0)