11//! Untyped safe interface for NumPy ndarray
22
3- use cpython:: * ;
43use ndarray:: * ;
54use npyffi;
6- use pyffi ;
5+ use pyo3 :: * ;
76
87use std:: os:: raw:: c_void;
98use std:: ptr:: null_mut;
@@ -13,22 +12,19 @@ use super::*;
1312
1413/// Untyped safe interface for NumPy ndarray.
1514pub struct PyArray ( PyObject ) ;
15+ pyobject_native_type ! ( PyArray , * npyffi:: PyArray_Type_Ptr , npyffi:: PyArray_Check ) ;
1616
1717impl PyArray {
18- pub fn as_ptr ( & self ) -> * mut npyffi:: PyArrayObject {
19- self . 0 . as_ptr ( ) as * mut npyffi :: PyArrayObject
18+ pub fn as_array_ptr ( & self ) -> * mut npyffi:: PyArrayObject {
19+ self . as_ptr ( ) as _
2020 }
2121
22- pub fn steal_ptr ( self ) -> * mut npyffi:: PyArrayObject {
23- self . 0 . steal_ptr ( ) as * mut npyffi:: PyArrayObject
24- }
25-
26- pub unsafe fn from_owned_ptr ( py : Python , ptr : * mut pyffi:: PyObject ) -> Self {
22+ pub unsafe fn from_owned_ptr ( py : Python , ptr : * mut pyo3:: ffi:: PyObject ) -> Self {
2723 let obj = PyObject :: from_owned_ptr ( py, ptr) ;
2824 PyArray ( obj)
2925 }
3026
31- pub unsafe fn from_borrowed_ptr ( py : Python , ptr : * mut pyffi :: PyObject ) -> Self {
27+ pub unsafe fn from_borrowed_ptr ( py : Python , ptr : * mut pyo3 :: ffi :: PyObject ) -> Self {
3228 let obj = PyObject :: from_borrowed_ptr ( py, ptr) ;
3329 PyArray ( obj)
3430 }
@@ -37,7 +33,7 @@ impl PyArray {
3733 ///
3834 /// https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_NDIM
3935 pub fn ndim ( & self ) -> usize {
40- let ptr = self . as_ptr ( ) ;
36+ let ptr = self . as_array_ptr ( ) ;
4137 unsafe { ( * ptr) . nd as usize }
4238 }
4339
@@ -46,7 +42,7 @@ impl PyArray {
4642 /// https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_DIMS
4743 pub fn dims ( & self ) -> Vec < usize > {
4844 let n = self . ndim ( ) ;
49- let ptr = self . as_ptr ( ) ;
45+ let ptr = self . as_array_ptr ( ) ;
5046 let dims = unsafe {
5147 let p = ( * ptr) . dimensions ;
5248 :: std:: slice:: from_raw_parts ( p, n)
@@ -69,7 +65,7 @@ impl PyArray {
6965 /// - https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.strides.html#numpy.ndarray.strides
7066 pub fn strides ( & self ) -> Vec < isize > {
7167 let n = self . ndim ( ) ;
72- let ptr = self . as_ptr ( ) ;
68+ let ptr = self . as_array_ptr ( ) ;
7369 let dims = unsafe {
7470 let p = ( * ptr) . strides ;
7571 :: std:: slice:: from_raw_parts ( p, n)
@@ -78,7 +74,7 @@ impl PyArray {
7874 }
7975
8076 unsafe fn data < T > ( & self ) -> * mut T {
81- let ptr = self . as_ptr ( ) ;
77+ let ptr = self . as_array_ptr ( ) ;
8278 ( * ptr) . data as * mut T
8379 }
8480
@@ -94,7 +90,7 @@ impl PyArray {
9490
9591 pub fn typenum ( & self ) -> i32 {
9692 unsafe {
97- let descr = ( * self . as_ptr ( ) ) . descr ;
93+ let descr = ( * self . as_array_ptr ( ) ) . descr ;
9894 ( * descr) . type_num
9995 }
10096 }
@@ -143,7 +139,7 @@ impl PyArray {
143139 unsafe { Ok ( :: std:: slice:: from_raw_parts_mut ( self . data ( ) , self . len ( ) ) ) }
144140 }
145141
146- pub unsafe fn new_ < T : TypeNum > (
142+ pub unsafe fn new_ < T : types :: TypeNum > (
147143 py : Python ,
148144 np : & PyArrayModule ,
149145 dims : & [ usize ] ,
@@ -204,75 +200,3 @@ impl PyArray {
204200 }
205201 }
206202}
207-
208- impl < ' source > FromPyObject < ' source > for PyArray {
209- fn extract ( py : Python , obj : & ' source PyObject ) -> PyResult < Self > {
210- Ok ( obj. clone_ref ( py) . cast_into :: < PyArray > ( py) ?)
211- }
212- }
213-
214- impl < ' source > FromPyObject < ' source > for & ' source PyArray {
215- fn extract ( py : Python , obj : & ' source PyObject ) -> PyResult < Self > {
216- Ok ( obj. cast_as :: < PyArray > ( py) ?)
217- }
218- }
219-
220- impl ToPyObject for PyArray {
221- type ObjectType = Self ;
222-
223- fn to_py_object ( & self , py : Python ) -> Self {
224- PyClone :: clone_ref ( self , py)
225- }
226- }
227-
228- impl PythonObject for PyArray {
229- #[ inline]
230- fn as_object ( & self ) -> & PyObject {
231- & self . 0
232- }
233-
234- #[ inline]
235- fn into_object ( self ) -> PyObject {
236- self . 0
237- }
238-
239- #[ inline]
240- unsafe fn unchecked_downcast_from ( obj : PyObject ) -> Self {
241- PyArray ( obj)
242- }
243-
244- #[ inline]
245- unsafe fn unchecked_downcast_borrow_from < ' a > ( obj : & ' a PyObject ) -> & ' a Self {
246- :: std:: mem:: transmute ( obj)
247- }
248- }
249-
250- impl PythonObjectWithCheckedDowncast for PyArray {
251- fn downcast_from < ' p > (
252- py : Python < ' p > ,
253- obj : PyObject ,
254- ) -> Result < PyArray , PythonObjectDowncastError < ' p > > {
255- let np = PyArrayModule :: import ( py) . unwrap ( ) ;
256- unsafe {
257- if npyffi:: PyArray_Check ( & np, obj. as_ptr ( ) ) != 0 {
258- Ok ( PyArray ( obj) )
259- } else {
260- Err ( PythonObjectDowncastError ( py) )
261- }
262- }
263- }
264-
265- fn downcast_borrow_from < ' a , ' p > (
266- py : Python < ' p > ,
267- obj : & ' a PyObject ,
268- ) -> Result < & ' a PyArray , PythonObjectDowncastError < ' p > > {
269- let np = PyArrayModule :: import ( py) . unwrap ( ) ;
270- unsafe {
271- if npyffi:: PyArray_Check ( & np, obj. as_ptr ( ) ) != 0 {
272- Ok ( :: std:: mem:: transmute ( obj) )
273- } else {
274- Err ( PythonObjectDowncastError ( py) )
275- }
276- }
277- }
278- }
0 commit comments