From 12015873531cee5c1a7c9f1ac634164c28abf53e Mon Sep 17 00:00:00 2001 From: konstin Date: Wed, 4 Jul 2018 19:07:27 +0200 Subject: [PATCH] Remove some old-syn clutter --- pyo3-derive-backend/src/module.rs | 2 +- pyo3-derive-backend/src/py_class.rs | 64 ++++++++++++----------------- pyo3cls/src/lib.rs | 2 +- 3 files changed, 29 insertions(+), 39 deletions(-) diff --git a/pyo3-derive-backend/src/module.rs b/pyo3-derive-backend/src/module.rs index a6c312df..e59f1fa0 100644 --- a/pyo3-derive-backend/src/module.rs +++ b/pyo3-derive-backend/src/module.rs @@ -207,7 +207,7 @@ fn function_wrapper_ident(name: &syn::Ident) -> syn::Ident { /// Generates python wrapper over a function that allows adding it to a python module as a python /// function pub fn add_fn_to_module( - func: &mut syn::ItemFn, + func: &syn::ItemFn, python_name: &syn::Ident, pyfn_attrs: Vec, ) -> TokenStream { diff --git a/pyo3-derive-backend/src/py_class.rs b/pyo3-derive-backend/src/py_class.rs index 1c139082..2658ac3f 100644 --- a/pyo3-derive-backend/src/py_class.rs +++ b/pyo3-derive-backend/src/py_class.rs @@ -1,45 +1,35 @@ // Copyright (c) 2017-present PyO3 Project and Contributors -use std::collections::HashMap; - -use syn; - use method::{FnArg, FnSpec, FnType}; use proc_macro2::{Span, TokenStream}; use py_method::{impl_py_getter_def, impl_py_setter_def, impl_wrap_getter, impl_wrap_setter}; +use std::collections::HashMap; +use syn; use utils; -pub fn build_py_class(ast: &mut syn::DeriveInput, attr: &Vec) -> TokenStream { +pub fn build_py_class(class: &mut syn::ItemStruct, attr: &Vec) -> TokenStream { let (params, flags, base) = parse_attribute(attr); - let doc = utils::get_doc(&ast.attrs, true); + let doc = utils::get_doc(&class.attrs, true); let mut token: Option = None; let mut descriptors = Vec::new(); - if let syn::Data::Struct(ref mut struc) = ast.data { - if let syn::Fields::Named(ref mut fields) = struc.fields { - for field in fields.named.iter_mut() { - if is_python_token(field) { - token = field.ident.clone(); - break; - } else { - let field_descs = parse_descriptors(field); - if !field_descs.is_empty() { - descriptors.push((field.clone(), field_descs)); - } + if let syn::Fields::Named(ref mut fields) = class.fields { + for field in fields.named.iter_mut() { + if is_python_token(field) { + token = field.ident.clone(); + break; + } else { + let field_descs = parse_descriptors(field); + if !field_descs.is_empty() { + descriptors.push((field.clone(), field_descs)); } } - } else { - panic!("#[class] can only be used with C-style structs") } } else { - panic!("#[class] can only be used with structs") + panic!("#[class] can only be used with C-style structs") } - let tokens = impl_class(&ast.ident, &base, token, doc, params, flags, descriptors); - - quote! { - #tokens - } + impl_class(&class.ident, &base, token, doc, params, flags, descriptors) } fn parse_descriptors(item: &mut syn::Field) -> Vec { @@ -202,22 +192,22 @@ fn impl_class( let mut has_dict = false; for f in flags.iter() { if let syn::Expr::Path(ref epath) = f { - if epath.path == parse_quote!{::pyo3::typeob::PY_TYPE_FLAG_WEAKREF} { + if epath.path == parse_quote! {::pyo3::typeob::PY_TYPE_FLAG_WEAKREF} { has_weakref = true; - } else if epath.path == parse_quote!{::pyo3::typeob::PY_TYPE_FLAG_DICT} { + } else if epath.path == parse_quote! {::pyo3::typeob::PY_TYPE_FLAG_DICT} { has_dict = true; } } } let weakref = if has_weakref { - quote!{std::mem::size_of::<*const ::pyo3::ffi::PyObject>()} + quote! {std::mem::size_of::<*const ::pyo3::ffi::PyObject>()} } else { - quote!{0} + quote! {0} }; let dict = if has_dict { - quote!{std::mem::size_of::<*const ::pyo3::ffi::PyObject>()} + quote! {std::mem::size_of::<*const ::pyo3::ffi::PyObject>()} } else { - quote!{0} + quote! {0} }; quote! { @@ -391,8 +381,8 @@ fn parse_attribute( syn::TypePath, ) { let mut params = HashMap::new(); - let mut flags = vec![syn::Expr::Lit(parse_quote!{0})]; - let mut base: syn::TypePath = parse_quote!{::pyo3::PyObjectRef}; + let mut flags = vec![syn::Expr::Lit(parse_quote! {0})]; + let mut base: syn::TypePath = parse_quote! {::pyo3::PyObjectRef}; for expr in args.iter() { match expr { @@ -409,22 +399,22 @@ fn parse_attribute( { "gc" => { flags.push(syn::Expr::Path( - parse_quote!{::pyo3::typeob::PY_TYPE_FLAG_GC}, + parse_quote! {::pyo3::typeob::PY_TYPE_FLAG_GC}, )); } "weakref" => { flags.push(syn::Expr::Path( - parse_quote!{::pyo3::typeob::PY_TYPE_FLAG_WEAKREF}, + parse_quote! {::pyo3::typeob::PY_TYPE_FLAG_WEAKREF}, )); } "subclass" => { flags.push(syn::Expr::Path( - parse_quote!{::pyo3::typeob::PY_TYPE_FLAG_BASETYPE}, + parse_quote! {::pyo3::typeob::PY_TYPE_FLAG_BASETYPE}, )); } "dict" => { flags.push(syn::Expr::Path( - parse_quote!{::pyo3::typeob::PY_TYPE_FLAG_DICT}, + parse_quote! {::pyo3::typeob::PY_TYPE_FLAG_DICT}, )); } param => { diff --git a/pyo3cls/src/lib.rs b/pyo3cls/src/lib.rs index ef744196..71831586 100644 --- a/pyo3cls/src/lib.rs +++ b/pyo3cls/src/lib.rs @@ -88,7 +88,7 @@ pub fn class( input: proc_macro::TokenStream, ) -> proc_macro::TokenStream { // Parse the token stream into a syntax tree - let mut ast: syn::DeriveInput = syn::parse(input).expect("#[class] must be used on a `struct`"); + let mut ast: syn::ItemStruct = syn::parse(input).expect("#[class] must be used on a `struct`"); // Parse the macro arguments into a list of expressions let args: Vec = {