@@ -9,7 +9,7 @@ use std::mem;
9
9
use std:: os:: raw:: c_int;
10
10
use std:: ptr;
11
11
12
- use convert:: { NpyIndex , ToNpyDims } ;
12
+ use convert:: { IntoPyArray , NpyIndex , ToNpyDims , ToPyArray } ;
13
13
use error:: { ErrorKind , IntoPyResult } ;
14
14
use slice_box:: SliceBox ;
15
15
use types:: { NpyDataType , TypeNum } ;
@@ -265,6 +265,10 @@ impl<T, D> PyArray<T, D> {
265
265
let ptr = self . as_array_ptr ( ) ;
266
266
( * ptr) . data as * mut T
267
267
}
268
+
269
+ pub ( crate ) unsafe fn copy_ptr ( & self , other : * const T , len : usize ) {
270
+ ptr:: copy_nonoverlapping ( other, self . data ( ) , len)
271
+ }
268
272
}
269
273
270
274
impl < T : TypeNum , D : Dimension > PyArray < T , D > {
@@ -306,7 +310,7 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
306
310
unsafe { PyArray :: new_ ( py, dims, ptr:: null_mut ( ) , flags) }
307
311
}
308
312
309
- unsafe fn new_ < ' py , ID > (
313
+ pub ( crate ) unsafe fn new_ < ' py , ID > (
310
314
py : Python < ' py > ,
311
315
dims : ID ,
312
316
strides : * mut npy_intp ,
@@ -330,16 +334,17 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
330
334
Self :: from_owned_ptr ( py, ptr)
331
335
}
332
336
333
- pub ( crate ) unsafe fn new_with_data < ' py , ID > (
337
+ pub ( crate ) unsafe fn from_boxed_slice < ' py , ID > (
334
338
py : Python < ' py > ,
335
339
dims : ID ,
336
340
strides : * mut npy_intp ,
337
- slice : & SliceBox < T > ,
341
+ slice : Box < [ T ] > ,
338
342
) -> & ' py Self
339
343
where
340
344
ID : IntoDimension < Dim = D > ,
341
345
{
342
346
let dims = dims. into_dimension ( ) ;
347
+ let slice = SliceBox :: new ( slice) ;
343
348
let ptr = PY_ARRAY_API . PyArray_New (
344
349
PY_ARRAY_API . get_type_object ( npyffi:: ArrayType :: PyArray_Type ) ,
345
350
dims. ndim_cint ( ) ,
@@ -388,7 +393,7 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
388
393
}
389
394
}
390
395
391
- /// Construct PyArray from ndarray::Array .
396
+ /// Construct PyArray from ` ndarray::ArrayBase` .
392
397
///
393
398
/// This method allocates memory in Python's heap via numpy api, and then copies all elements
394
399
/// of the array there.
@@ -398,25 +403,32 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
398
403
/// # extern crate pyo3; extern crate numpy; #[macro_use] extern crate ndarray; fn main() {
399
404
/// use numpy::PyArray;
400
405
/// let gil = pyo3::Python::acquire_gil();
401
- /// let pyarray = PyArray::from_ndarray (gil.python(), &array![[1, 2], [3, 4]]);
406
+ /// let pyarray = PyArray::from_array (gil.python(), &array![[1, 2], [3, 4]]);
402
407
/// assert_eq!(pyarray.as_array().unwrap(), array![[1, 2], [3, 4]]);
403
408
/// # }
404
409
/// ```
405
- pub fn from_ndarray < ' py , S > ( py : Python < ' py > , arr : & ArrayBase < S , D > ) -> & ' py Self
410
+ pub fn from_array < ' py , S > ( py : Python < ' py > , arr : & ArrayBase < S , D > ) -> & ' py Self
406
411
where
407
412
S : Data < Elem = T > ,
408
413
{
409
- let len = arr. len ( ) ;
410
- let mut strides: Vec < _ > = arr
411
- . strides ( )
412
- . into_iter ( )
413
- . map ( |n| n * mem:: size_of :: < T > ( ) as npy_intp )
414
- . collect ( ) ;
415
- unsafe {
416
- let array = PyArray :: new_ ( py, arr. raw_dim ( ) , strides. as_mut_ptr ( ) as * mut npy_intp , 0 ) ;
417
- ptr:: copy_nonoverlapping ( arr. as_ptr ( ) , array. data ( ) , len) ;
418
- array
419
- }
414
+ ToPyArray :: to_pyarray ( arr, py)
415
+ }
416
+
417
+ /// Construct PyArray from `ndarray::Array`.
418
+ ///
419
+ /// This method uses internal `Vec` of `ndarray::Array` as numpy array.
420
+ ///
421
+ /// # Example
422
+ /// ```
423
+ /// # extern crate pyo3; extern crate numpy; #[macro_use] extern crate ndarray; fn main() {
424
+ /// use numpy::PyArray;
425
+ /// let gil = pyo3::Python::acquire_gil();
426
+ /// let pyarray = PyArray::from_owned_array(gil.python(), array![[1, 2], [3, 4]]);
427
+ /// assert_eq!(pyarray.as_array().unwrap(), array![[1, 2], [3, 4]]);
428
+ /// # }
429
+ /// ```
430
+ pub fn from_owned_array < ' py > ( py : Python < ' py > , arr : Array < T , D > ) -> & ' py Self {
431
+ IntoPyArray :: into_pyarray ( arr, py)
420
432
}
421
433
422
434
/// Get the immutable view of the internal data of `PyArray`, as `ndarray::ArrayView`.
@@ -565,8 +577,7 @@ impl<T: TypeNum> PyArray<T, Ix1> {
565
577
pub fn from_slice < ' py > ( py : Python < ' py > , slice : & [ T ] ) -> & ' py Self {
566
578
let array = PyArray :: new ( py, [ slice. len ( ) ] , false ) ;
567
579
unsafe {
568
- let src = slice. as_ptr ( ) as * mut T ;
569
- ptr:: copy_nonoverlapping ( src, array. data ( ) , slice. len ( ) ) ;
580
+ array. copy_ptr ( slice. as_ptr ( ) , slice. len ( ) ) ;
570
581
}
571
582
array
572
583
}
0 commit comments