@@ -14,13 +14,15 @@ use super::*;
14
14
/// Untyped safe interface for NumPy ndarray.
15
15
pub struct PyArray ( PyObject ) ;
16
16
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 ( )
20
20
}
21
+ }
21
22
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 _
24
26
}
25
27
26
28
pub unsafe fn from_owned_ptr ( py : Python , ptr : * mut pyo3:: ffi:: PyObject ) -> Self {
@@ -37,7 +39,7 @@ impl PyArray {
37
39
///
38
40
/// https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_NDIM
39
41
pub fn ndim ( & self ) -> usize {
40
- let ptr = self . as_ptr ( ) ;
42
+ let ptr = self . as_array_ptr ( ) ;
41
43
unsafe { ( * ptr) . nd as usize }
42
44
}
43
45
@@ -46,7 +48,7 @@ impl PyArray {
46
48
/// https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_DIMS
47
49
pub fn dims ( & self ) -> Vec < usize > {
48
50
let n = self . ndim ( ) ;
49
- let ptr = self . as_ptr ( ) ;
51
+ let ptr = self . as_array_ptr ( ) ;
50
52
let dims = unsafe {
51
53
let p = ( * ptr) . dimensions ;
52
54
:: std:: slice:: from_raw_parts ( p, n)
@@ -69,7 +71,7 @@ impl PyArray {
69
71
/// - https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.strides.html#numpy.ndarray.strides
70
72
pub fn strides ( & self ) -> Vec < isize > {
71
73
let n = self . ndim ( ) ;
72
- let ptr = self . as_ptr ( ) ;
74
+ let ptr = self . as_array_ptr ( ) ;
73
75
let dims = unsafe {
74
76
let p = ( * ptr) . strides ;
75
77
:: std:: slice:: from_raw_parts ( p, n)
@@ -78,7 +80,7 @@ impl PyArray {
78
80
}
79
81
80
82
unsafe fn data < T > ( & self ) -> * mut T {
81
- let ptr = self . as_ptr ( ) ;
83
+ let ptr = self . as_array_ptr ( ) ;
82
84
( * ptr) . data as * mut T
83
85
}
84
86
@@ -94,7 +96,7 @@ impl PyArray {
94
96
95
97
pub fn typenum ( & self ) -> i32 {
96
98
unsafe {
97
- let descr = ( * self . as_ptr ( ) ) . descr ;
99
+ let descr = ( * self . as_array_ptr ( ) ) . descr ;
98
100
( * descr) . type_num
99
101
}
100
102
}
@@ -143,7 +145,7 @@ impl PyArray {
143
145
unsafe { Ok ( :: std:: slice:: from_raw_parts_mut ( self . data ( ) , self . len ( ) ) ) }
144
146
}
145
147
146
- pub unsafe fn new_ < T : TypeNum > (
148
+ pub unsafe fn new_ < T : types :: TypeNum > (
147
149
py : Python ,
148
150
np : & PyArrayModule ,
149
151
dims : & [ usize ] ,
@@ -204,75 +206,3 @@ impl PyArray {
204
206
}
205
207
}
206
208
}
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