Remove some old-syn clutter
This commit is contained in:
parent
bc412ede8f
commit
1201587353
|
@ -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
|
/// Generates python wrapper over a function that allows adding it to a python module as a python
|
||||||
/// function
|
/// function
|
||||||
pub fn add_fn_to_module(
|
pub fn add_fn_to_module(
|
||||||
func: &mut syn::ItemFn,
|
func: &syn::ItemFn,
|
||||||
python_name: &syn::Ident,
|
python_name: &syn::Ident,
|
||||||
pyfn_attrs: Vec<args::Argument>,
|
pyfn_attrs: Vec<args::Argument>,
|
||||||
) -> TokenStream {
|
) -> TokenStream {
|
||||||
|
|
|
@ -1,45 +1,35 @@
|
||||||
// Copyright (c) 2017-present PyO3 Project and Contributors
|
// Copyright (c) 2017-present PyO3 Project and Contributors
|
||||||
|
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
use syn;
|
|
||||||
|
|
||||||
use method::{FnArg, FnSpec, FnType};
|
use method::{FnArg, FnSpec, FnType};
|
||||||
use proc_macro2::{Span, TokenStream};
|
use proc_macro2::{Span, TokenStream};
|
||||||
use py_method::{impl_py_getter_def, impl_py_setter_def, impl_wrap_getter, impl_wrap_setter};
|
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;
|
use utils;
|
||||||
|
|
||||||
pub fn build_py_class(ast: &mut syn::DeriveInput, attr: &Vec<syn::Expr>) -> TokenStream {
|
pub fn build_py_class(class: &mut syn::ItemStruct, attr: &Vec<syn::Expr>) -> TokenStream {
|
||||||
let (params, flags, base) = parse_attribute(attr);
|
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<syn::Ident> = None;
|
let mut token: Option<syn::Ident> = None;
|
||||||
let mut descriptors = Vec::new();
|
let mut descriptors = Vec::new();
|
||||||
|
|
||||||
if let syn::Data::Struct(ref mut struc) = ast.data {
|
if let syn::Fields::Named(ref mut fields) = class.fields {
|
||||||
if let syn::Fields::Named(ref mut fields) = struc.fields {
|
for field in fields.named.iter_mut() {
|
||||||
for field in fields.named.iter_mut() {
|
if is_python_token(field) {
|
||||||
if is_python_token(field) {
|
token = field.ident.clone();
|
||||||
token = field.ident.clone();
|
break;
|
||||||
break;
|
} else {
|
||||||
} else {
|
let field_descs = parse_descriptors(field);
|
||||||
let field_descs = parse_descriptors(field);
|
if !field_descs.is_empty() {
|
||||||
if !field_descs.is_empty() {
|
descriptors.push((field.clone(), field_descs));
|
||||||
descriptors.push((field.clone(), field_descs));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
panic!("#[class] can only be used with C-style structs")
|
|
||||||
}
|
}
|
||||||
} else {
|
} 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);
|
impl_class(&class.ident, &base, token, doc, params, flags, descriptors)
|
||||||
|
|
||||||
quote! {
|
|
||||||
#tokens
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_descriptors(item: &mut syn::Field) -> Vec<FnType> {
|
fn parse_descriptors(item: &mut syn::Field) -> Vec<FnType> {
|
||||||
|
@ -202,22 +192,22 @@ fn impl_class(
|
||||||
let mut has_dict = false;
|
let mut has_dict = false;
|
||||||
for f in flags.iter() {
|
for f in flags.iter() {
|
||||||
if let syn::Expr::Path(ref epath) = f {
|
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;
|
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;
|
has_dict = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let weakref = if has_weakref {
|
let weakref = if has_weakref {
|
||||||
quote!{std::mem::size_of::<*const ::pyo3::ffi::PyObject>()}
|
quote! {std::mem::size_of::<*const ::pyo3::ffi::PyObject>()}
|
||||||
} else {
|
} else {
|
||||||
quote!{0}
|
quote! {0}
|
||||||
};
|
};
|
||||||
let dict = if has_dict {
|
let dict = if has_dict {
|
||||||
quote!{std::mem::size_of::<*const ::pyo3::ffi::PyObject>()}
|
quote! {std::mem::size_of::<*const ::pyo3::ffi::PyObject>()}
|
||||||
} else {
|
} else {
|
||||||
quote!{0}
|
quote! {0}
|
||||||
};
|
};
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
|
@ -391,8 +381,8 @@ fn parse_attribute(
|
||||||
syn::TypePath,
|
syn::TypePath,
|
||||||
) {
|
) {
|
||||||
let mut params = HashMap::new();
|
let mut params = HashMap::new();
|
||||||
let mut flags = vec![syn::Expr::Lit(parse_quote!{0})];
|
let mut flags = vec![syn::Expr::Lit(parse_quote! {0})];
|
||||||
let mut base: syn::TypePath = parse_quote!{::pyo3::PyObjectRef};
|
let mut base: syn::TypePath = parse_quote! {::pyo3::PyObjectRef};
|
||||||
|
|
||||||
for expr in args.iter() {
|
for expr in args.iter() {
|
||||||
match expr {
|
match expr {
|
||||||
|
@ -409,22 +399,22 @@ fn parse_attribute(
|
||||||
{
|
{
|
||||||
"gc" => {
|
"gc" => {
|
||||||
flags.push(syn::Expr::Path(
|
flags.push(syn::Expr::Path(
|
||||||
parse_quote!{::pyo3::typeob::PY_TYPE_FLAG_GC},
|
parse_quote! {::pyo3::typeob::PY_TYPE_FLAG_GC},
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
"weakref" => {
|
"weakref" => {
|
||||||
flags.push(syn::Expr::Path(
|
flags.push(syn::Expr::Path(
|
||||||
parse_quote!{::pyo3::typeob::PY_TYPE_FLAG_WEAKREF},
|
parse_quote! {::pyo3::typeob::PY_TYPE_FLAG_WEAKREF},
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
"subclass" => {
|
"subclass" => {
|
||||||
flags.push(syn::Expr::Path(
|
flags.push(syn::Expr::Path(
|
||||||
parse_quote!{::pyo3::typeob::PY_TYPE_FLAG_BASETYPE},
|
parse_quote! {::pyo3::typeob::PY_TYPE_FLAG_BASETYPE},
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
"dict" => {
|
"dict" => {
|
||||||
flags.push(syn::Expr::Path(
|
flags.push(syn::Expr::Path(
|
||||||
parse_quote!{::pyo3::typeob::PY_TYPE_FLAG_DICT},
|
parse_quote! {::pyo3::typeob::PY_TYPE_FLAG_DICT},
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
param => {
|
param => {
|
||||||
|
|
|
@ -88,7 +88,7 @@ pub fn class(
|
||||||
input: proc_macro::TokenStream,
|
input: proc_macro::TokenStream,
|
||||||
) -> proc_macro::TokenStream {
|
) -> proc_macro::TokenStream {
|
||||||
// Parse the token stream into a syntax tree
|
// 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
|
// Parse the macro arguments into a list of expressions
|
||||||
let args: Vec<syn::Expr> = {
|
let args: Vec<syn::Expr> = {
|
||||||
|
|
Loading…
Reference in New Issue