Merge pull request #750 from kngwyu/separete-extract

Separate FromPyObjectImpl from pyobject_native_type_convert!
This commit is contained in:
Yuji Kanagawa 2020-01-29 19:53:29 +09:00 committed by GitHub
commit 99c601b6a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 4 deletions

View File

@ -33,6 +33,7 @@ pyobject_native_type_convert!(
Some("builtins"),
ffi::PyObject_Check
);
pyobject_native_type_extract!(PyAny);
impl PyAny {
pub fn downcast_ref<T>(&self) -> Result<&T, PyDowncastError>

View File

@ -63,6 +63,7 @@ macro_rules! pyobject_native_type {
impl $crate::type_object::PyObjectSizedLayout<$name> for $layout {}
pyobject_native_type_named!($name $(,$type_param)*);
pyobject_native_type_convert!($name, $layout, $typeobject, $module, $checkfunction $(,$type_param)*);
pyobject_native_type_extract!($name $(,$type_param)*);
impl<'a, $($type_param,)*> ::std::convert::From<&'a $name> for &'a $crate::types::PyAny {
fn from(ob: &'a $name) -> Self {
@ -84,6 +85,8 @@ macro_rules! pyobject_native_var_type {
pyobject_native_type_named!($name $(,$type_param)*);
pyobject_native_type_convert!($name, $crate::ffi::PyObject,
$typeobject, $module, $checkfunction $(,$type_param)*);
pyobject_native_type_extract!($name $(,$type_param)*);
impl<'a, $($type_param,)*> ::std::convert::From<&'a $name> for &'a $crate::types::PyAny {
fn from(ob: &'a $name) -> Self {
unsafe{&*(ob as *const $name as *const $crate::types::PyAny)}
@ -97,6 +100,16 @@ macro_rules! pyobject_native_var_type {
};
}
// NOTE: This macro is not included in pyobject_native_type_convert!
// because rust-numpy has a special implementation.
macro_rules! pyobject_native_type_extract {
($name: ty $(,$type_param: ident)*) => {
impl<$($type_param,)*> $crate::conversion::FromPyObjectImpl for &'_ $name {
type Impl = $crate::conversion::extract_impl::Reference;
}
}
}
#[macro_export]
macro_rules! pyobject_native_type_convert(
($name: ty, $layout: path, $typeobject: expr,
@ -142,10 +155,6 @@ macro_rules! pyobject_native_type_convert(
}
}
impl<$($type_param,)*> $crate::conversion::FromPyObjectImpl for &'_ $name {
type Impl = $crate::conversion::extract_impl::Reference;
}
impl<$($type_param,)*> ::std::fmt::Debug for $name {
fn fmt(&self, f: &mut ::std::fmt::Formatter)
-> Result<(), ::std::fmt::Error>