diff --git a/pyo3cls/src/py_class.rs b/pyo3cls/src/py_class.rs index 0655b64d..96d904d0 100644 --- a/pyo3cls/src/py_class.rs +++ b/pyo3cls/src/py_class.rs @@ -32,22 +32,21 @@ fn impl_class(cls: &syn::Ident, base: &syn::Ident) -> Tokens { let cls_name = quote! { #cls }.as_str().to_string(); quote! { - - impl _pyo3::class::typeob::PyTypeObjectInfo for #cls { + impl _pyo3::class::typeob::PyTypeInfo for #cls { type Type = #cls; #[inline] fn size() -> usize { - Self::offset() + std::mem::size_of::<#cls>() + Self::offset() as usize + std::mem::size_of::<#cls>() } #[inline] - fn offset() -> usize { + fn offset() -> isize { let align = std::mem::align_of::<#cls>(); - let bs = <#base as _pyo3::class::typeob::PyTypeObjectInfo>::size(); + let bs = <#base as _pyo3::class::typeob::PyTypeInfo>::size(); // round base_size up to next multiple of align - (bs + align - 1) / align * align + ((bs + align - 1) / align * align) as isize } #[inline] diff --git a/src/class/basic.rs b/src/class/basic.rs index b689bac9..8532213b 100644 --- a/src/class/basic.rs +++ b/src/class/basic.rs @@ -61,7 +61,7 @@ pub trait PyObjectProtocol<'a> : Sized + 'static { pub trait PyObjectGetAttrProtocol<'a>: PyObjectProtocol<'a> { - type Name: ::FromPyObj<'a> + ::class::typeob::PyTypeObjectInfo; + type Name: ::FromPyObj<'a> + ::class::typeob::PyTypeInfo; type Result: Into>; } @@ -71,7 +71,7 @@ pub trait PyObjectGetAttrProtocol<'a>: PyObjectProtocol<'a> { // type Result: Into>; //} pub trait PyObjectSetAttrProtocol<'a>: PyObjectProtocol<'a> { - type Name: FromPyObject<'a> + ::class::typeob::PyTypeObjectInfo + ::class::typeob::PyTypeObject + ::PythonObject; + type Name: FromPyObject<'a> + ::class::typeob::PyTypeInfo + ::class::typeob::PyTypeObject + ::PythonObject; type Value: FromPyObject<'a>; type Result: Into>; } @@ -177,14 +177,14 @@ use callback::CallbackConverter; impl<'a, T> PyObjectGetAttrProtocolImpl for T - where T: PyObjectGetAttrProtocol<'a> + ::class::typeob::PyTypeObjectInfo + where T: PyObjectGetAttrProtocol<'a> + ::class::typeob::PyTypeInfo { #[inline] fn tp_getattro() -> Option { //py_binary_func_!(PyObjectGetAttrProtocol, T::__getattr__, PyObjectCallbackConverter) unsafe extern "C" fn wrap<'a, T>(slf: *mut ffi::PyObject, arg: *mut ffi::PyObject) -> *mut ffi::PyObject - where T: PyObjectGetAttrProtocol<'a> + ::class::typeob::PyTypeObjectInfo + where T: PyObjectGetAttrProtocol<'a> + ::class::typeob::PyTypeInfo { const LOCATION: &'static str = concat!(stringify!($class), ".", stringify!($f), "()"); { diff --git a/src/class/mod.rs b/src/class/mod.rs index ac4b823d..7261f8e3 100644 --- a/src/class/mod.rs +++ b/src/class/mod.rs @@ -69,7 +69,7 @@ pub trait BaseObject { /// A PythonObject that is usable as a base type for #[class] -impl BaseObject for T where T : typeob::PyTypeObjectInfo { +impl BaseObject for T where T : typeob::PyTypeInfo { type Type = T::Type; /// Allocates a new object (usually by calling ty->tp_alloc), @@ -78,10 +78,10 @@ impl BaseObject for T where T : typeob::PyTypeObjectInfo { /// must be of type `ty`. unsafe fn alloc(_py: Python, value: T::Type) -> PyResult<*mut ffi::PyObject> { let obj = ffi::PyType_GenericAlloc( - ::type_object(), 0); + ::type_object(), 0); - let offset = ::offset(); - let ptr = (obj as *mut u8).offset(offset as isize) as *mut Self::Type; + let offset = ::offset(); + let ptr = (obj as *mut u8).offset(offset) as *mut Self::Type; std::ptr::write(ptr, value); Ok(obj) @@ -92,7 +92,7 @@ impl BaseObject for T where T : typeob::PyTypeObjectInfo { /// This function is used as tp_dealloc implementation. unsafe fn dealloc(_py: Python, obj: *mut ffi::PyObject) { let ptr = (obj as *mut u8).offset( - ::offset() as isize) as *mut Self::Type; + ::offset() as isize) as *mut Self::Type; std::ptr::drop_in_place(ptr); let ty = ffi::Py_TYPE(obj); diff --git a/src/class/typeob.rs b/src/class/typeob.rs index 9146709f..0deebcee 100644 --- a/src/class/typeob.rs +++ b/src/class/typeob.rs @@ -20,12 +20,12 @@ pub trait PyTypeObject { /// Trait implemented by object that generated by py::class macro -pub trait PyTypeObjectInfo { +pub trait PyTypeInfo { type Type; fn size() -> usize; - fn offset() -> usize; + fn offset() -> isize; fn type_name() -> &'static str; @@ -34,69 +34,69 @@ pub trait PyTypeObjectInfo { } -impl<'a, T: ?Sized> PyTypeObjectInfo for &'a T where T: PyTypeObjectInfo { +impl<'a, T: ?Sized> PyTypeInfo for &'a T where T: PyTypeInfo { type Type = T::Type; #[inline] default fn size() -> usize { - ::size() + ::size() } #[inline] - default fn offset() -> usize { - ::offset() + default fn offset() -> isize { + ::offset() } #[inline] default fn type_name() -> &'static str { - ::type_name() + ::type_name() } #[inline] default fn type_object() -> &'static mut ffi::PyTypeObject { - ::type_object() + ::type_object() } } -impl<'a, T> PyTypeObjectInfo for Py<'a, T> where T: PyTypeObjectInfo { +impl<'a, T> PyTypeInfo for Py<'a, T> where T: PyTypeInfo { type Type = T::Type; #[inline] default fn size() -> usize { - ::size() + ::size() } #[inline] - default fn offset() -> usize { - ::offset() + default fn offset() -> isize { + ::offset() } #[inline] default fn type_name() -> &'static str { - ::type_name() + ::type_name() } #[inline] default fn type_object() -> &'static mut ffi::PyTypeObject { - ::type_object() + ::type_object() } } -impl PyTypeObject for T where T: BaseObject + PyTypeObjectInfo { +impl PyTypeObject for T where T: BaseObject + PyTypeInfo { #[inline] fn type_object(py: Python) -> PyType { - let mut ty = ::type_object(); + let mut ty = ::type_object(); if (ty.tp_flags & ffi::Py_TPFLAGS_READY) != 0 { unsafe { PyType::from_type_ptr(py, ty) } } else { // automatically initialize the class on-demand - initialize_type::(py, None, ::type_name(), ty).expect( + initialize_type::(py, None, ::type_name(), ty).expect( format!("An error occurred while initializing class {}", - ::type_name()).as_ref()); + ::type_name()).as_ref()); unsafe { PyType::from_type_ptr(py, ty) } } } @@ -104,7 +104,7 @@ impl PyTypeObject for T where T: BaseObject + PyTypeObjectInfo { pub fn initialize_type(py: Python, module_name: Option<&str>, type_name: &str, type_object: &mut ffi::PyTypeObject) -> PyResult - where T: BaseObject + PyTypeObjectInfo + where T: BaseObject + PyTypeInfo { // type name let name = match module_name { @@ -120,7 +120,7 @@ pub fn initialize_type(py: Python, module_name: Option<&str>, type_name: &str type_object.tp_dealloc = Some(tp_dealloc_callback::); // type size - type_object.tp_basicsize = ::size() as ffi::Py_ssize_t; + type_object.tp_basicsize = ::size() as ffi::Py_ssize_t; // GC support // ::update_type_object(type_object); @@ -225,7 +225,7 @@ pub fn initialize_type(py: Python, module_name: Option<&str>, type_name: &str } unsafe extern "C" fn tp_dealloc_callback(obj: *mut ffi::PyObject) - where T: BaseObject + PyTypeObjectInfo + where T: PyTypeInfo { let guard = AbortOnDrop("Cannot unwind out of tp_dealloc"); let py = Python::assume_gil_acquired(); diff --git a/src/conversion.rs b/src/conversion.rs index 870ddfbc..7a5e36dc 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -117,7 +117,7 @@ pub trait FromPyObject<'source> : Sized { pub trait FromPyObj<'source> : Sized { /// Extracts `Self` from the source `PyObject`. fn extr(py: &'source Py<'source, S>) -> PyResult - where S: ::class::typeob::PyTypeObjectInfo; + where S: ::class::typeob::PyTypeInfo; } pub trait RefFromPyObject { diff --git a/src/objects/mod.rs b/src/objects/mod.rs index 304f2df5..7cef2c23 100644 --- a/src/objects/mod.rs +++ b/src/objects/mod.rs @@ -93,7 +93,7 @@ macro_rules! pyobject_newtype( ($name: ident, $checkfunction: ident, $typeobject: ident) => ( pyobject_newtype!($name, $checkfunction); - impl $crate::class::typeob::PyTypeObjectInfo for $name { + impl $crate::class::typeob::PyTypeInfo for $name { type Type = (); #[inline] @@ -102,7 +102,7 @@ macro_rules! pyobject_newtype( } #[inline] - fn offset() -> usize { + fn offset() -> isize { 0 } @@ -155,16 +155,16 @@ macro_rules! pyobject_newtype_( } } - impl $crate::class::typeob::PyTypeObjectInfo for $name { + impl $crate::class::typeob::PyTypeInfo for $name { type Type = (); #[inline] fn size() -> usize { - 0 + $crate::std::mem::size_of::<$name>() } #[inline] - fn offset() -> usize { + fn offset() -> isize { 0 } @@ -201,12 +201,12 @@ pub mod exc; use std; use ffi; -use class::typeob::PyTypeObjectInfo; +use class::typeob::PyTypeInfo; pub struct PyObj; -impl PyTypeObjectInfo for PyObj { +impl PyTypeInfo for PyObj { type Type = (); #[inline] @@ -215,7 +215,7 @@ impl PyTypeObjectInfo for PyObj { } #[inline] - fn offset() -> usize { + fn offset() -> isize { 0 } @@ -230,7 +230,7 @@ impl PyTypeObjectInfo for PyObj { } } -impl PyTypeObjectInfo for PyObject { +impl PyTypeInfo for PyObject { type Type = (); #[inline] @@ -239,7 +239,7 @@ impl PyTypeObjectInfo for PyObject { } #[inline] - fn offset() -> usize { + fn offset() -> isize { 0 } diff --git a/src/objects/module.rs b/src/objects/module.rs index 3ed916c9..8b481a74 100644 --- a/src/objects/module.rs +++ b/src/objects/module.rs @@ -110,10 +110,10 @@ impl PyModule { /// sets `new_type.__module__` to this module's name, /// and adds the type to this module. pub fn add_class<'p, T>(&self, py: Python<'p>) -> PyResult<()> - where T: ::class::BaseObject + PythonObject + ::class::typeob::PyTypeObjectInfo + where T: ::class::BaseObject + PythonObject + ::class::typeob::PyTypeInfo { - let mut ty = ::type_object(); - let type_name = ::type_name(); + let mut ty = ::type_object(); + let type_name = ::type_name(); let ty = if (ty.tp_flags & ffi::Py_TPFLAGS_READY) != 0 { unsafe { PyType::from_type_ptr(py, ty) } @@ -123,7 +123,7 @@ impl PyModule { ::class::typeob::initialize_type::( py, Some(name), type_name, ty).expect( format!("An error occurred while initializing class {}", - ::type_name()).as_ref()); + ::type_name()).as_ref()); unsafe { PyType::from_type_ptr(py, ty) } }; diff --git a/src/pyptr.rs b/src/pyptr.rs index dfbce6c0..7a107155 100644 --- a/src/pyptr.rs +++ b/src/pyptr.rs @@ -12,7 +12,7 @@ use err::{self, PyResult}; use python::Python; use class::BaseObject; use objects::PyObject; -use class::typeob::PyTypeObjectInfo; +use class::typeob::PyTypeInfo; #[derive(Debug)] @@ -46,7 +46,7 @@ impl PyPtr { ptr } - /// Gets the reference count of this Py object. + /// Gets the reference count of this PyPtr object. #[inline] pub fn get_refcnt(&self) -> usize { unsafe { ffi::Py_REFCNT(self.inner) as usize } @@ -161,7 +161,7 @@ impl<'p, T> Py<'p, T> } -impl<'p, T> Py<'p, T> where T: PyTypeObjectInfo +impl<'p, T> Py<'p, T> where T: PyTypeInfo { /// Create new python object and move T instance under python management pub fn new(py: Python<'p>, value: T) -> PyResult> where T: BaseObject @@ -218,28 +218,20 @@ impl<'p, T> Py<'p, T> where T: PyTypeObjectInfo #[inline] pub fn as_ref(&self) -> &T { - let align = std::mem::align_of::(); - let bs = ::size(); - - // round base_size up to next multiple of align - let offset = (bs + align - 1) / align * align; + let offset = ::offset(); unsafe { - let ptr = (self.inner as *mut u8).offset(offset as isize) as *mut T; + let ptr = (self.inner as *mut u8).offset(offset) as *mut T; ptr.as_ref().unwrap() } } #[inline] pub fn as_mut(&self) -> &mut T { - let align = std::mem::align_of::(); - let bs = ::size(); - - // round base_size up to next multiple of align - let offset = (bs + align - 1) / align * align; + let offset = ::offset(); unsafe { - let ptr = (self.inner as *mut u8).offset(offset as isize) as *mut T; + let ptr = (self.inner as *mut u8).offset(offset) as *mut T; ptr.as_mut().unwrap() } } @@ -279,13 +271,9 @@ impl<'p, T> Py<'p, T> where T: PyTypeObjectInfo /// Undefined behavior if the input object does not have the expected type. #[inline] pub unsafe fn unchecked_downcast_borrow_from<'a, S>(py: &'a Py<'a, S>) -> &'a T { - let align = std::mem::align_of::(); - let bs = ::size(); + let offset = ::offset(); - // round base_size up to next multiple of align - let offset = (bs + align - 1) / align * align; - - let ptr = (py.inner as *mut u8).offset(offset as isize) as *mut T; + let ptr = (py.inner as *mut u8).offset(offset) as *mut T; ptr.as_ref().unwrap() } @@ -319,7 +307,7 @@ impl<'p, T> Clone for Py<'p, T> { } } -impl<'p, T> Deref for Py<'p, T> where T: PyTypeObjectInfo { +impl<'p, T> Deref for Py<'p, T> where T: PyTypeInfo { type Target = T; fn deref(&self) -> &T { @@ -327,21 +315,21 @@ impl<'p, T> Deref for Py<'p, T> where T: PyTypeObjectInfo { } } -impl<'p, T> AsRef for Py<'p, T> where T: PyTypeObjectInfo { +impl<'p, T> AsRef for Py<'p, T> where T: PyTypeInfo { #[inline] fn as_ref(&self) -> &T { self.as_ref() } } -impl<'p, T> AsMut for Py<'p, T> where T: PyTypeObjectInfo { +impl<'p, T> AsMut for Py<'p, T> where T: PyTypeInfo { #[inline] fn as_mut(&mut self) -> &mut T { Py::::as_mut(self) } } -impl<'p, T> ::PyWithCheckedDowncast<'p> for T where T: PyTypeObjectInfo +impl<'p, T> ::PyWithCheckedDowncast<'p> for T where T: PyTypeInfo { #[inline] default fn downcast_from(ob: Py<'p, S>) @@ -360,7 +348,7 @@ impl<'p, T> ::PyWithCheckedDowncast<'p> for T where T: PyTypeObjectInfo #[inline] default fn downcast_borrow_from<'source, S>( ob: &'source Py<'p, S>) -> Result<&'source T, ::PythonObjectDowncastError<'p>> - where S: PyTypeObjectInfo + where S: PyTypeInfo { println!("downcast borrow from {:?}", ob); let checked = unsafe { ffi::PyObject_TypeCheck(ob.inner, T::type_object()) != 0 }; @@ -376,22 +364,22 @@ impl<'p, T> ::PyWithCheckedDowncast<'p> for T where T: PyTypeObjectInfo } impl<'source, T> ::FromPyObj<'source> for &'source T - where T: PyTypeObjectInfo + where T: PyTypeInfo { #[inline] default fn extr(py: &'source Py<'source, S>) -> PyResult<&'source T> - where S: PyTypeObjectInfo + where S: PyTypeInfo { Ok(::PyWithCheckedDowncast::downcast_borrow_from(py)?) } } impl<'source, T> ::FromPyObj<'source> for Py<'source, T> - where T: PyTypeObjectInfo + where T: PyTypeInfo { #[inline] default fn extr(py: &'source Py<'source, S>) -> PyResult> - where S: PyTypeObjectInfo + where S: PyTypeInfo { Ok(::PyWithCheckedDowncast::downcast_from(py.clone())?) } @@ -416,7 +404,7 @@ impl<'p, T> ToPyObject for Py<'p, T> { } } -impl<'p, T> fmt::Debug for Py<'p, T> where T: PyTypeObjectInfo { +impl<'p, T> fmt::Debug for Py<'p, T> where T: PyTypeInfo { fn fmt(&self, f : &mut fmt::Formatter) -> Result<(), fmt::Error> { let repr_obj = try!(unsafe { err::result_cast_from_owned_ptr::<::PyString>(self.py(), ffi::PyObject_Repr(self.as_ptr())) diff --git a/src/python.rs b/src/python.rs index 7945753d..1bce4236 100644 --- a/src/python.rs +++ b/src/python.rs @@ -97,7 +97,7 @@ pub trait PyWithCheckedDowncast<'p> : Sized { /// Cast from PyObject to a concrete Python object type. fn downcast_borrow_from<'source, S>(&'source ::Py<'p, S>) -> Result<&'source Self, PythonObjectDowncastError<'p>> - where S: ::class::typeob::PyTypeObjectInfo; + where S: ::class::typeob::PyTypeInfo; } impl PythonObjectWithCheckedDowncast for T where T: PyTypeObject + PythonObject {