11use pyo3:: class:: methods:: { PyMethodDefType , PyMethodsProtocol } ;
2- use pyo3:: { ffi, type_object, types:: PyAny , AsPyPointer , PyObjectAlloc , Python } ;
3- use std:: os:: raw:: c_void;
2+ use pyo3:: pyclass:: { PyClass , PyClassAlloc } ;
3+ use pyo3:: pyclass_slots:: PyClassDummySlot ;
4+ use pyo3:: { ffi, type_object, types:: PyAny , PyCell , PyClassInitializer } ;
45
5- /// It's a memory store for IntoPyArray.
6- /// See IntoPyArray's doc for what concretely this type is for.
7- #[ repr( C ) ]
86pub ( crate ) struct SliceBox < T > {
9- ob_base : ffi:: PyObject ,
10- inner : * mut [ T ] ,
7+ pub ( crate ) data : * mut [ T ] ,
118}
129
1310impl < T > SliceBox < T > {
14- pub ( crate ) unsafe fn new < ' a > ( box_ : Box < [ T ] > ) -> & ' a Self {
15- let type_ob = <Self as type_object:: PyTypeObject >:: init_type ( ) . as_ptr ( ) ;
16- let base = ffi:: _PyObject_New ( type_ob) ;
17- * base = ffi:: PyObject_HEAD_INIT ;
18- ( * base) . ob_type = type_ob;
19- let self_ = base as * mut SliceBox < T > ;
20- ( * self_) . inner = Box :: into_raw ( box_) ;
21- & * self_
11+ pub ( crate ) fn new ( value : Box < [ T ] > ) -> Self {
12+ SliceBox {
13+ data : Box :: into_raw ( value) ,
14+ }
2215 }
23- pub ( crate ) fn data ( & self ) -> * mut c_void {
24- self . inner as * mut c_void
16+ }
17+
18+ impl < T > Drop for SliceBox < T > {
19+ fn drop ( & mut self ) {
20+ let _boxed_slice = unsafe { Box :: from_raw ( self . data ) } ;
2521 }
2622}
2723
28- impl < T > type_object:: PyTypeInfo for SliceBox < T > {
24+ impl < T > PyClassAlloc for SliceBox < T > { }
25+
26+ impl < T > PyClass for SliceBox < T > {
27+ type Dict = PyClassDummySlot ;
28+ type WeakRef = PyClassDummySlot ;
29+ type BaseNativeType = PyAny ;
30+ }
31+
32+ unsafe impl < T > type_object:: PyTypeInfo for SliceBox < T > {
2933 type Type = ( ) ;
3034 type BaseType = PyAny ;
35+ type BaseLayout = pyo3:: pycell:: PyCellBase < PyAny > ;
36+ type Layout = PyCell < Self > ;
37+ type Initializer = PyClassInitializer < Self > ;
38+ type AsRefTarget = PyCell < Self > ;
3139 const NAME : & ' static str = "SliceBox" ;
3240 const MODULE : Option < & ' static str > = Some ( "_rust_numpy" ) ;
33- const DESCRIPTION : & ' static str = "Memory store for PyArray using rust's Box<[T]>. " ;
41+ const DESCRIPTION : & ' static str = "Memory store for PyArray using rust's Box<[T]> \0 " ;
3442 const FLAGS : usize = 0 ;
35- const SIZE : usize = std:: mem:: size_of :: < Self > ( ) ;
36- const OFFSET : isize = 0 ;
43+
3744 #[ inline]
38- unsafe fn type_object ( ) -> & ' static mut ffi:: PyTypeObject {
39- static mut TYPE_OBJECT : :: pyo3:: ffi:: PyTypeObject = :: pyo3:: ffi:: PyTypeObject_INIT ;
40- & mut TYPE_OBJECT
45+ fn type_object ( ) -> & ' static ffi:: PyTypeObject {
46+ use pyo3:: type_object:: LazyStaticType ;
47+ static TYPE_OBJECT : LazyStaticType = LazyStaticType :: new ( ) ;
48+ TYPE_OBJECT . get_or_init :: < Self > ( )
4149 }
4250}
4351
@@ -46,24 +54,3 @@ impl<T> PyMethodsProtocol for SliceBox<T> {
4654 Vec :: new ( )
4755 }
4856}
49-
50- impl < T > AsPyPointer for SliceBox < T > {
51- #[ inline]
52- fn as_ptr ( & self ) -> * mut ffi:: PyObject {
53- & self . ob_base as * const _ as * mut _
54- }
55- }
56-
57- impl < T > PyObjectAlloc for SliceBox < T > {
58- /// Calls the rust destructor for the object.
59- unsafe fn drop ( py : Python < ' _ > , obj : * mut ffi:: PyObject ) {
60- let data = ( * ( obj as * mut SliceBox < T > ) ) . inner ;
61- let boxed_slice = Box :: from_raw ( data) ;
62- drop ( boxed_slice) ;
63- <Self as type_object:: PyTypeInfo >:: BaseType :: drop ( py, obj) ;
64- }
65- unsafe fn dealloc ( py : Python < ' _ > , obj : * mut ffi:: PyObject ) {
66- Self :: drop ( py, obj) ;
67- ffi:: PyObject_Free ( obj as * mut c_void ) ;
68- }
69- }
0 commit comments