11use 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 } ;
34use std:: os:: raw:: c_void;
5+ use std:: ptr:: NonNull ;
46
57/// It's a memory store for IntoPyArray.
68/// See IntoPyArray's doc for what concretely this type is for.
@@ -12,8 +14,8 @@ pub(crate) struct SliceBox<T> {
1214
1315impl < T > SliceBox < T > {
1416 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 _ ;
1719 let base = ffi:: _PyObject_New ( type_ob) ;
1820 * base = ffi:: PyObject_HEAD_INIT ;
1921 ( * base) . ob_type = type_ob;
@@ -26,9 +28,9 @@ impl<T> SliceBox<T> {
2628 }
2729}
2830
29- impl < T > typeob :: PyTypeInfo for SliceBox < T > {
31+ impl < T > type_object :: PyTypeInfo for SliceBox < T > {
3032 type Type = ( ) ;
31- type BaseType = PyObjectRef ;
33+ type BaseType = PyAny ;
3234 const NAME : & ' static str = "SliceBox" ;
3335 const DESCRIPTION : & ' static str = "Memory store for PyArray using rust's Box<[T]>." ;
3436 const FLAGS : usize = 0 ;
@@ -41,38 +43,42 @@ impl<T> typeob::PyTypeInfo for SliceBox<T> {
4143 }
4244}
4345
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+ {
4550 #[ 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+ // })
5965 }
6066}
6167
62- impl < T > ToPyPointer for SliceBox < T > {
68+ impl < T > AsPyPointer for SliceBox < T > {
6369 #[ inline]
6470 fn as_ptr ( & self ) -> * mut ffi:: PyObject {
6571 & self . ob_base as * const _ as * mut _
6672 }
6773}
6874
69- impl < T > PyObjectAlloc < SliceBox < T > > for SliceBox < T > {
75+ impl < T > PyObjectAlloc for SliceBox < T > {
7076 /// Calls the rust destructor for the object.
7177 unsafe fn drop ( py : Python , obj : * mut ffi:: PyObject ) {
7278 let data = ( * ( obj as * mut SliceBox < T > ) ) . inner ;
7379 let boxed_slice = Box :: from_raw ( data) ;
7480 drop ( boxed_slice) ;
75- <Self as typeob :: PyTypeInfo >:: BaseType :: drop ( py, obj) ;
81+ <Self as type_object :: PyTypeInfo >:: BaseType :: drop ( py, obj) ;
7682 }
7783 unsafe fn dealloc ( py : Python , obj : * mut ffi:: PyObject ) {
7884 Self :: drop ( py, obj) ;
0 commit comments