@@ -26,7 +26,7 @@ use crate::cold;
2626use crate :: convert:: { ArrayExt , IntoPyArray , NpyIndex , ToNpyDims , ToPyArray } ;
2727use crate :: dtype:: { Element , PyArrayDescrMethods } ;
2828use crate :: error:: {
29- BorrowError , DimensionalityError , FromVecError , IgnoreError , NotContiguousError , TypeError ,
29+ AsSliceError , BorrowError , DimensionalityError , FromVecError , IgnoreError , TypeError ,
3030 DIMENSIONALITY_MISMATCH_ERR , MAX_DIMENSIONALITY_ERR ,
3131} ;
3232use crate :: npyffi:: { self , npy_intp, NPY_ORDER , PY_ARRAY_API } ;
@@ -739,7 +739,7 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> {
739739 /// or concurrently modified by Python or other native code.
740740 ///
741741 /// Please consider the safe alternative [`PyReadonlyArray::as_slice`].
742- unsafe fn as_slice ( & self ) -> Result < & [ T ] , NotContiguousError >
742+ unsafe fn as_slice ( & self ) -> Result < & [ T ] , AsSliceError >
743743 where
744744 T : Element ,
745745 D : Dimension ,
@@ -749,10 +749,12 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> {
749749 // We can still produce a slice over zero objects regardless of whether
750750 // the underlying pointer is aligned or not.
751751 Ok ( & [ ] )
752- } else if self . is_aligned ( ) && self . is_contiguous ( ) {
753- Ok ( slice:: from_raw_parts ( self . data ( ) , len) )
752+ } else if !self . is_aligned ( ) {
753+ Err ( AsSliceError :: NotAligned )
754+ } else if !self . is_contiguous ( ) {
755+ Err ( AsSliceError :: NotContiguous )
754756 } else {
755- Err ( NotContiguousError )
757+ Ok ( slice :: from_raw_parts ( self . data ( ) , len ) )
756758 }
757759 }
758760
@@ -766,7 +768,7 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> {
766768 ///
767769 /// Please consider the safe alternative [`PyReadwriteArray::as_slice_mut`].
768770 #[ allow( clippy:: mut_from_ref) ]
769- unsafe fn as_slice_mut ( & self ) -> Result < & mut [ T ] , NotContiguousError >
771+ unsafe fn as_slice_mut ( & self ) -> Result < & mut [ T ] , AsSliceError >
770772 where
771773 T : Element ,
772774 D : Dimension ,
@@ -776,10 +778,12 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> {
776778 // We can still produce a slice over zero objects regardless of whether
777779 // the underlying pointer is aligned or not.
778780 Ok ( & mut [ ] )
779- } else if self . is_aligned ( ) && self . is_contiguous ( ) {
780- Ok ( slice:: from_raw_parts_mut ( self . data ( ) , len) )
781+ } else if !self . is_aligned ( ) {
782+ Err ( AsSliceError :: NotAligned )
783+ } else if !self . is_contiguous ( ) {
784+ Err ( AsSliceError :: NotContiguous )
781785 } else {
782- Err ( NotContiguousError )
786+ Ok ( slice :: from_raw_parts_mut ( self . data ( ) , len ) )
783787 }
784788 }
785789
@@ -961,7 +965,7 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> {
961965 /// })
962966 /// # }
963967 /// ```
964- fn to_vec ( & self ) -> Result < Vec < T > , NotContiguousError >
968+ fn to_vec ( & self ) -> Result < Vec < T > , AsSliceError >
965969 where
966970 T : Element ,
967971 D : Dimension ;
@@ -1468,7 +1472,7 @@ impl<'py, T, D> PyArrayMethods<'py, T, D> for Bound<'py, PyArray<T, D>> {
14681472 unsafe { self . cast_unchecked ( ) }
14691473 }
14701474
1471- fn to_vec ( & self ) -> Result < Vec < T > , NotContiguousError >
1475+ fn to_vec ( & self ) -> Result < Vec < T > , AsSliceError >
14721476 where
14731477 T : Element ,
14741478 D : Dimension ,
0 commit comments