@@ -4,19 +4,16 @@ use ndarray::*;
4
4
use num_traits:: AsPrimitive ;
5
5
use pyo3:: { ffi, prelude:: * , type_object, types:: PyAny } ;
6
6
use pyo3:: { AsPyPointer , PyDowncastError , PyNativeType } ;
7
- use std:: iter:: ExactSizeIterator ;
8
- use std:: marker:: PhantomData ;
9
- use std:: mem;
10
- use std:: os:: raw:: c_int;
11
- use std:: ptr;
7
+ use std:: { iter:: ExactSizeIterator , marker:: PhantomData } ;
8
+ use std:: { mem, os:: raw:: c_int, ptr, slice} ;
12
9
13
10
use crate :: convert:: { IntoPyArray , NpyIndex , ToNpyDims , ToPyArray } ;
14
11
use crate :: error:: { ErrorKind , IntoPyResult } ;
15
12
use crate :: slice_box:: SliceBox ;
16
13
use crate :: types:: { NpyDataType , TypeNum } ;
17
14
18
15
/// A safe, static-typed interface for
19
- /// [NumPy ndarray](https://docs.scipy. org/doc/numpy /reference/arrays.ndarray.html).
16
+ /// [NumPy ndarray](https://numpy. org/doc/stable /reference/arrays.ndarray.html).
20
17
///
21
18
/// # Memory location
22
19
///
@@ -77,7 +74,7 @@ use crate::types::{NpyDataType, TypeNum};
77
74
/// );
78
75
/// # }
79
76
/// ```
80
- pub struct PyArray < T , D > ( PyObject , PhantomData < T > , PhantomData < D > ) ;
77
+ pub struct PyArray < T , D > ( PyAny , PhantomData < T > , PhantomData < D > ) ;
81
78
82
79
/// one-dimensional array
83
80
pub type PyArray1 < T > = PyArray < T , Ix1 > ;
@@ -113,7 +110,7 @@ pyobject_native_type_convert!(
113
110
114
111
pyobject_native_type_named ! ( PyArray <T , D >, T , D ) ;
115
112
116
- impl < ' a , T , D > :: std:: convert:: From < & ' a PyArray < T , D > > for & ' a PyAny {
113
+ impl < ' a , T , D > std:: convert:: From < & ' a PyArray < T , D > > for & ' a PyAny {
117
114
fn from ( ob : & ' a PyArray < T , D > ) -> Self {
118
115
unsafe { & * ( ob as * const PyArray < T , D > as * const PyAny ) }
119
116
}
@@ -139,8 +136,8 @@ impl<'a, T: TypeNum, D: Dimension> FromPyObject<'a> for &'a PyArray<T, D> {
139
136
}
140
137
141
138
impl < T , D > IntoPy < PyObject > for PyArray < T , D > {
142
- fn into_py ( self , _py : Python < ' _ > ) -> PyObject {
143
- self . 0
139
+ fn into_py ( self , py : Python < ' _ > ) -> PyObject {
140
+ unsafe { PyObject :: from_borrowed_ptr ( py , self . as_ptr ( ) ) }
144
141
}
145
142
}
146
143
@@ -226,7 +223,7 @@ impl<T, D> PyArray<T, D> {
226
223
227
224
/// Returns the number of dimensions in the array.
228
225
///
229
- /// Same as [numpy.ndarray.ndim](https://docs.scipy. org/doc/numpy /reference/generated/numpy.ndarray.ndim.html)
226
+ /// Same as [numpy.ndarray.ndim](https://numpy. org/doc/stable /reference/generated/numpy.ndarray.ndim.html)
230
227
///
231
228
/// # Example
232
229
/// ```
@@ -237,15 +234,15 @@ impl<T, D> PyArray<T, D> {
237
234
/// assert_eq!(arr.ndim(), 3);
238
235
/// # }
239
236
/// ```
240
- // C API: https://docs.scipy. org/doc/numpy /reference/c-api. array.html#c.PyArray_NDIM
237
+ // C API: https://numpy. org/doc/stable /reference/c-api/ array.html#c.PyArray_NDIM
241
238
pub fn ndim ( & self ) -> usize {
242
239
let ptr = self . as_array_ptr ( ) ;
243
240
unsafe { ( * ptr) . nd as usize }
244
241
}
245
242
246
243
/// Returns a slice which contains how many bytes you need to jump to the next row.
247
244
///
248
- /// Same as [numpy.ndarray.strides](https://docs.scipy. org/doc/numpy /reference/generated/numpy.ndarray.strides.html)
245
+ /// Same as [numpy.ndarray.strides](https://numpy. org/doc/stable /reference/generated/numpy.ndarray.strides.html)
249
246
/// # Example
250
247
/// ```
251
248
/// # fn main() {
@@ -255,19 +252,19 @@ impl<T, D> PyArray<T, D> {
255
252
/// assert_eq!(arr.strides(), &[240, 48, 8]);
256
253
/// # }
257
254
/// ```
258
- // C API: https://docs.scipy. org/doc/numpy /reference/c-api. array.html#c.PyArray_STRIDES
255
+ // C API: https://numpy. org/doc/stable /reference/c-api/ array.html#c.PyArray_STRIDES
259
256
pub fn strides ( & self ) -> & [ isize ] {
260
257
let n = self . ndim ( ) ;
261
258
let ptr = self . as_array_ptr ( ) ;
262
259
unsafe {
263
260
let p = ( * ptr) . strides ;
264
- :: std :: slice:: from_raw_parts ( p, n)
261
+ slice:: from_raw_parts ( p, n)
265
262
}
266
263
}
267
264
268
265
/// Returns a slice which contains dimmensions of the array.
269
266
///
270
- /// Same as [numpy.ndarray.shape](https://docs.scipy. org/doc/numpy /reference/generated/numpy.ndarray.shape.html)
267
+ /// Same as [numpy.ndarray.shape](https://numpy. org/doc/stable /reference/generated/numpy.ndarray.shape.html)
271
268
/// # Example
272
269
/// ```
273
270
/// # fn main() {
@@ -277,13 +274,13 @@ impl<T, D> PyArray<T, D> {
277
274
/// assert_eq!(arr.shape(), &[4, 5, 6]);
278
275
/// # }
279
276
/// ```
280
- // C API: https://docs.scipy. org/doc/numpy /reference/c-api. array.html#c.PyArray_DIMS
277
+ // C API: https://numpy. org/doc/stable /reference/c-api/ array.html#c.PyArray_DIMS
281
278
pub fn shape ( & self ) -> & [ usize ] {
282
279
let n = self . ndim ( ) ;
283
280
let ptr = self . as_array_ptr ( ) ;
284
281
unsafe {
285
282
let p = ( * ptr) . dimensions as * mut usize ;
286
- :: std :: slice:: from_raw_parts ( p, n)
283
+ slice:: from_raw_parts ( p, n)
287
284
}
288
285
}
289
286
@@ -314,7 +311,7 @@ impl<T, D> PyArray<T, D> {
314
311
let ptr = self . as_array_ptr ( ) ;
315
312
unsafe {
316
313
let p = ( * ptr) . strides ;
317
- :: std :: slice:: from_raw_parts ( p as * const _ , n)
314
+ slice:: from_raw_parts ( p as * const _ , n)
318
315
}
319
316
}
320
317
}
@@ -371,11 +368,11 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
371
368
dims. ndim_cint ( ) ,
372
369
dims. as_dims_ptr ( ) ,
373
370
T :: typenum_default ( ) ,
374
- strides as * mut _ , // strides
375
- ptr:: null_mut ( ) , // data
376
- 0 , // itemsize
377
- flag, // flag
378
- :: std :: ptr:: null_mut ( ) , //obj
371
+ strides as * mut _ , // strides
372
+ ptr:: null_mut ( ) , // data
373
+ 0 , // itemsize
374
+ flag, // flag
375
+ ptr:: null_mut ( ) , //obj
379
376
) ;
380
377
Self :: from_owned_ptr ( py, ptr)
381
378
}
@@ -404,7 +401,7 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
404
401
data_ptr as _ , // data
405
402
mem:: size_of :: < T > ( ) as i32 , // itemsize
406
403
0 , // flag
407
- :: std :: ptr:: null_mut ( ) , //obj
404
+ ptr:: null_mut ( ) , //obj
408
405
) ;
409
406
PY_ARRAY_API . PyArray_SetBaseObject ( ptr as * mut npyffi:: PyArrayObject , cell as _ ) ;
410
407
Self :: from_owned_ptr ( py, ptr)
@@ -415,7 +412,7 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
415
412
/// If `is_fortran` is true, then
416
413
/// a fortran order array is created, otherwise a C-order array is created.
417
414
///
418
- /// See also [PyArray_Zeros](https://docs.scipy. org/doc/numpy /reference/c-api. array.html#c.PyArray_Zeros)
415
+ /// See also [PyArray_Zeros](https://numpy. org/doc/stable /reference/c-api/ array.html#c.PyArray_Zeros)
419
416
///
420
417
/// # Example
421
418
/// ```
@@ -469,7 +466,7 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
469
466
if !self . is_contiguous ( ) {
470
467
Err ( ErrorKind :: NotContiguous )
471
468
} else {
472
- Ok ( unsafe { :: std :: slice:: from_raw_parts ( self . data ( ) , self . len ( ) ) } )
469
+ Ok ( unsafe { slice:: from_raw_parts ( self . data ( ) , self . len ( ) ) } )
473
470
}
474
471
}
475
472
@@ -479,7 +476,7 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
479
476
if !self . is_contiguous ( ) {
480
477
Err ( ErrorKind :: NotContiguous )
481
478
} else {
482
- Ok ( unsafe { :: std :: slice:: from_raw_parts_mut ( self . data ( ) , self . len ( ) ) } )
479
+ Ok ( unsafe { slice:: from_raw_parts_mut ( self . data ( ) , self . len ( ) ) } )
483
480
}
484
481
}
485
482
@@ -1037,9 +1034,9 @@ impl<T: TypeNum, D> PyArray<T, D> {
1037
1034
1038
1035
impl < T : TypeNum + AsPrimitive < f64 > > PyArray < T , Ix1 > {
1039
1036
/// Return evenly spaced values within a given interval.
1040
- /// Same as [numpy.arange](https://docs.scipy. org/doc/numpy /reference/generated/numpy.arange.html).
1037
+ /// Same as [numpy.arange](https://numpy. org/doc/stable /reference/generated/numpy.arange.html).
1041
1038
///
1042
- /// See also [PyArray_Arange](https://docs.scipy. org/doc/numpy /reference/c-api. array.html#c.PyArray_Arange).
1039
+ /// See also [PyArray_Arange](https://numpy. org/doc/stable /reference/c-api/ array.html#c.PyArray_Arange).
1043
1040
///
1044
1041
/// # Example
1045
1042
/// ```
0 commit comments