Merge pull request #588 from kngwyu/ref-into-tweak

Enable slf: PyRef<'py, Self>
This commit is contained in:
konstin 2019-09-05 12:43:47 +02:00 committed by GitHub
commit d3c5a61d75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -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,14 +391,24 @@ 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
}

View File

@ -20,6 +20,9 @@ impl Reader {
fn clone_ref(slf: PyRef<Self>) -> PyRef<Self> {
slf
}
fn clone_ref_with_py<'py>(slf: PyRef<'py, Self>, _py: Python<'py>) -> PyRef<'py, Self> {
slf
}
fn get_iter(slf: PyRef<Self>, keys: Py<PyBytes>) -> PyResult<Iter> {
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]