Skip to content

Commit

Permalink
py_class: Fix create_instance() to take inner type of "@shared data"
Browse files Browse the repository at this point in the history
Since the use of PySharedRefCell type is encapsulated, it shouldn't be the
type of create_instance() arguments. This patch adds $init_expr and $init_ty
to map $init_ty argument to the storage $data_ty object.

    ($data_name: $init_ty) -> $data_ty { $init_expr }
  • Loading branch information
yuja committed Jan 25, 2020
1 parent d845063 commit 71af8ec
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/py_class/py_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ macro_rules! py_class {
/* traverse_proc: */ None,
/* traverse_data: */ [ /*name*/ ]
},
/* data: */ [ /* { offset, name, type } */ ]
/* data: */ [ /* { offset, name, type, init_expr, init_type } */ ]
// TODO: base type, documentation, ...
}
/* slots: */ {
Expand Down Expand Up @@ -465,7 +465,7 @@ macro_rules! py_class {
/* traverse_proc: */ None,
/* traverse_data: */ [ /*name*/ ]
},
/* data: */ [ /* { offset, name, type } */ ]
/* data: */ [ /* { offset, name, type, init_expr, init_type } */ ]
// TODO: base type, documentation, ...
}
/* slots: */ {
Expand Down
16 changes: 10 additions & 6 deletions src/py_class/py_class_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
$size:expr,
{ $( $class_visibility:tt )* },
$gc:tt,
/* data: */ [ $( { $data_offset:expr, $data_name:ident, $data_ty:ty } )* ]
/* data: */ [ $( { $data_offset:expr, $data_name:ident, $data_ty:ty, $init_expr:expr, $init_ty:ty } )* ]
}
$slots:tt { $( $imp:item )* } $members:tt
} => {
Expand Down Expand Up @@ -112,7 +112,7 @@
py_coerce_item! {
impl $crate::py_class::BaseObject for $class {
type InitType = ( $( $data_ty, )* );
type InitType = ( $( $init_ty, )* );
#[inline]
fn size() -> usize {
Expand All @@ -126,7 +126,7 @@
) -> $crate::PyResult<$crate::PyObject>
{
let obj = <$base_type as $crate::py_class::BaseObject>::alloc(py, ty, ())?;
$( $crate::py_class::data_init::<$data_ty>(py, &obj, $data_offset, $data_name); )*
$( $crate::py_class::data_init::<$data_ty>(py, &obj, $data_offset, $init_expr); )*
Ok(obj)
}
Expand All @@ -139,7 +139,7 @@
$($imp)*
py_coerce_item! {
impl $class {
fn create_instance(py: $crate::Python $( , $data_name : $data_ty )* ) -> $crate::PyResult<$class> {
fn create_instance(py: $crate::Python $( , $data_name : $init_ty )* ) -> $crate::PyResult<$class> {
let obj = unsafe {
<$class as $crate::py_class::BaseObject>::alloc(
py, &py.get_type::<$class>(), ( $($data_name,)* )
Expand Down Expand Up @@ -350,7 +350,9 @@ def data_decl():
{
$crate::py_class::data_offset::<$data_type>($size),
$data_name,
$data_type
$data_type,
/* init_expr: */ $data_name,
/* init_ty: */ $data_type
}
]
}
Expand Down Expand Up @@ -384,7 +386,9 @@ def shared_data_decl():
{
$crate::py_class::data_offset::<$crate::PySharedRefCell<$data_type>>($size),
$data_name,
$crate::PySharedRefCell<$data_type>
/* data_ty: */ $crate::PySharedRefCell<$data_type>,
/* init_expr: */ $crate::PySharedRefCell::<$data_type>::new($data_name),
/* init_ty: */ $data_type
}
]
}
Expand Down
16 changes: 10 additions & 6 deletions src/py_class/py_class_impl2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ macro_rules! py_class_impl {
$size:expr,
{ $( $class_visibility:tt )* },
$gc:tt,
/* data: */ [ $( { $data_offset:expr, $data_name:ident, $data_ty:ty } )* ]
/* data: */ [ $( { $data_offset:expr, $data_name:ident, $data_ty:ty, $init_expr:expr, $init_ty:ty } )* ]
}
$slots:tt { $( $imp:item )* } $members:tt
} => {
Expand Down Expand Up @@ -104,7 +104,7 @@ macro_rules! py_class_impl {

py_coerce_item! {
impl $crate::py_class::BaseObject for $class {
type InitType = ( $( $data_ty, )* );
type InitType = ( $( $init_ty, )* );

#[inline]
fn size() -> usize {
Expand All @@ -118,7 +118,7 @@ macro_rules! py_class_impl {
) -> $crate::PyResult<$crate::PyObject>
{
let obj = <$base_type as $crate::py_class::BaseObject>::alloc(py, ty, ())?;
$( $crate::py_class::data_init::<$data_ty>(py, &obj, $data_offset, $data_name); )*
$( $crate::py_class::data_init::<$data_ty>(py, &obj, $data_offset, $init_expr); )*
Ok(obj)
}

Expand All @@ -131,7 +131,7 @@ macro_rules! py_class_impl {
$($imp)*
py_coerce_item! {
impl $class {
fn create_instance(py: $crate::Python $( , $data_name : $data_ty )* ) -> $crate::PyResult<$class> {
fn create_instance(py: $crate::Python $( , $data_name : $init_ty )* ) -> $crate::PyResult<$class> {
let obj = unsafe {
<$class as $crate::py_class::BaseObject>::alloc(
py, &py.get_type::<$class>(), ( $($data_name,)* )
Expand Down Expand Up @@ -222,7 +222,9 @@ macro_rules! py_class_impl {
{
$crate::py_class::data_offset::<$data_type>($size),
$data_name,
$data_type
$data_type,
/* init_expr: */ $data_name,
/* init_ty: */ $data_type
}
]
}
Expand Down Expand Up @@ -268,7 +270,9 @@ macro_rules! py_class_impl {
{
$crate::py_class::data_offset::<$crate::PySharedRefCell<$data_type>>($size),
$data_name,
$crate::PySharedRefCell<$data_type>
/* data_ty: */ $crate::PySharedRefCell<$data_type>,
/* init_expr: */ $crate::PySharedRefCell::<$data_type>::new($data_name),
/* init_ty: */ $data_type
}
]
}
Expand Down
16 changes: 10 additions & 6 deletions src/py_class/py_class_impl3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ macro_rules! py_class_impl {
$size:expr,
{ $( $class_visibility:tt )* },
$gc:tt,
/* data: */ [ $( { $data_offset:expr, $data_name:ident, $data_ty:ty } )* ]
/* data: */ [ $( { $data_offset:expr, $data_name:ident, $data_ty:ty, $init_expr:expr, $init_ty:ty } )* ]
}
$slots:tt { $( $imp:item )* } $members:tt
} => {
Expand Down Expand Up @@ -104,7 +104,7 @@ macro_rules! py_class_impl {

py_coerce_item! {
impl $crate::py_class::BaseObject for $class {
type InitType = ( $( $data_ty, )* );
type InitType = ( $( $init_ty, )* );

#[inline]
fn size() -> usize {
Expand All @@ -118,7 +118,7 @@ macro_rules! py_class_impl {
) -> $crate::PyResult<$crate::PyObject>
{
let obj = <$base_type as $crate::py_class::BaseObject>::alloc(py, ty, ())?;
$( $crate::py_class::data_init::<$data_ty>(py, &obj, $data_offset, $data_name); )*
$( $crate::py_class::data_init::<$data_ty>(py, &obj, $data_offset, $init_expr); )*
Ok(obj)
}

Expand All @@ -131,7 +131,7 @@ macro_rules! py_class_impl {
$($imp)*
py_coerce_item! {
impl $class {
fn create_instance(py: $crate::Python $( , $data_name : $data_ty )* ) -> $crate::PyResult<$class> {
fn create_instance(py: $crate::Python $( , $data_name : $init_ty )* ) -> $crate::PyResult<$class> {
let obj = unsafe {
<$class as $crate::py_class::BaseObject>::alloc(
py, &py.get_type::<$class>(), ( $($data_name,)* )
Expand Down Expand Up @@ -222,7 +222,9 @@ macro_rules! py_class_impl {
{
$crate::py_class::data_offset::<$data_type>($size),
$data_name,
$data_type
$data_type,
/* init_expr: */ $data_name,
/* init_ty: */ $data_type
}
]
}
Expand Down Expand Up @@ -268,7 +270,9 @@ macro_rules! py_class_impl {
{
$crate::py_class::data_offset::<$crate::PySharedRefCell<$data_type>>($size),
$data_name,
$crate::PySharedRefCell<$data_type>
/* data_ty: */ $crate::PySharedRefCell<$data_type>,
/* init_expr: */ $crate::PySharedRefCell::<$data_type>::new($data_name),
/* init_ty: */ $data_type
}
]
}
Expand Down
4 changes: 2 additions & 2 deletions tests/test_sharedref.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[macro_use]
extern crate cpython;

use cpython::{GILGuard, PySharedRefCell, Python};
use cpython::{GILGuard, Python};

py_class!(class Owner |py| {
@shared data string: String;
Expand All @@ -11,7 +11,7 @@ fn prepare_env() -> (GILGuard, Owner) {
let gil = Python::acquire_gil();
let owner = {
let py = gil.python();
Owner::create_instance(py, PySharedRefCell::new("new".to_owned())).unwrap()
Owner::create_instance(py, "new".to_owned()).unwrap()
};
(gil, owner)
}
Expand Down

0 comments on commit 71af8ec

Please sign in to comment.