Skip to content

Commit 0bdce23

Browse files
authored
Merge pull request #389 from jturner314/fix-step-by
Change SliceOrIndex::step_by step to be multiplicative and add debug assertion for step != 0
2 parents 0ece678 + f2dff3e commit 0bdce23

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/slice.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,24 @@ impl SliceOrIndex {
172172
}
173173
}
174174

175-
/// Returns a new `SliceOrIndex` with the given step size.
175+
/// Returns a new `SliceOrIndex` with the given step size (multiplied with
176+
/// the previous step size).
177+
///
178+
/// `step` must be nonzero.
179+
/// (This method checks with a debug assertion that `step` is not zero.)
176180
#[inline]
177181
pub fn step_by(self, step: isize) -> Self {
182+
debug_assert_ne!(step, 0, "SliceOrIndex::step_by: step must be nonzero");
178183
match self {
179-
SliceOrIndex::Slice { start, end, .. } => SliceOrIndex::Slice { start, end, step },
184+
SliceOrIndex::Slice {
185+
start,
186+
end,
187+
step: orig_step,
188+
} => SliceOrIndex::Slice {
189+
start,
190+
end,
191+
step: orig_step * step,
192+
},
180193
SliceOrIndex::Index(s) => SliceOrIndex::Index(s),
181194
}
182195
}

0 commit comments

Comments
 (0)