From fc5cdc10317521be99f050589860be391d02b81c Mon Sep 17 00:00:00 2001 From: kngwyu Date: Fri, 12 Jul 2019 22:22:56 +0900 Subject: [PATCH] [derive-backend] Add utils::if_type_is_python --- pyo3-derive-backend/src/method.rs | 16 ++++++---------- pyo3-derive-backend/src/module.rs | 13 ++----------- pyo3-derive-backend/src/utils.rs | 14 +++++++++++++- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/pyo3-derive-backend/src/method.rs b/pyo3-derive-backend/src/method.rs index 818a89c7..920689fb 100644 --- a/pyo3-derive-backend/src/method.rs +++ b/pyo3-derive-backend/src/method.rs @@ -85,16 +85,7 @@ impl<'a> FnSpec<'a> { } }; - let py = match ty { - syn::Type::Path(syn::TypePath { ref path, .. }) => { - if let Some(segment) = path.segments.last() { - segment.value().ident == "Python" - } else { - false - } - } - _ => false, - }; + let py = crate::utils::if_type_is_python(ty); let opt = check_arg_ty_and_optional(name, ty); arguments.push(FnArg { @@ -209,6 +200,11 @@ impl<'a> FnSpec<'a> { } false } + + /// A FnSpec is valid as getter if it has no argument or has one argument of type `Python` + pub fn valid_as_getter(&self) -> bool { + false + } } pub fn is_ref(name: &syn::Ident, ty: &syn::Type) -> bool { diff --git a/pyo3-derive-backend/src/module.rs b/pyo3-derive-backend/src/module.rs index 87117a8b..b9133533 100644 --- a/pyo3-derive-backend/src/module.rs +++ b/pyo3-derive-backend/src/module.rs @@ -64,16 +64,7 @@ fn wrap_fn_argument<'a>(input: &'a syn::FnArg, name: &'a Ident) -> Option panic!("unsupported argument: {:?}", cap.pat), }; - let py = match cap.ty { - syn::Type::Path(ref typath) => typath - .path - .segments - .last() - .map(|seg| seg.value().ident == "Python") - .unwrap_or(false), - _ => false, - }; - + let py = crate::utils::if_type_is_python(&cap.ty); let opt = method::check_arg_ty_and_optional(&name, &cap.ty); Some(method::FnArg { name: ident, @@ -111,7 +102,7 @@ fn extract_pyfn_attrs( } _ => panic!("The first parameter of pyfn must be a MetaItem"), } - // read Python fonction name + // read Python function name match meta[1] { syn::NestedMeta::Literal(syn::Lit::Str(ref lits)) => { fnname = Some(syn::Ident::new(&lits.value(), lits.span())); diff --git a/pyo3-derive-backend/src/utils.rs b/pyo3-derive-backend/src/utils.rs index 886fae4c..a6a9eb26 100644 --- a/pyo3-derive-backend/src/utils.rs +++ b/pyo3-derive-backend/src/utils.rs @@ -2,12 +2,24 @@ use proc_macro2::Span; use proc_macro2::TokenStream; -use syn; pub fn print_err(msg: String, t: TokenStream) { println!("Error: {} in '{}'", msg, t.to_string()); } +/// Check if the given type `ty` is `pyo3::Python`. +pub fn if_type_is_python(ty: &syn::Type) -> bool { + match ty { + syn::Type::Path(ref typath) => typath + .path + .segments + .last() + .map(|seg| seg.value().ident == "Python") + .unwrap_or(false), + _ => false, + } +} + // FIXME(althonos): not sure the docstring formatting is on par here. pub fn get_doc(attrs: &[syn::Attribute], null_terminated: bool) -> syn::Lit { let mut doc = Vec::new();