simplify PythonToken
This commit is contained in:
parent
6834a18a5b
commit
bcaaeb8522
|
@ -10,21 +10,11 @@ pub fn build_py_class(ast: &mut syn::DeriveInput) -> Tokens {
|
||||||
|
|
||||||
match ast.body {
|
match ast.body {
|
||||||
syn::Body::Struct(syn::VariantData::Struct(ref mut fields)) => {
|
syn::Body::Struct(syn::VariantData::Struct(ref mut fields)) => {
|
||||||
for field in fields.iter_mut() {
|
for field in fields.iter() {
|
||||||
let mut attrs = vec![];
|
if is_python_token(field) {
|
||||||
for attr in field.attrs.iter() {
|
token = field.ident.clone();
|
||||||
match attr.value {
|
break
|
||||||
syn::MetaItem::Word(ref a) => {
|
|
||||||
if a.as_ref() == "token" {
|
|
||||||
token = field.ident.clone();
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
attrs.push(attr.clone());
|
|
||||||
}
|
}
|
||||||
field.attrs = attrs;
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_ => panic!("#[class] can only be used with notmal structs"),
|
_ => panic!("#[class] can only be used with notmal structs"),
|
||||||
|
@ -149,3 +139,15 @@ fn impl_class(cls: &syn::Ident, base: &syn::Ident, token: Option<syn::Ident>) ->
|
||||||
#extra
|
#extra
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_python_token(field: &syn::Field) -> bool {
|
||||||
|
match field.ty {
|
||||||
|
syn::Ty::Path(_, ref path) => {
|
||||||
|
if let Some(segment) = path.segments.last() {
|
||||||
|
return segment.ident.as_ref() == "PythonToken"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
|
@ -214,7 +214,7 @@ impl<'p> Python<'p> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_token<T, F>(self, f: F) -> Py<'p, T>
|
pub fn with_token<T, F>(self, f: F) -> Py<'p, T>
|
||||||
where F: FnOnce(PythonToken<T>) -> T,
|
where F: FnOnce(PythonToken) -> T,
|
||||||
T: PyTypeInfo + PyObjectAlloc<Type=T>
|
T: PyTypeInfo + PyObjectAlloc<Type=T>
|
||||||
{
|
{
|
||||||
::token::with_token(self, f)
|
::token::with_token(self, f)
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// Copyright (c) 2017-present PyO3 Project and Contributors
|
// Copyright (c) 2017-present PyO3 Project and Contributors
|
||||||
|
|
||||||
|
use std::rc::Rc;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use pointers::Py;
|
use pointers::Py;
|
||||||
|
@ -7,9 +8,9 @@ use python::Python;
|
||||||
use typeob::{PyTypeInfo, PyObjectAlloc};
|
use typeob::{PyTypeInfo, PyObjectAlloc};
|
||||||
|
|
||||||
|
|
||||||
pub struct PythonToken<T>(PhantomData<T>);
|
pub struct PythonToken(PhantomData<Rc<()>>);
|
||||||
|
|
||||||
impl<T> PythonToken<T> {
|
impl PythonToken {
|
||||||
pub fn token<'p>(&'p self) -> Python<'p> {
|
pub fn token<'p>(&'p self) -> Python<'p> {
|
||||||
unsafe { Python::assume_gil_acquired() }
|
unsafe { Python::assume_gil_acquired() }
|
||||||
}
|
}
|
||||||
|
@ -17,7 +18,7 @@ impl<T> PythonToken<T> {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn with_token<'p, T, F>(py: Python<'p>, f: F) -> Py<'p, T>
|
pub fn with_token<'p, T, F>(py: Python<'p>, f: F) -> Py<'p, T>
|
||||||
where F: FnOnce(PythonToken<T>) -> T,
|
where F: FnOnce(PythonToken) -> T,
|
||||||
T: PyTypeInfo + PyObjectAlloc<Type=T>
|
T: PyTypeInfo + PyObjectAlloc<Type=T>
|
||||||
{
|
{
|
||||||
let value = f(PythonToken(PhantomData));
|
let value = f(PythonToken(PhantomData));
|
||||||
|
|
|
@ -69,8 +69,7 @@ fn empty_class_in_module() {
|
||||||
|
|
||||||
#[py::class]
|
#[py::class]
|
||||||
struct EmptyClassWithNew {
|
struct EmptyClassWithNew {
|
||||||
#[token]
|
token: PythonToken
|
||||||
token: PythonToken<EmptyClassWithNew>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[py::ptr(EmptyClassWithNew)]
|
#[py::ptr(EmptyClassWithNew)]
|
||||||
|
@ -95,8 +94,7 @@ fn empty_class_with_new() {
|
||||||
#[py::class]
|
#[py::class]
|
||||||
struct NewWithOneArg {
|
struct NewWithOneArg {
|
||||||
_data: i32,
|
_data: i32,
|
||||||
#[token]
|
token: PythonToken
|
||||||
token: PythonToken<NewWithOneArg>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[py::ptr(NewWithOneArg)]
|
#[py::ptr(NewWithOneArg)]
|
||||||
|
@ -125,8 +123,7 @@ struct NewWithTwoArgs {
|
||||||
_data1: i32,
|
_data1: i32,
|
||||||
_data2: i32,
|
_data2: i32,
|
||||||
|
|
||||||
#[token]
|
token: PythonToken
|
||||||
token: PythonToken<NewWithTwoArgs>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[py::ptr(NewWithTwoArgs)]
|
#[py::ptr(NewWithTwoArgs)]
|
||||||
|
@ -164,7 +161,7 @@ impl Drop for TestDropCall {
|
||||||
struct DataIsDropped {
|
struct DataIsDropped {
|
||||||
member1: TestDropCall,
|
member1: TestDropCall,
|
||||||
member2: TestDropCall,
|
member2: TestDropCall,
|
||||||
token: PythonToken<DataIsDropped>,
|
token: PythonToken,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -190,7 +187,7 @@ fn data_is_dropped() {
|
||||||
#[py::class]
|
#[py::class]
|
||||||
struct InstanceMethod {
|
struct InstanceMethod {
|
||||||
member: i32,
|
member: i32,
|
||||||
token: PythonToken<InstanceMethod>
|
token: PythonToken
|
||||||
}
|
}
|
||||||
|
|
||||||
#[py::methods]
|
#[py::methods]
|
||||||
|
@ -215,7 +212,7 @@ fn instance_method() {
|
||||||
#[py::class]
|
#[py::class]
|
||||||
struct InstanceMethodWithArgs {
|
struct InstanceMethodWithArgs {
|
||||||
member: i32,
|
member: i32,
|
||||||
token: PythonToken<InstanceMethodWithArgs>
|
token: PythonToken
|
||||||
}
|
}
|
||||||
#[py::methods]
|
#[py::methods]
|
||||||
impl InstanceMethodWithArgs {
|
impl InstanceMethodWithArgs {
|
||||||
|
@ -283,8 +280,7 @@ fn class_method() {
|
||||||
|
|
||||||
#[py::class]
|
#[py::class]
|
||||||
struct StaticMethod {
|
struct StaticMethod {
|
||||||
#[token]
|
token: PythonToken
|
||||||
token: PythonToken<StaticMethod>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[py::ptr(StaticMethod)]
|
#[py::ptr(StaticMethod)]
|
||||||
|
@ -338,8 +334,7 @@ impl StaticMethod {
|
||||||
struct GCIntegration {
|
struct GCIntegration {
|
||||||
self_ref: RefCell<PyObjectPtr>,
|
self_ref: RefCell<PyObjectPtr>,
|
||||||
dropped: TestDropCall,
|
dropped: TestDropCall,
|
||||||
#[token]
|
token: PythonToken,
|
||||||
token: PythonToken<GCIntegration>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[py::ptr(GCIntegration)]
|
#[py::ptr(GCIntegration)]
|
||||||
|
@ -377,7 +372,7 @@ fn gc_integration() {
|
||||||
#[py::class]
|
#[py::class]
|
||||||
pub struct Len {
|
pub struct Len {
|
||||||
l: usize,
|
l: usize,
|
||||||
token: PythonToken<Len>,
|
token: PythonToken,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[py::proto]
|
#[py::proto]
|
||||||
|
@ -426,7 +421,7 @@ fn iterator() {
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
#[py::class]
|
#[py::class]
|
||||||
struct StringMethods {token: PythonToken<StringMethods>}
|
struct StringMethods {token: PythonToken}
|
||||||
|
|
||||||
#[py::proto]
|
#[py::proto]
|
||||||
impl<'p> PyObjectProtocol<'p> for StringMethods {
|
impl<'p> PyObjectProtocol<'p> for StringMethods {
|
||||||
|
@ -467,8 +462,7 @@ fn string_methods() {
|
||||||
#[py::class]
|
#[py::class]
|
||||||
struct Comparisons {
|
struct Comparisons {
|
||||||
val: i32,
|
val: i32,
|
||||||
#[token]
|
token: PythonToken,
|
||||||
token: PythonToken<Comparisons>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[py::proto]
|
#[py::proto]
|
||||||
|
@ -502,8 +496,7 @@ fn comparisons() {
|
||||||
|
|
||||||
#[py::class]
|
#[py::class]
|
||||||
struct Sequence {
|
struct Sequence {
|
||||||
#[token]
|
token: PythonToken}
|
||||||
token: PythonToken<Sequence>}
|
|
||||||
|
|
||||||
#[py::proto]
|
#[py::proto]
|
||||||
impl PySequenceProtocol for Sequence {
|
impl PySequenceProtocol for Sequence {
|
||||||
|
@ -531,7 +524,7 @@ fn sequence() {
|
||||||
|
|
||||||
|
|
||||||
#[py::class]
|
#[py::class]
|
||||||
struct Callable {token: PythonToken<Callable>}
|
struct Callable {token: PythonToken}
|
||||||
|
|
||||||
#[py::methods]
|
#[py::methods]
|
||||||
impl Callable {
|
impl Callable {
|
||||||
|
@ -559,7 +552,7 @@ fn callable() {
|
||||||
struct SetItem {
|
struct SetItem {
|
||||||
key: i32,
|
key: i32,
|
||||||
val: i32,
|
val: i32,
|
||||||
token: PythonToken<SetItem>,
|
token: PythonToken,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[py::proto]
|
#[py::proto]
|
||||||
|
@ -586,7 +579,7 @@ fn setitem() {
|
||||||
#[py::class]
|
#[py::class]
|
||||||
struct DelItem {
|
struct DelItem {
|
||||||
key: i32,
|
key: i32,
|
||||||
token: PythonToken<DelItem>,
|
token: PythonToken,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[py::proto]
|
#[py::proto]
|
||||||
|
@ -611,7 +604,7 @@ fn delitem() {
|
||||||
#[py::class]
|
#[py::class]
|
||||||
struct SetDelItem {
|
struct SetDelItem {
|
||||||
val: Option<i32>,
|
val: Option<i32>,
|
||||||
token: PythonToken<SetDelItem>,
|
token: PythonToken,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[py::proto]
|
#[py::proto]
|
||||||
|
@ -640,7 +633,7 @@ fn setdelitem() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[py::class]
|
#[py::class]
|
||||||
struct Reversed {token: PythonToken<Reversed>}
|
struct Reversed {token: PythonToken}
|
||||||
|
|
||||||
#[py::proto]
|
#[py::proto]
|
||||||
impl PyMappingProtocol for Reversed{
|
impl PyMappingProtocol for Reversed{
|
||||||
|
@ -659,7 +652,7 @@ fn reversed() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[py::class]
|
#[py::class]
|
||||||
struct Contains {token: PythonToken<Contains>}
|
struct Contains {token: PythonToken}
|
||||||
|
|
||||||
#[py::proto]
|
#[py::proto]
|
||||||
impl PySequenceProtocol for Contains {
|
impl PySequenceProtocol for Contains {
|
||||||
|
@ -682,7 +675,7 @@ fn contains() {
|
||||||
|
|
||||||
|
|
||||||
#[py::class]
|
#[py::class]
|
||||||
struct UnaryArithmetic {token: PythonToken<UnaryArithmetic>}
|
struct UnaryArithmetic {token: PythonToken}
|
||||||
|
|
||||||
#[py::proto]
|
#[py::proto]
|
||||||
impl PyNumberProtocol for UnaryArithmetic {
|
impl PyNumberProtocol for UnaryArithmetic {
|
||||||
|
@ -719,8 +712,7 @@ fn unary_arithmetic() {
|
||||||
|
|
||||||
#[py::class]
|
#[py::class]
|
||||||
struct BinaryArithmetic {
|
struct BinaryArithmetic {
|
||||||
#[token]
|
token: PythonToken
|
||||||
token: PythonToken<BinaryArithmetic>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[py::proto]
|
#[py::proto]
|
||||||
|
@ -794,8 +786,7 @@ fn binary_arithmetic() {
|
||||||
|
|
||||||
#[py::class]
|
#[py::class]
|
||||||
struct RichComparisons {
|
struct RichComparisons {
|
||||||
#[token]
|
token: PythonToken
|
||||||
token: PythonToken<RichComparisons>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[py::proto]
|
#[py::proto]
|
||||||
|
@ -818,7 +809,7 @@ impl PyObjectProtocol for RichComparisons {
|
||||||
|
|
||||||
#[py::class]
|
#[py::class]
|
||||||
struct RichComparisons2 {
|
struct RichComparisons2 {
|
||||||
py: PythonToken<RichComparisons2>
|
py: PythonToken
|
||||||
}
|
}
|
||||||
|
|
||||||
#[py::proto]
|
#[py::proto]
|
||||||
|
@ -892,7 +883,7 @@ fn rich_comparisons_python_3_type_error() {
|
||||||
#[py::class]
|
#[py::class]
|
||||||
struct InPlaceOperations {
|
struct InPlaceOperations {
|
||||||
value: u32,
|
value: u32,
|
||||||
token: PythonToken<InPlaceOperations>,
|
token: PythonToken,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[py::proto]
|
#[py::proto]
|
||||||
|
@ -978,9 +969,7 @@ fn inplace_operations() {
|
||||||
#[py::class]
|
#[py::class]
|
||||||
struct ContextManager {
|
struct ContextManager {
|
||||||
exit_called: bool,
|
exit_called: bool,
|
||||||
|
token: PythonToken,
|
||||||
#[token]
|
|
||||||
token: PythonToken<ContextManager>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1027,7 +1016,7 @@ fn context_manager() {
|
||||||
#[py::class]
|
#[py::class]
|
||||||
struct ClassWithProperties {
|
struct ClassWithProperties {
|
||||||
num: i32,
|
num: i32,
|
||||||
token: PythonToken<ClassWithProperties>,
|
token: PythonToken,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[py::methods]
|
#[py::methods]
|
||||||
|
|
Loading…
Reference in a new issue