@@ -7,7 +7,7 @@ use std::marker::PhantomData;
7
7
use std:: os:: raw:: c_void;
8
8
use std:: ptr:: null_mut;
9
9
10
- use super :: error:: ArrayCastError ;
10
+ use super :: error:: ErrorKind ;
11
11
use super :: * ;
12
12
13
13
/// Interface for [NumPy ndarray](https://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html).
@@ -239,10 +239,13 @@ impl<T: TypeNum> PyArray<T> {
239
239
/// assert!(PyArray::from_vec2(gil.python(), &vec![vec![1], vec![2, 3]]).is_err());
240
240
/// # }
241
241
/// ```
242
- pub fn from_vec2 < ' py > ( py : Python < ' py > , v : & Vec < Vec < T > > ) -> Result < & ' py Self , ArrayCastError > {
242
+ pub fn from_vec2 < ' py > ( py : Python < ' py > , v : & Vec < Vec < T > > ) -> Result < & ' py Self , ErrorKind > {
243
243
let last_len = v. last ( ) . map_or ( 0 , |v| v. len ( ) ) ;
244
244
if v. iter ( ) . any ( |v| v. len ( ) != last_len) {
245
- return Err ( ArrayCastError :: FromVec ) ;
245
+ return Err ( ErrorKind :: FromVec {
246
+ dim1 : v. len ( ) ,
247
+ dim2 : last_len,
248
+ } ) ;
246
249
}
247
250
let dims = [ v. len ( ) , last_len] ;
248
251
let flattend: Vec < _ > = v. iter ( ) . cloned ( ) . flatten ( ) . collect ( ) ;
@@ -274,14 +277,20 @@ impl<T: TypeNum> PyArray<T> {
274
277
pub fn from_vec3 < ' py > (
275
278
py : Python < ' py > ,
276
279
v : & Vec < Vec < Vec < T > > > ,
277
- ) -> Result < & ' py PyArray < T > , ArrayCastError > {
280
+ ) -> Result < & ' py PyArray < T > , ErrorKind > {
278
281
let dim2 = v. last ( ) . map_or ( 0 , |v| v. len ( ) ) ;
279
282
if v. iter ( ) . any ( |v| v. len ( ) != dim2) {
280
- return Err ( ArrayCastError :: FromVec ) ;
283
+ return Err ( ErrorKind :: FromVec {
284
+ dim1 : v. len ( ) ,
285
+ dim2,
286
+ } ) ;
281
287
}
282
288
let dim3 = v. last ( ) . map_or ( 0 , |v| v. last ( ) . map_or ( 0 , |v| v. len ( ) ) ) ;
283
289
if v. iter ( ) . any ( |v| v. iter ( ) . any ( |v| v. len ( ) != dim3) ) {
284
- return Err ( ArrayCastError :: FromVec ) ;
290
+ return Err ( ErrorKind :: FromVec {
291
+ dim1 : v. len ( ) ,
292
+ dim2 : dim3,
293
+ } ) ;
285
294
}
286
295
let dims = [ v. len ( ) , dim2, dim3] ;
287
296
let flattend: Vec < _ > = v. iter ( ) . flat_map ( |v| v. iter ( ) . cloned ( ) . flatten ( ) ) . collect ( ) ;
@@ -338,23 +347,23 @@ impl<T: TypeNum> PyArray<T> {
338
347
NpyDataType :: from_i32 ( self . typenum ( ) )
339
348
}
340
349
341
- fn type_check ( & self ) -> Result < ( ) , ArrayCastError > {
350
+ fn type_check ( & self ) -> Result < ( ) , ErrorKind > {
342
351
let truth = self . typenum ( ) ;
343
352
if T :: is_same_type ( truth) {
344
353
Ok ( ( ) )
345
354
} else {
346
- Err ( ArrayCastError :: to_rust ( truth, T :: npy_data_type ( ) ) )
355
+ Err ( ErrorKind :: to_rust ( truth, T :: npy_data_type ( ) ) )
347
356
}
348
357
}
349
358
350
359
/// Get data as a ndarray::ArrayView
351
- pub fn as_array ( & self ) -> Result < ArrayViewD < T > , ArrayCastError > {
360
+ pub fn as_array ( & self ) -> Result < ArrayViewD < T > , ErrorKind > {
352
361
self . type_check ( ) ?;
353
362
unsafe { Ok ( ArrayView :: from_shape_ptr ( self . ndarray_shape ( ) , self . data ( ) ) ) }
354
363
}
355
364
356
365
/// Get data as a ndarray::ArrayViewMut
357
- pub fn as_array_mut ( & self ) -> Result < ArrayViewMutD < T > , ArrayCastError > {
366
+ pub fn as_array_mut ( & self ) -> Result < ArrayViewMutD < T > , ErrorKind > {
358
367
self . type_check ( ) ?;
359
368
unsafe {
360
369
Ok ( ArrayViewMut :: from_shape_ptr (
@@ -365,13 +374,13 @@ impl<T: TypeNum> PyArray<T> {
365
374
}
366
375
367
376
/// Get data as a Rust immutable slice
368
- pub fn as_slice ( & self ) -> Result < & [ T ] , ArrayCastError > {
377
+ pub fn as_slice ( & self ) -> Result < & [ T ] , ErrorKind > {
369
378
self . type_check ( ) ?;
370
379
unsafe { Ok ( :: std:: slice:: from_raw_parts ( self . data ( ) , self . len ( ) ) ) }
371
380
}
372
381
373
382
/// Get data as a Rust mutable slice
374
- pub fn as_slice_mut ( & self ) -> Result < & mut [ T ] , ArrayCastError > {
383
+ pub fn as_slice_mut ( & self ) -> Result < & mut [ T ] , ErrorKind > {
375
384
self . type_check ( ) ?;
376
385
unsafe { Ok ( :: std:: slice:: from_raw_parts_mut ( self . data ( ) , self . len ( ) ) ) }
377
386
}
@@ -476,12 +485,12 @@ impl<T: TypeNum> PyArray<T> {
476
485
/// assert!(pyarray_f.copy_to(pyarray_i).is_ok());
477
486
/// assert_eq!(pyarray_i.as_slice().unwrap(), &[2, 3, 4]);
478
487
/// # }
479
- pub fn copy_to < U : TypeNum > ( & self , other : & PyArray < U > ) -> Result < ( ) , ArrayCastError > {
488
+ pub fn copy_to < U : TypeNum > ( & self , other : & PyArray < U > ) -> Result < ( ) , ErrorKind > {
480
489
let self_ptr = self . as_array_ptr ( ) ;
481
490
let other_ptr = other. as_array_ptr ( ) ;
482
491
let result = unsafe { PY_ARRAY_API . PyArray_CopyInto ( other_ptr, self_ptr) } ;
483
492
if result == -1 {
484
- Err ( ArrayCastError :: dtype_cast ( self , U :: npy_data_type ( ) ) )
493
+ Err ( ErrorKind :: dtype_cast ( self , U :: npy_data_type ( ) ) )
485
494
} else {
486
495
Ok ( ( ) )
487
496
}
@@ -498,12 +507,12 @@ impl<T: TypeNum> PyArray<T> {
498
507
/// assert!(pyarray_f.move_to(pyarray_i).is_ok());
499
508
/// assert_eq!(pyarray_i.as_slice().unwrap(), &[2, 3, 4]);
500
509
/// # }
501
- pub fn move_to < U : TypeNum > ( & self , other : & PyArray < U > ) -> Result < ( ) , ArrayCastError > {
510
+ pub fn move_to < U : TypeNum > ( & self , other : & PyArray < U > ) -> Result < ( ) , ErrorKind > {
502
511
let self_ptr = self . as_array_ptr ( ) ;
503
512
let other_ptr = other. as_array_ptr ( ) ;
504
513
let result = unsafe { PY_ARRAY_API . PyArray_MoveInto ( other_ptr, self_ptr) } ;
505
514
if result == -1 {
506
- Err ( ArrayCastError :: dtype_cast ( self , U :: npy_data_type ( ) ) )
515
+ Err ( ErrorKind :: dtype_cast ( self , U :: npy_data_type ( ) ) )
507
516
} else {
508
517
Ok ( ( ) )
509
518
}
@@ -522,7 +531,7 @@ impl<T: TypeNum> PyArray<T> {
522
531
pub fn cast < ' py , U : TypeNum > (
523
532
& ' py self ,
524
533
is_fortran : bool ,
525
- ) -> Result < & ' py PyArray < U > , ArrayCastError > {
534
+ ) -> Result < & ' py PyArray < U > , ErrorKind > {
526
535
let ptr = unsafe {
527
536
let descr = PY_ARRAY_API . PyArray_DescrFromType ( U :: typenum_default ( ) ) ;
528
537
PY_ARRAY_API . PyArray_CastToType (
@@ -532,7 +541,7 @@ impl<T: TypeNum> PyArray<T> {
532
541
)
533
542
} ;
534
543
if ptr. is_null ( ) {
535
- Err ( ArrayCastError :: dtype_cast ( self , U :: npy_data_type ( ) ) )
544
+ Err ( ErrorKind :: dtype_cast ( self , U :: npy_data_type ( ) ) )
536
545
} else {
537
546
Ok ( unsafe { PyArray :: < U > :: from_owned_ptr ( self . py ( ) , ptr) } )
538
547
}
@@ -557,7 +566,7 @@ impl<T: TypeNum> PyArray<T> {
557
566
/// # }
558
567
/// ```
559
568
#[ inline( always) ]
560
- pub fn reshape < ' py , D : ToNpyDims > ( & ' py self , dims : D ) -> Result < & Self , ArrayCastError > {
569
+ pub fn reshape < ' py , D : ToNpyDims > ( & ' py self , dims : D ) -> Result < & Self , ErrorKind > {
561
570
self . reshape_with_order ( dims, NPY_ORDER :: NPY_ANYORDER )
562
571
}
563
572
@@ -566,7 +575,7 @@ impl<T: TypeNum> PyArray<T> {
566
575
& ' py self ,
567
576
dims : D ,
568
577
order : NPY_ORDER ,
569
- ) -> Result < & Self , ArrayCastError > {
578
+ ) -> Result < & Self , ErrorKind > {
570
579
let mut np_dims = dims. to_npy_dims ( ) ;
571
580
let ptr = unsafe {
572
581
PY_ARRAY_API . PyArray_Newshape (
@@ -576,7 +585,7 @@ impl<T: TypeNum> PyArray<T> {
576
585
)
577
586
} ;
578
587
if ptr. is_null ( ) {
579
- Err ( ArrayCastError :: dims_cast ( self , dims) )
588
+ Err ( ErrorKind :: dims_cast ( self , dims) )
580
589
} else {
581
590
Ok ( unsafe { PyArray :: < T > :: from_owned_ptr ( self . py ( ) , ptr) } )
582
591
}
0 commit comments