Skip to content

Commit

Permalink
attempt to do the minimum NonZero
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty committed Jul 27, 2019
1 parent 483200b commit 7e22b7d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::error::{ErrorKind, ShapeError};
use crate::{ArrayView, ArrayViewMut, Dimension, RawArrayViewMut};
use std::fmt;
use std::marker::PhantomData;
use std::num::NonZeroIsize;
use std::ops::{Deref, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};

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

Expand Down Expand Up @@ -129,8 +131,7 @@ impl SliceOrIndex {
/// `step` must be nonzero.
/// (This method checks with a debug assertion that `step` is not zero.)
#[inline]
pub fn step_by(self, step: isize) -> Self {
debug_assert_ne!(step, 0, "SliceOrIndex::step_by: step must be nonzero");
pub fn step_by(self, step: NonZeroIsize) -> Self {
match self {
SliceOrIndex::Slice {
start,
Expand All @@ -139,7 +140,7 @@ impl SliceOrIndex {
} => SliceOrIndex::Slice {
start,
end,
step: orig_step * step,
step: orig_step * step.get(),
},
SliceOrIndex::Index(s) => SliceOrIndex::Index(s),
}
Expand Down Expand Up @@ -610,7 +611,7 @@ macro_rules! s(
};
// convert range/index and step into SliceOrIndex
(@convert $r:expr, $s:expr) => {
<$crate::SliceOrIndex as ::std::convert::From<_>>::from($r).step_by($s as isize)
<$crate::SliceOrIndex as ::std::convert::From<_>>::from($r).step_by(std::num::NonZeroIsize::new($s).unwrap())
};
($($t:tt)*) => {
// The extra `*&` is a workaround for this compiler bug:
Expand Down
2 changes: 1 addition & 1 deletion tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ fn test_slice_dyninput_vec_dyn() {
let info = &SliceInfo::<_, IxDyn>::new(vec![
SliceOrIndex::from(1..),
SliceOrIndex::from(1),
SliceOrIndex::from(..).step_by(2),
SliceOrIndex::from(..).step_by(2isize),
])
.unwrap();
arr.slice(info.as_ref());
Expand Down

0 comments on commit 7e22b7d

Please sign in to comment.