@@ -61,33 +61,42 @@ pub fn dim_stride_overlap<D: Dimension>(dim: &D, strides: &D) -> bool {
61
61
pub fn can_index_slice < A , D : Dimension > ( data : & [ A ] , dim : & D , strides : & D )
62
62
-> Result < ( ) , ShapeError >
63
63
{
64
- if strides. slice ( ) . iter ( ) . cloned ( ) . all ( stride_is_positive) {
65
- if dim. size_checked ( ) . is_none ( ) {
64
+ // check lengths of axes.
65
+ let len = match dim. size_checked ( ) {
66
+ Some ( l) => l,
67
+ None => return Err ( from_kind ( ErrorKind :: OutOfBounds ) ) ,
68
+ } ;
69
+ // check if strides are strictly positive (zero ok for len 0)
70
+ for & s in strides. slice ( ) {
71
+ let s = s as Ixs ;
72
+ if s < 1 && ( len != 0 || s < 0 ) {
73
+ return Err ( from_kind ( ErrorKind :: Unsupported ) ) ;
74
+ }
75
+ }
76
+ if len == 0 {
77
+ return Ok ( ( ) ) ;
78
+ }
79
+ // check that the maximum index is in bounds
80
+ let mut last_index = dim. clone ( ) ;
81
+ for mut index in last_index. slice_mut ( ) . iter_mut ( ) {
82
+ * index -= 1 ;
83
+ }
84
+ if let Some ( offset) = stride_offset_checked_arithmetic ( dim,
85
+ strides,
86
+ & last_index)
87
+ {
88
+ // offset is guaranteed to be positive so no issue converting
89
+ // to usize here
90
+ if ( offset as usize ) >= data. len ( ) {
66
91
return Err ( from_kind ( ErrorKind :: OutOfBounds ) ) ;
67
92
}
68
- let mut last_index = dim. clone ( ) ;
69
- for mut index in last_index. slice_mut ( ) . iter_mut ( ) {
70
- * index -= 1 ;
71
- }
72
- if let Some ( offset) = stride_offset_checked_arithmetic ( dim,
73
- strides,
74
- & last_index)
75
- {
76
- // offset is guaranteed to be positive so no issue converting
77
- // to usize here
78
- if ( offset as usize ) >= data. len ( ) {
79
- return Err ( from_kind ( ErrorKind :: OutOfBounds ) ) ;
80
- }
81
- if dim_stride_overlap ( dim, strides) {
82
- return Err ( from_kind ( ErrorKind :: Unsupported ) ) ;
83
- }
84
- } else {
85
- return Err ( from_kind ( ErrorKind :: OutOfBounds ) ) ;
93
+ if dim_stride_overlap ( dim, strides) {
94
+ return Err ( from_kind ( ErrorKind :: Unsupported ) ) ;
86
95
}
87
- Ok ( ( ) )
88
96
} else {
89
- return Err ( from_kind ( ErrorKind :: Unsupported ) ) ;
97
+ return Err ( from_kind ( ErrorKind :: OutOfBounds ) ) ;
90
98
}
99
+ Ok ( ( ) )
91
100
}
92
101
93
102
/// Return stride offset for this dimension and index.
0 commit comments