|
| 1 | +use crate::types::TypeNum; |
| 2 | +use pyo3::{self, ffi, typeob, PyObjectAlloc, Python, ToPyPointer}; |
| 3 | +use std::os::raw::c_void; |
| 4 | + |
| 5 | +#[repr(C)] |
| 6 | +pub(crate) struct SliceBox<T> { |
| 7 | + ob_base: ffi::PyObject, |
| 8 | + inner: *mut [T], |
| 9 | +} |
| 10 | + |
| 11 | +impl<T> SliceBox<T> { |
| 12 | + pub(crate) unsafe fn new<'a>(box_: Box<[T]>) -> &'a Self { |
| 13 | + <Self as typeob::PyTypeObject>::init_type(); |
| 14 | + let type_ob = <Self as typeob::PyTypeInfo>::type_object() as *mut _; |
| 15 | + let base = ffi::_PyObject_New(type_ob); |
| 16 | + *base = ffi::PyObject_HEAD_INIT; |
| 17 | + (*base).ob_type = <Self as typeob::PyTypeInfo>::type_object() as *mut _; |
| 18 | + let self_ = base as *mut SliceBox<T>; |
| 19 | + (*self_).inner = Box::into_raw(box_); |
| 20 | + &*self_ |
| 21 | + } |
| 22 | + pub(crate) fn data(&self) -> *mut c_void { |
| 23 | + self.inner as *mut c_void |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl<T> typeob::PyTypeInfo for SliceBox<T> { |
| 28 | + type Type = (); |
| 29 | + type BaseType = pyo3::PyObjectRef; |
| 30 | + const NAME: &'static str = "SliceBox"; |
| 31 | + const DESCRIPTION: &'static str = "Memory store for PyArray made by IntoPyArray."; |
| 32 | + const FLAGS: usize = 0; |
| 33 | + const SIZE: usize = { Self::OFFSET as usize + std::mem::size_of::<Self>() + 0 + 0 }; |
| 34 | + const OFFSET: isize = 0; |
| 35 | + #[inline] |
| 36 | + unsafe fn type_object() -> &'static mut ::pyo3::ffi::PyTypeObject { |
| 37 | + static mut TYPE_OBJECT: ::pyo3::ffi::PyTypeObject = ::pyo3::ffi::PyTypeObject_INIT; |
| 38 | + &mut TYPE_OBJECT |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl<T: TypeNum> typeob::PyTypeObject for SliceBox<T> { |
| 43 | + #[inline(always)] |
| 44 | + fn init_type() { |
| 45 | + static START: std::sync::Once = std::sync::ONCE_INIT; |
| 46 | + START.call_once(|| { |
| 47 | + let ty = unsafe { <Self as typeob::PyTypeInfo>::type_object() }; |
| 48 | + if (ty.tp_flags & ffi::Py_TPFLAGS_READY) == 0 { |
| 49 | + let gil = Python::acquire_gil(); |
| 50 | + let py = gil.python(); |
| 51 | + let mod_name = format!("rust_numpy.{:?}", T::npy_data_type()); |
| 52 | + typeob::initialize_type::<Self>(py, Some(&mod_name)) |
| 53 | + .map_err(|e| e.print(py)) |
| 54 | + .expect("Failed to initialize SliceBox"); |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl<T> ToPyPointer for SliceBox<T> { |
| 61 | + #[inline] |
| 62 | + fn as_ptr(&self) -> *mut ffi::PyObject { |
| 63 | + &self.ob_base as *const _ as *mut _ |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl<T> PyObjectAlloc<SliceBox<T>> for SliceBox<T> { |
| 68 | + /// Calls the rust destructor for the object. |
| 69 | + unsafe fn drop(py: Python, obj: *mut ffi::PyObject) { |
| 70 | + let data = (*(obj as *mut SliceBox<T>)).inner; |
| 71 | + let box_ = Box::from_raw(data); |
| 72 | + drop(box_); |
| 73 | + <Self as typeob::PyTypeInfo>::BaseType::drop(py, obj); |
| 74 | + } |
| 75 | + unsafe fn dealloc(py: Python, obj: *mut ffi::PyObject) { |
| 76 | + Self::drop(py, obj); |
| 77 | + ffi::PyObject_Free(obj as *mut c_void); |
| 78 | + } |
| 79 | +} |
0 commit comments