@@ -14,13 +14,15 @@ use super::*;
1414/// Untyped safe interface for NumPy ndarray.
1515pub struct PyArray ( PyObject ) ;
1616
17- impl PyArray {
18- pub fn as_ptr ( & self ) -> * mut npyffi :: PyArrayObject {
19- self . 0 . as_ptr ( ) as * mut npyffi :: PyArrayObject
17+ impl ToPyPointer for PyArray {
18+ fn as_ptr ( & self ) -> * mut ffi :: PyObject {
19+ self . 0 . as_ptr ( )
2020 }
21+ }
2122
22- pub fn steal_ptr ( self ) -> * mut npyffi:: PyArrayObject {
23- self . 0 . steal_ptr ( ) as * mut npyffi:: PyArrayObject
23+ impl PyArray {
24+ pub fn as_array_ptr ( & self ) -> * mut npyffi:: PyArrayObject {
25+ self . as_ptr ( ) as _
2426 }
2527
2628 pub unsafe fn from_owned_ptr ( py : Python , ptr : * mut pyo3:: ffi:: PyObject ) -> Self {
@@ -37,7 +39,7 @@ impl PyArray {
3739 ///
3840 /// https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_NDIM
3941 pub fn ndim ( & self ) -> usize {
40- let ptr = self . as_ptr ( ) ;
42+ let ptr = self . as_array_ptr ( ) ;
4143 unsafe { ( * ptr) . nd as usize }
4244 }
4345
@@ -46,7 +48,7 @@ impl PyArray {
4648 /// https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_DIMS
4749 pub fn dims ( & self ) -> Vec < usize > {
4850 let n = self . ndim ( ) ;
49- let ptr = self . as_ptr ( ) ;
51+ let ptr = self . as_array_ptr ( ) ;
5052 let dims = unsafe {
5153 let p = ( * ptr) . dimensions ;
5254 :: std:: slice:: from_raw_parts ( p, n)
@@ -69,7 +71,7 @@ impl PyArray {
6971 /// - https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.strides.html#numpy.ndarray.strides
7072 pub fn strides ( & self ) -> Vec < isize > {
7173 let n = self . ndim ( ) ;
72- let ptr = self . as_ptr ( ) ;
74+ let ptr = self . as_array_ptr ( ) ;
7375 let dims = unsafe {
7476 let p = ( * ptr) . strides ;
7577 :: std:: slice:: from_raw_parts ( p, n)
@@ -78,7 +80,7 @@ impl PyArray {
7880 }
7981
8082 unsafe fn data < T > ( & self ) -> * mut T {
81- let ptr = self . as_ptr ( ) ;
83+ let ptr = self . as_array_ptr ( ) ;
8284 ( * ptr) . data as * mut T
8385 }
8486
@@ -94,7 +96,7 @@ impl PyArray {
9496
9597 pub fn typenum ( & self ) -> i32 {
9698 unsafe {
97- let descr = ( * self . as_ptr ( ) ) . descr ;
99+ let descr = ( * self . as_array_ptr ( ) ) . descr ;
98100 ( * descr) . type_num
99101 }
100102 }
@@ -143,7 +145,7 @@ impl PyArray {
143145 unsafe { Ok ( :: std:: slice:: from_raw_parts_mut ( self . data ( ) , self . len ( ) ) ) }
144146 }
145147
146- pub unsafe fn new_ < T : TypeNum > (
148+ pub unsafe fn new_ < T : types :: TypeNum > (
147149 py : Python ,
148150 np : & PyArrayModule ,
149151 dims : & [ usize ] ,
@@ -204,75 +206,3 @@ impl PyArray {
204206 }
205207 }
206208}
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