diff --git a/src/dimension/dimension_trait.rs b/src/dimension/dimension_trait.rs index 3544a7f3c..3a406a097 100644 --- a/src/dimension/dimension_trait.rs +++ b/src/dimension/dimension_trait.rs @@ -195,26 +195,32 @@ pub trait Dimension: #[doc(hidden)] /// Iteration -- Use self as size, and return next index after `index` /// or None if there are no more. - // FIXME: use &Self for index or even &mut? #[inline] - fn next_for(&self, index: Self) -> Option + fn next_for(&self, mut index: Self) -> Option + { + if self.next_for_mut(&mut index) { + Some(index) + } else { + None + } + } + + #[doc(hidden)] + /// Iteration -- Similar to `next_for`, but addresses the index as mutable reference. + #[inline] + fn next_for_mut(&self, index: &mut Self) -> bool { - let mut index = index; - let mut done = false; + let mut end_iteration = true; for (&dim, ix) in zip(self.slice(), index.slice_mut()).rev() { *ix += 1; if *ix == dim { *ix = 0; } else { - done = true; + end_iteration = false; break; } } - if done { - Some(index) - } else { - None - } + !end_iteration } #[doc(hidden)] diff --git a/src/iterators/mod.rs b/src/iterators/mod.rs index f7892a8c9..d57a0d815 100644 --- a/src/iterators/mod.rs +++ b/src/iterators/mod.rs @@ -74,10 +74,12 @@ impl Iterator for Baseiter { let index = match self.index { None => return None, - Some(ref ix) => ix.clone(), + Some(ref mut ix) => ix, }; - let offset = D::stride_offset(&index, &self.strides); - self.index = self.dim.next_for(index); + let offset = D::stride_offset(index, &self.strides); + if !self.dim.next_for_mut(index) { + self.index = None; + } unsafe { Some(self.ptr.offset(offset)) } } diff --git a/src/partial.rs b/src/partial.rs index dbaa0e105..e6ba64537 100644 --- a/src/partial.rs +++ b/src/partial.rs @@ -71,7 +71,7 @@ impl Partial // covers the whole output. if left.is_stub() { right - } else if left.ptr.wrapping_add(left.len) == right.ptr { + } else if ptr::eq(left.ptr.wrapping_add(left.len), right.ptr) { left.len += right.release_ownership(); left } else {