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