Skip to content

Commit 7e22b7d

Browse files
committed
attempt to do the minimum NonZero
1 parent 483200b commit 7e22b7d

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/slice.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::error::{ErrorKind, ShapeError};
99
use crate::{ArrayView, ArrayViewMut, Dimension, RawArrayViewMut};
1010
use std::fmt;
1111
use std::marker::PhantomData;
12+
use std::num::NonZeroIsize;
1213
use std::ops::{Deref, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};
1314

1415
/// A slice (range with step size).
@@ -48,6 +49,7 @@ impl Slice {
4849
/// (This method checks with a debug assertion that `step` is not zero.)
4950
pub fn new(start: isize, end: Option<isize>, step: isize) -> Slice {
5051
debug_assert_ne!(step, 0, "Slice::new: step must be nonzero");
52+
// let step = NonZeroIsize::new(step).unwrap();
5153
Slice { start, end, step }
5254
}
5355

@@ -129,8 +131,7 @@ impl SliceOrIndex {
129131
/// `step` must be nonzero.
130132
/// (This method checks with a debug assertion that `step` is not zero.)
131133
#[inline]
132-
pub fn step_by(self, step: isize) -> Self {
133-
debug_assert_ne!(step, 0, "SliceOrIndex::step_by: step must be nonzero");
134+
pub fn step_by(self, step: NonZeroIsize) -> Self {
134135
match self {
135136
SliceOrIndex::Slice {
136137
start,
@@ -139,7 +140,7 @@ impl SliceOrIndex {
139140
} => SliceOrIndex::Slice {
140141
start,
141142
end,
142-
step: orig_step * step,
143+
step: orig_step * step.get(),
143144
},
144145
SliceOrIndex::Index(s) => SliceOrIndex::Index(s),
145146
}
@@ -610,7 +611,7 @@ macro_rules! s(
610611
};
611612
// convert range/index and step into SliceOrIndex
612613
(@convert $r:expr, $s:expr) => {
613-
<$crate::SliceOrIndex as ::std::convert::From<_>>::from($r).step_by($s as isize)
614+
<$crate::SliceOrIndex as ::std::convert::From<_>>::from($r).step_by(std::num::NonZeroIsize::new($s).unwrap())
614615
};
615616
($($t:tt)*) => {
616617
// The extra `*&` is a workaround for this compiler bug:

tests/array.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ fn test_slice_dyninput_vec_dyn() {
257257
let info = &SliceInfo::<_, IxDyn>::new(vec![
258258
SliceOrIndex::from(1..),
259259
SliceOrIndex::from(1),
260-
SliceOrIndex::from(..).step_by(2),
260+
SliceOrIndex::from(..).step_by(2isize),
261261
])
262262
.unwrap();
263263
arr.slice(info.as_ref());

0 commit comments

Comments
 (0)