@@ -4,19 +4,16 @@ use ndarray::*;
44use num_traits:: AsPrimitive ;
55use pyo3:: { ffi, prelude:: * , type_object, types:: PyAny } ;
66use 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} ;
129
1310use crate :: convert:: { IntoPyArray , NpyIndex , ToNpyDims , ToPyArray } ;
1411use crate :: error:: { ErrorKind , IntoPyResult } ;
1512use crate :: slice_box:: SliceBox ;
1613use crate :: types:: { NpyDataType , TypeNum } ;
1714
1815/// 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).
2017///
2118/// # Memory location
2219///
@@ -77,7 +74,7 @@ use crate::types::{NpyDataType, TypeNum};
7774/// );
7875/// # }
7976/// ```
80- pub struct PyArray < T , D > ( PyObject , PhantomData < T > , PhantomData < D > ) ;
77+ pub struct PyArray < T , D > ( PyAny , PhantomData < T > , PhantomData < D > ) ;
8178
8279/// one-dimensional array
8380pub type PyArray1 < T > = PyArray < T , Ix1 > ;
@@ -113,7 +110,7 @@ pyobject_native_type_convert!(
113110
114111pyobject_native_type_named ! ( PyArray <T , D >, T , D ) ;
115112
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 {
117114 fn from ( ob : & ' a PyArray < T , D > ) -> Self {
118115 unsafe { & * ( ob as * const PyArray < T , D > as * const PyAny ) }
119116 }
@@ -139,8 +136,8 @@ impl<'a, T: TypeNum, D: Dimension> FromPyObject<'a> for &'a PyArray<T, D> {
139136}
140137
141138impl < 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 ( ) ) }
144141 }
145142}
146143
@@ -226,7 +223,7 @@ impl<T, D> PyArray<T, D> {
226223
227224 /// Returns the number of dimensions in the array.
228225 ///
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)
230227 ///
231228 /// # Example
232229 /// ```
@@ -237,15 +234,15 @@ impl<T, D> PyArray<T, D> {
237234 /// assert_eq!(arr.ndim(), 3);
238235 /// # }
239236 /// ```
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
241238 pub fn ndim ( & self ) -> usize {
242239 let ptr = self . as_array_ptr ( ) ;
243240 unsafe { ( * ptr) . nd as usize }
244241 }
245242
246243 /// Returns a slice which contains how many bytes you need to jump to the next row.
247244 ///
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)
249246 /// # Example
250247 /// ```
251248 /// # fn main() {
@@ -255,19 +252,19 @@ impl<T, D> PyArray<T, D> {
255252 /// assert_eq!(arr.strides(), &[240, 48, 8]);
256253 /// # }
257254 /// ```
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
259256 pub fn strides ( & self ) -> & [ isize ] {
260257 let n = self . ndim ( ) ;
261258 let ptr = self . as_array_ptr ( ) ;
262259 unsafe {
263260 let p = ( * ptr) . strides ;
264- :: std :: slice:: from_raw_parts ( p, n)
261+ slice:: from_raw_parts ( p, n)
265262 }
266263 }
267264
268265 /// Returns a slice which contains dimmensions of the array.
269266 ///
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)
271268 /// # Example
272269 /// ```
273270 /// # fn main() {
@@ -277,13 +274,13 @@ impl<T, D> PyArray<T, D> {
277274 /// assert_eq!(arr.shape(), &[4, 5, 6]);
278275 /// # }
279276 /// ```
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
281278 pub fn shape ( & self ) -> & [ usize ] {
282279 let n = self . ndim ( ) ;
283280 let ptr = self . as_array_ptr ( ) ;
284281 unsafe {
285282 let p = ( * ptr) . dimensions as * mut usize ;
286- :: std :: slice:: from_raw_parts ( p, n)
283+ slice:: from_raw_parts ( p, n)
287284 }
288285 }
289286
@@ -314,7 +311,7 @@ impl<T, D> PyArray<T, D> {
314311 let ptr = self . as_array_ptr ( ) ;
315312 unsafe {
316313 let p = ( * ptr) . strides ;
317- :: std :: slice:: from_raw_parts ( p as * const _ , n)
314+ slice:: from_raw_parts ( p as * const _ , n)
318315 }
319316 }
320317}
@@ -371,11 +368,11 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
371368 dims. ndim_cint ( ) ,
372369 dims. as_dims_ptr ( ) ,
373370 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
379376 ) ;
380377 Self :: from_owned_ptr ( py, ptr)
381378 }
@@ -404,7 +401,7 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
404401 data_ptr as _ , // data
405402 mem:: size_of :: < T > ( ) as i32 , // itemsize
406403 0 , // flag
407- :: std :: ptr:: null_mut ( ) , //obj
404+ ptr:: null_mut ( ) , //obj
408405 ) ;
409406 PY_ARRAY_API . PyArray_SetBaseObject ( ptr as * mut npyffi:: PyArrayObject , cell as _ ) ;
410407 Self :: from_owned_ptr ( py, ptr)
@@ -415,7 +412,7 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
415412 /// If `is_fortran` is true, then
416413 /// a fortran order array is created, otherwise a C-order array is created.
417414 ///
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)
419416 ///
420417 /// # Example
421418 /// ```
@@ -469,7 +466,7 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
469466 if !self . is_contiguous ( ) {
470467 Err ( ErrorKind :: NotContiguous )
471468 } else {
472- Ok ( unsafe { :: std :: slice:: from_raw_parts ( self . data ( ) , self . len ( ) ) } )
469+ Ok ( unsafe { slice:: from_raw_parts ( self . data ( ) , self . len ( ) ) } )
473470 }
474471 }
475472
@@ -479,7 +476,7 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
479476 if !self . is_contiguous ( ) {
480477 Err ( ErrorKind :: NotContiguous )
481478 } 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 ( ) ) } )
483480 }
484481 }
485482
@@ -1037,9 +1034,9 @@ impl<T: TypeNum, D> PyArray<T, D> {
10371034
10381035impl < T : TypeNum + AsPrimitive < f64 > > PyArray < T , Ix1 > {
10391036 /// 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).
10411038 ///
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).
10431040 ///
10441041 /// # Example
10451042 /// ```
0 commit comments