diff --git a/src/vector/mod.rs b/src/vector/mod.rs index b6728e8..d984c47 100644 --- a/src/vector/mod.rs +++ b/src/vector/mod.rs @@ -1881,8 +1881,21 @@ impl<'a, A: Clone> Iterator for Iter<'a, A> { } fn size_hint(&self) -> (usize, Option) { - let remaining = self.back_index - self.front_index; - (remaining, Some(remaining)) + let len = self.len(); + (len, Some(len)) + } + + fn count(self) -> usize { + self.len() + } + + fn nth(&mut self, n: usize) -> Option { + if n >= self.len() { + None + } else { + self.front_index += n; + self.next() + } } } @@ -1899,9 +1912,22 @@ impl<'a, A: Clone> DoubleEndedIterator for Iter<'a, A> { let focus: &'a mut Focus<'a, A> = unsafe { &mut *(&mut self.focus as *mut _) }; focus.get(self.back_index) } + + fn nth_back(&mut self, n: usize) -> Option { + if n >= self.len() { + None + } else { + self.back_index -= n; + self.next_back() + } + } } -impl<'a, A: Clone> ExactSizeIterator for Iter<'a, A> {} +impl<'a, A: Clone> ExactSizeIterator for Iter<'a, A> { + fn len(&self) -> usize { + self.back_index - self.front_index + } +} impl<'a, A: Clone> FusedIterator for Iter<'a, A> {} @@ -1963,8 +1989,21 @@ where } fn size_hint(&self) -> (usize, Option) { - let remaining = self.back_index - self.front_index; - (remaining, Some(remaining)) + let len = self.len(); + (len, Some(len)) + } + + fn count(self) -> usize { + self.len() + } + + fn nth(&mut self, n: usize) -> Option { + if n >= self.len() { + None + } else { + self.front_index += n; + self.next() + } } } @@ -1984,9 +2023,22 @@ where let focus: &'a mut FocusMut<'a, A> = unsafe { &mut *(&mut self.focus as *mut _) }; focus.get_mut(self.back_index) } + + fn nth_back(&mut self, n: usize) -> Option { + if n >= self.len() { + None + } else { + self.back_index -= n; + self.next_back() + } + } } -impl<'a, A: Clone> ExactSizeIterator for IterMut<'a, A> {} +impl<'a, A: Clone> ExactSizeIterator for IterMut<'a, A> { + fn len(&self) -> usize { + self.back_index - self.front_index + } +} impl<'a, A: Clone> FusedIterator for IterMut<'a, A> {} @@ -2089,6 +2141,24 @@ impl<'a, A: Clone> Iterator for Chunks<'a, A> { self.front_index = range.end; Some(value) } + + fn size_hint(&self) -> (usize, Option) { + let len = self.len(); + (len, Some(len)) + } + + fn count(self) -> usize { + self.len() + } + + fn nth(&mut self, n: usize) -> Option { + if n >= self.len() { + None + } else { + self.front_index += n; + self.next() + } + } } impl<'a, A: Clone> DoubleEndedIterator for Chunks<'a, A> { @@ -2106,6 +2176,21 @@ impl<'a, A: Clone> DoubleEndedIterator for Chunks<'a, A> { self.back_index = range.start; Some(value) } + + fn nth_back(&mut self, n: usize) -> Option { + if n >= self.len() { + None + } else { + self.back_index -= n; + self.next_back() + } + } +} + +impl<'a, A: Clone> ExactSizeIterator for Chunks<'a, A> { + fn len(&self) -> usize { + self.back_index - self.front_index + } } impl<'a, A: Clone> FusedIterator for Chunks<'a, A> {} @@ -2148,6 +2233,24 @@ impl<'a, A: Clone> Iterator for ChunksMut<'a, A> { self.front_index = range.end; Some(value) } + + fn size_hint(&self) -> (usize, Option) { + let len = self.len(); + (len, Some(len)) + } + + fn count(self) -> usize { + self.len() + } + + fn nth(&mut self, n: usize) -> Option { + if n >= self.len() { + None + } else { + self.front_index += n; + self.next() + } + } } impl<'a, A: Clone> DoubleEndedIterator for ChunksMut<'a, A> { @@ -2165,6 +2268,21 @@ impl<'a, A: Clone> DoubleEndedIterator for ChunksMut<'a, A> { self.back_index = range.start; Some(value) } + + fn nth_back(&mut self, n: usize) -> Option { + if n >= self.len() { + None + } else { + self.back_index -= n; + self.next_back() + } + } +} + +impl<'a, A: Clone> ExactSizeIterator for ChunksMut<'a, A> { + fn len(&self) -> usize { + self.back_index - self.front_index + } } impl<'a, A: Clone> FusedIterator for ChunksMut<'a, A> {} @@ -2635,6 +2753,31 @@ mod test { assert_eq!(&vec[index], item); } assert_eq!(vec.len(), seq.len()); + assert_eq!(vec.iter().count(), seq.iter().count()); + } + + #[test] + #[allow(clippy::iter_nth)] + fn iter_nth(ref vec in vec(i32::ANY, 0..1000)) { + let seq: Vector = Vector::from_iter(vec.iter().cloned()); + assert_eq!(vec.iter().nth(0), seq.iter().nth(0)); + assert_eq!(vec.iter().nth(1), seq.iter().nth(1)); + let last_index = seq.len().checked_sub(1).unwrap_or(0); + assert_eq!(vec.iter().nth(last_index), seq.iter().nth(last_index)); + let last_index_plus_one = seq.len(); + assert_eq!(vec.iter().nth(last_index_plus_one), seq.iter().nth(last_index_plus_one)); + } + + #[test] + #[allow(clippy::iter_nth)] + fn iter_nth_back(ref vec in vec(i32::ANY, 0..1000)) { + let seq: Vector = Vector::from_iter(vec.iter().cloned()); + assert_eq!(vec.iter().nth_back(0), seq.iter().nth_back(0)); + assert_eq!(vec.iter().nth_back(1), seq.iter().nth_back(1)); + let last_index = seq.len().checked_sub(1).unwrap_or(0); + assert_eq!(vec.iter().nth_back(last_index), seq.iter().nth_back(last_index)); + let last_index_plus_one = seq.len(); + assert_eq!(vec.iter().nth_back(last_index_plus_one), seq.iter().nth_back(last_index_plus_one)); } #[test]