diff --git a/src/types/any.rs b/src/types/any.rs index f17adc3f..a37205f2 100644 --- a/src/types/any.rs +++ b/src/types/any.rs @@ -33,6 +33,7 @@ pyobject_native_type_convert!( Some("builtins"), ffi::PyObject_Check ); +pyobject_native_type_extract!(PyAny); impl PyAny { pub fn downcast_ref(&self) -> Result<&T, PyDowncastError> diff --git a/src/types/mod.rs b/src/types/mod.rs index 3616b718..f8ee0e0d 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -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>