|
1 | 1 | use pyo3::class::impl_::{PyClassImpl, ThreadCheckerStub}; |
2 | 2 | use pyo3::pyclass::PyClass; |
3 | 3 | use pyo3::pyclass_slots::PyClassDummySlot; |
4 | | -use pyo3::{ffi, type_object, types::PyAny, PyCell}; |
| 4 | +use pyo3::type_object::{LazyStaticType, PyTypeInfo}; |
| 5 | +use pyo3::{ffi, types::PyAny, PyCell}; |
5 | 6 |
|
6 | | -pub(crate) struct SliceBox<T> { |
7 | | - pub(crate) data: Box<[T]>, |
| 7 | +pub(crate) struct SliceBox { |
| 8 | + ptr: *mut [u8], |
| 9 | + drop: unsafe fn(*mut [u8]), |
8 | 10 | } |
9 | 11 |
|
10 | | -impl<T> SliceBox<T> { |
11 | | - pub(crate) fn new(data: Box<[T]>) -> Self { |
12 | | - Self { data } |
| 12 | +unsafe impl Send for SliceBox {} |
| 13 | + |
| 14 | +impl SliceBox { |
| 15 | + pub(crate) fn new<T: Send>(data: Box<[T]>) -> Self { |
| 16 | + unsafe fn drop_boxed_slice<T>(ptr: *mut [u8]) { |
| 17 | + let _ = Box::from_raw(ptr as *mut [T]); |
| 18 | + } |
| 19 | + |
| 20 | + let ptr = Box::into_raw(data) as *mut [u8]; |
| 21 | + let drop = drop_boxed_slice::<T>; |
| 22 | + |
| 23 | + Self { ptr, drop } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl Drop for SliceBox { |
| 28 | + fn drop(&mut self) { |
| 29 | + unsafe { |
| 30 | + (self.drop)(self.ptr); |
| 31 | + } |
13 | 32 | } |
14 | 33 | } |
15 | 34 |
|
16 | | -impl<T> PyClass for SliceBox<T> |
17 | | -where |
18 | | - T: Send, |
19 | | -{ |
| 35 | +impl PyClass for SliceBox { |
20 | 36 | type Dict = PyClassDummySlot; |
21 | 37 | type WeakRef = PyClassDummySlot; |
22 | 38 | type BaseNativeType = PyAny; |
23 | 39 | } |
24 | 40 |
|
25 | | -impl<T> PyClassImpl for SliceBox<T> |
26 | | -where |
27 | | - T: Send, |
28 | | -{ |
| 41 | +impl PyClassImpl for SliceBox { |
29 | 42 | const DOC: &'static str = "Memory store for PyArray using rust's Box<[T]> \0"; |
30 | 43 |
|
31 | 44 | type BaseType = PyAny; |
32 | 45 | type Layout = PyCell<Self>; |
33 | 46 | type ThreadChecker = ThreadCheckerStub<Self>; |
34 | 47 | } |
35 | 48 |
|
36 | | -unsafe impl<T> type_object::PyTypeInfo for SliceBox<T> |
37 | | -where |
38 | | - T: Send, |
39 | | -{ |
| 49 | +unsafe impl PyTypeInfo for SliceBox { |
40 | 50 | type AsRefTarget = PyCell<Self>; |
41 | 51 | const NAME: &'static str = "SliceBox"; |
42 | 52 | const MODULE: Option<&'static str> = Some("_rust_numpy"); |
43 | 53 |
|
44 | 54 | #[inline] |
45 | 55 | fn type_object_raw(py: pyo3::Python) -> *mut ffi::PyTypeObject { |
46 | | - use pyo3::type_object::LazyStaticType; |
47 | 56 | static TYPE_OBJECT: LazyStaticType = LazyStaticType::new(); |
48 | 57 | TYPE_OBJECT.get_or_init::<Self>(py) |
49 | 58 | } |
|
0 commit comments