1
1
use crate :: types:: TypeNum ;
2
- use pyo3:: { ffi, typeob, types:: PyObjectRef , PyObjectAlloc , Python , ToPyPointer } ;
2
+ use pyo3:: class:: methods:: PyMethodsProtocol ;
3
+ use pyo3:: { ffi, type_object, types:: PyAny , AsPyPointer , PyObjectAlloc , Python } ;
3
4
use std:: os:: raw:: c_void;
5
+ use std:: ptr:: NonNull ;
4
6
5
7
/// It's a memory store for IntoPyArray.
6
8
/// See IntoPyArray's doc for what concretely this type is for.
@@ -12,8 +14,8 @@ pub(crate) struct SliceBox<T> {
12
14
13
15
impl < T > SliceBox < T > {
14
16
pub ( crate ) unsafe fn new < ' a > ( box_ : Box < [ T ] > ) -> & ' a Self {
15
- <Self as typeob :: PyTypeObject >:: init_type ( ) ;
16
- let type_ob = <Self as typeob :: PyTypeInfo >:: type_object ( ) as * mut _ ;
17
+ // <Self as type_object ::PyTypeObject>::init_type();
18
+ let type_ob = <Self as type_object :: PyTypeInfo >:: type_object ( ) as * mut _ ;
17
19
let base = ffi:: _PyObject_New ( type_ob) ;
18
20
* base = ffi:: PyObject_HEAD_INIT ;
19
21
( * base) . ob_type = type_ob;
@@ -26,9 +28,9 @@ impl<T> SliceBox<T> {
26
28
}
27
29
}
28
30
29
- impl < T > typeob :: PyTypeInfo for SliceBox < T > {
31
+ impl < T > type_object :: PyTypeInfo for SliceBox < T > {
30
32
type Type = ( ) ;
31
- type BaseType = PyObjectRef ;
33
+ type BaseType = PyAny ;
32
34
const NAME : & ' static str = "SliceBox" ;
33
35
const DESCRIPTION : & ' static str = "Memory store for PyArray using rust's Box<[T]>." ;
34
36
const FLAGS : usize = 0 ;
@@ -41,38 +43,42 @@ impl<T> typeob::PyTypeInfo for SliceBox<T> {
41
43
}
42
44
}
43
45
44
- impl < T : TypeNum > typeob:: PyTypeCreate for SliceBox < T > {
46
+ impl < T : TypeNum > type_object:: PyTypeObject for SliceBox < T >
47
+ where
48
+ SliceBox < T > : PyMethodsProtocol ,
49
+ {
45
50
#[ inline( always) ]
46
- fn init_type ( ) {
47
- static START : std:: sync:: Once = std:: sync:: ONCE_INIT ;
48
- START . call_once ( || {
49
- let ty = unsafe { <Self as typeob:: PyTypeInfo >:: type_object ( ) } ;
50
- if ( ty. tp_flags & ffi:: Py_TPFLAGS_READY ) == 0 {
51
- let gil = Python :: acquire_gil ( ) ;
52
- let py = gil. python ( ) ;
53
- let mod_name = format ! ( "rust_numpy.{:?}" , T :: npy_data_type( ) ) ;
54
- typeob:: initialize_type :: < Self > ( py, Some ( & mod_name) )
55
- . map_err ( |e| e. print ( py) )
56
- . expect ( "Failed to initialize SliceBox" ) ;
57
- }
58
- } ) ;
51
+ fn init_type ( ) -> NonNull < ffi:: PyTypeObject > {
52
+ // static START: std::sync::Once = std::sync::ONCE_INIT;
53
+ // START.call_once(|| -> NonNull<ffi::PyTypeObject> {
54
+ let ty = unsafe { <Self as type_object:: PyTypeInfo >:: type_object ( ) } ;
55
+ if ( ty. tp_flags & ffi:: Py_TPFLAGS_READY ) == 0 {
56
+ let gil = Python :: acquire_gil ( ) ;
57
+ let py = gil. python ( ) ;
58
+ // let mod_name = format!("rust_numpy.{:?}", T::npy_data_type());
59
+ type_object:: initialize_type :: < Self > ( py)
60
+ . map_err ( |e| e. print ( py) )
61
+ . expect ( "Failed to initialize SliceBox" ) ;
62
+ }
63
+ unsafe { NonNull :: new_unchecked ( ty) }
64
+ // })
59
65
}
60
66
}
61
67
62
- impl < T > ToPyPointer for SliceBox < T > {
68
+ impl < T > AsPyPointer for SliceBox < T > {
63
69
#[ inline]
64
70
fn as_ptr ( & self ) -> * mut ffi:: PyObject {
65
71
& self . ob_base as * const _ as * mut _
66
72
}
67
73
}
68
74
69
- impl < T > PyObjectAlloc < SliceBox < T > > for SliceBox < T > {
75
+ impl < T > PyObjectAlloc for SliceBox < T > {
70
76
/// Calls the rust destructor for the object.
71
77
unsafe fn drop ( py : Python , obj : * mut ffi:: PyObject ) {
72
78
let data = ( * ( obj as * mut SliceBox < T > ) ) . inner ;
73
79
let boxed_slice = Box :: from_raw ( data) ;
74
80
drop ( boxed_slice) ;
75
- <Self as typeob :: PyTypeInfo >:: BaseType :: drop ( py, obj) ;
81
+ <Self as type_object :: PyTypeInfo >:: BaseType :: drop ( py, obj) ;
76
82
}
77
83
unsafe fn dealloc ( py : Python , obj : * mut ffi:: PyObject ) {
78
84
Self :: drop ( py, obj) ;
0 commit comments