1
1
use 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 , PyClassShell } ;
3
+ use pyo3:: pyclass_slots:: PyClassDummySlot ;
4
+ use pyo3:: { ffi, type_object, types:: PyAny , PyClassInitializer , PyResult , Python } ;
4
5
5
- /// It's a memory store for IntoPyArray.
6
- /// See IntoPyArray's doc for what concretely this type is for.
7
- #[ repr( C ) ]
8
6
pub ( crate ) struct SliceBox < T > {
9
- ob_base : ffi:: PyObject ,
10
- inner : * mut [ T ] ,
7
+ pub ( crate ) data : * mut [ T ] ,
11
8
}
12
9
13
10
impl < 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 ) unsafe fn new (
12
+ py : Python < ' _ > ,
13
+ value : Box < [ T ] > ,
14
+ ) -> PyResult < * mut PyClassShell < SliceBox < T > > > {
15
+ let value = SliceBox {
16
+ data : Box :: into_raw ( value ) ,
17
+ } ;
18
+ PyClassInitializer :: from ( value ) . create_shell ( py )
22
19
}
23
- pub ( crate ) fn data ( & self ) -> * mut c_void {
24
- self . inner as * mut c_void
20
+ }
21
+
22
+ impl < T > Drop for SliceBox < T > {
23
+ fn drop ( & mut self ) {
24
+ let _boxed_slice = unsafe { Box :: from_raw ( self . data ) } ;
25
25
}
26
26
}
27
27
28
+ impl < T > PyClassAlloc for SliceBox < T > { }
29
+
30
+ impl < T > PyClass for SliceBox < T > {
31
+ type Dict = PyClassDummySlot ;
32
+ type WeakRef = PyClassDummySlot ;
33
+ }
34
+
28
35
impl < T > type_object:: PyTypeInfo for SliceBox < T > {
29
36
type Type = ( ) ;
30
37
type BaseType = PyAny ;
38
+ type ConcreteLayout = PyClassShell < Self > ;
39
+ type Initializer = PyClassInitializer < Self > ;
31
40
const NAME : & ' static str = "SliceBox" ;
32
41
const MODULE : Option < & ' static str > = Some ( "_rust_numpy" ) ;
33
42
const DESCRIPTION : & ' static str = "Memory store for PyArray using rust's Box<[T]>." ;
34
43
const FLAGS : usize = 0 ;
35
- const SIZE : usize = std:: mem:: size_of :: < Self > ( ) ;
36
- const OFFSET : isize = 0 ;
44
+
37
45
#[ inline]
38
46
unsafe fn type_object ( ) -> & ' static mut ffi:: PyTypeObject {
39
47
static mut TYPE_OBJECT : :: pyo3:: ffi:: PyTypeObject = :: pyo3:: ffi:: PyTypeObject_INIT ;
@@ -46,24 +54,3 @@ impl<T> PyMethodsProtocol for SliceBox<T> {
46
54
Vec :: new ( )
47
55
}
48
56
}
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