From 3d821b7f47766bbc3766ab1c5f8f4fd74ac7228b Mon Sep 17 00:00:00 2001 From: kngwyu Date: Mon, 2 Sep 2019 00:31:22 +0900 Subject: [PATCH] Enable slf: PyRef<'py, Self> --- pyo3-derive-backend/src/method.rs | 15 +++++++++++++-- tests/test_pyself.rs | 4 ++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/pyo3-derive-backend/src/method.rs b/pyo3-derive-backend/src/method.rs index b381a316..30fc837e 100644 --- a/pyo3-derive-backend/src/method.rs +++ b/pyo3-derive-backend/src/method.rs @@ -5,6 +5,7 @@ use crate::pyfunction::PyFunctionAttr; use proc_macro2::TokenStream; use quote::quote; use quote::ToTokens; +use syn::spanned::Spanned; #[derive(Clone, PartialEq, Debug)] pub struct FnArg<'a> { @@ -390,13 +391,23 @@ fn replace_self(path: &syn::TypePath) -> syn::TypePath { let mut res = path.to_owned(); for seg in &mut res.path.segments { if let syn::PathArguments::AngleBracketed(ref mut g) = seg.arguments { - for arg in &mut g.args { + let mut args = syn::punctuated::Punctuated::new(); + for arg in &g.args { + let mut add_arg = true; + if let syn::GenericArgument::Lifetime(_) = arg { + add_arg = false; + } if let syn::GenericArgument::Type(syn::Type::Path(p)) = arg { if p.path.segments.len() == 1 && p.path.segments[0].ident == "Self" { - *arg = infer(p.path.segments[0].ident.span()); + args.push(infer(p.span())); + add_arg = false; } } + if add_arg { + args.push(arg.clone()); + } } + g.args = args; } } res diff --git a/tests/test_pyself.rs b/tests/test_pyself.rs index fc0e691b..5f2ce854 100644 --- a/tests/test_pyself.rs +++ b/tests/test_pyself.rs @@ -20,6 +20,9 @@ impl Reader { fn clone_ref(slf: PyRef) -> PyRef { slf } + fn clone_ref_with_py<'py>(slf: PyRef<'py, Self>, _py: Python<'py>) -> PyRef<'py, Self> { + slf + } fn get_iter(slf: PyRef, keys: Py) -> PyResult { Ok(Iter { reader: slf.into(), @@ -98,6 +101,7 @@ fn test_clone_ref() { let py = gil.python(); let reader: PyObject = reader().into_py(py); py_assert!(py, reader, "reader == reader.clone_ref()"); + py_assert!(py, reader, "reader == reader.clone_ref_with_py()"); } #[test]