@@ -415,15 +415,14 @@ impl<T: TypeNum> PyArray<T> {
415
415
S : Data < Elem = T > ,
416
416
D : Dimension ,
417
417
{
418
- let dims: Vec < _ > = arr. shape ( ) . iter ( ) . cloned ( ) . collect ( ) ;
419
- let len = dims. iter ( ) . fold ( 1 , |prod, d| prod * d) ;
418
+ let len = arr. len ( ) ;
420
419
let mut strides: Vec < _ > = arr
421
420
. strides ( )
422
421
. into_iter ( )
423
422
. map ( |n| n * mem:: size_of :: < T > ( ) as npy_intp )
424
423
. collect ( ) ;
425
424
unsafe {
426
- let array = PyArray :: new_ ( py, & * dims , strides. as_mut_ptr ( ) as * mut npy_intp , 0 ) ;
425
+ let array = PyArray :: new_ ( py, arr . shape ( ) , strides. as_mut_ptr ( ) as * mut npy_intp , 0 ) ;
427
426
ptr:: copy_nonoverlapping ( arr. as_ptr ( ) , array. data ( ) , len) ;
428
427
array
429
428
}
@@ -507,21 +506,22 @@ impl<T: TypeNum> PyArray<T> {
507
506
/// assert_eq!(pyarray.shape(), &[4, 5, 6]);
508
507
/// # }
509
508
/// ```
510
- pub fn new < ' py , D : ToNpyDims > ( py : Python < ' py > , dims : D , is_fortran : bool ) -> & ' py Self {
509
+ pub fn new < ' py , D : IntoDimension > ( py : Python < ' py > , dims : D , is_fortran : bool ) -> & ' py Self {
511
510
let flags = if is_fortran { 1 } else { 0 } ;
512
511
unsafe { PyArray :: new_ ( py, dims, ptr:: null_mut ( ) , flags) }
513
512
}
514
513
515
- unsafe fn new_ < ' py , D : ToNpyDims > (
514
+ unsafe fn new_ < ' py , D : IntoDimension > (
516
515
py : Python < ' py > ,
517
516
dims : D ,
518
517
strides : * mut npy_intp ,
519
518
flag : c_int ,
520
519
) -> & ' py Self {
520
+ let dims = dims. into_dimension ( ) ;
521
521
let ptr = PY_ARRAY_API . PyArray_New (
522
522
PY_ARRAY_API . get_type_object ( npyffi:: ArrayType :: PyArray_Type ) ,
523
- dims. dims_len ( ) ,
524
- dims. dims_ptr ( ) ,
523
+ dims. ndim_cint ( ) ,
524
+ dims. as_dims_ptr ( ) ,
525
525
T :: typenum_default ( ) ,
526
526
strides, // strides
527
527
ptr:: null_mut ( ) , // data
@@ -548,12 +548,13 @@ impl<T: TypeNum> PyArray<T> {
548
548
/// assert_eq!(pyarray.as_array().unwrap(), array![[0, 0], [0, 0]].into_dyn());
549
549
/// # }
550
550
/// ```
551
- pub fn zeros < ' py , D : ToNpyDims > ( py : Python < ' py > , dims : D , is_fortran : bool ) -> & ' py Self {
551
+ pub fn zeros < ' py , D : IntoDimension > ( py : Python < ' py > , dims : D , is_fortran : bool ) -> & ' py Self {
552
+ let dims = dims. into_dimension ( ) ;
552
553
unsafe {
553
554
let descr = PY_ARRAY_API . PyArray_DescrFromType ( T :: typenum_default ( ) ) ;
554
555
let ptr = PY_ARRAY_API . PyArray_Zeros (
555
- dims. dims_len ( ) ,
556
- dims. dims_ptr ( ) ,
556
+ dims. ndim_cint ( ) ,
557
+ dims. as_dims_ptr ( ) ,
557
558
descr,
558
559
if is_fortran { -1 } else { 0 } ,
559
560
) ;
@@ -653,16 +654,17 @@ impl<T: TypeNum> PyArray<T> {
653
654
/// # }
654
655
/// ```
655
656
#[ inline( always) ]
656
- pub fn reshape < ' py , D : ToNpyDims > ( & ' py self , dims : D ) -> Result < & Self , ErrorKind > {
657
+ pub fn reshape < ' py , D : IntoDimension > ( & ' py self , dims : D ) -> Result < & Self , ErrorKind > {
657
658
self . reshape_with_order ( dims, NPY_ORDER :: NPY_ANYORDER )
658
659
}
659
660
660
661
/// Same as [reshape](method.reshape.html), but you can change the order of returned matrix.
661
- pub fn reshape_with_order < ' py , D : ToNpyDims > (
662
+ pub fn reshape_with_order < ' py , D : IntoDimension > (
662
663
& ' py self ,
663
664
dims : D ,
664
665
order : NPY_ORDER ,
665
666
) -> Result < & Self , ErrorKind > {
667
+ let dims = dims. into_dimension ( ) ;
666
668
let mut np_dims = dims. to_npy_dims ( ) ;
667
669
let ptr = unsafe {
668
670
PY_ARRAY_API . PyArray_Newshape (
@@ -679,12 +681,13 @@ impl<T: TypeNum> PyArray<T> {
679
681
}
680
682
681
683
// TODO: expose?
682
- fn resize_ < ' py , D : ToNpyDims > (
684
+ fn resize_ < ' py , D : IntoDimension > (
683
685
& ' py self ,
684
686
dims : D ,
685
687
check_ref : c_int ,
686
688
order : NPY_ORDER ,
687
689
) -> Result < ( ) , ErrorKind > {
690
+ let dims = dims. into_dimension ( ) ;
688
691
let mut np_dims = dims. to_npy_dims ( ) ;
689
692
let res = unsafe {
690
693
PY_ARRAY_API . PyArray_Resize (
0 commit comments