From 98c0878e74b338c0c05e2476c207549964b5675f Mon Sep 17 00:00:00 2001 From: bluss Date: Tue, 28 Nov 2017 20:26:24 +0100 Subject: [PATCH 1/2] API: Add debug assertions for step != 0 in Slice::new, step_by --- src/slice.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/slice.rs b/src/slice.rs index 32e14a8c7..e8a394fb5 100644 --- a/src/slice.rs +++ b/src/slice.rs @@ -35,7 +35,15 @@ pub struct Slice { } impl Slice { + /// Create a new `Slice` with the given extents. + /// + /// See also the `From` impls, converting from ranges; for example + /// `Slice::from(i..)` or `Slice::from(j..k)`. + /// + /// `step` must be nonzero. + /// (This method checks with a debug assertion that `step` is not zero.) pub fn new(start: isize, end: Option, step: isize) -> Slice { + debug_assert_ne!(step, 0, "Slice::new: step must be nonzero"); Slice { start, end, @@ -44,8 +52,12 @@ impl Slice { } /// Returns a new `Slice` with the given step size. + /// + /// `step` must be nonzero. + /// (This method checks with a debug assertion that `step` is not zero.) #[inline] - pub fn step_by(self, step: Ixs) -> Self { + pub fn step_by(self, step: isize) -> Self { + debug_assert_ne!(step, 0, "Slice::step_by: step must be nonzero"); Slice { step, ..self } } } From 9ac07d7b0db4a10cd4bc7a128dddbb23f0ff1565 Mon Sep 17 00:00:00 2001 From: bluss Date: Tue, 28 Nov 2017 20:29:18 +0100 Subject: [PATCH 2/2] API: Change Slice::step_by step to be multiplicative (cumulative) --- src/slice.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/slice.rs b/src/slice.rs index e8a394fb5..c3e8585a8 100644 --- a/src/slice.rs +++ b/src/slice.rs @@ -51,14 +51,15 @@ impl Slice { } } - /// Returns a new `Slice` with the given step size. + /// Create a new `Slice` with the given step size (multiplied with the + /// previous step size). /// /// `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, "Slice::step_by: step must be nonzero"); - Slice { step, ..self } + Slice { step: self.step * step, ..self } } }