Skip to content

Commit

Permalink
Fix Vec Iter::count() after next_back
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdavid committed Nov 24, 2024
1 parent 2129c23 commit 7f279a3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
6 changes: 1 addition & 5 deletions src/base_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,7 @@ where
}

fn count(self) -> usize {
let n = self.vec.len().saturating_sub(self.range.start);
if n > usize::MAX as u64 {
panic!("The number of items in the vec {n} does not fit into usize");
}
n as usize
self.range.count()
}

fn nth(&mut self, n: usize) -> Option<T> {
Expand Down
7 changes: 7 additions & 0 deletions src/vec/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ fn test_iter() {
assert_eq!(sv.iter().skip(3).count(), 0);
assert_eq!(sv.iter().skip(4).count(), 0);
assert_eq!(sv.iter().skip(usize::MAX).count(), 0);

{
assert_eq!(sv.len(), 3);
let mut iter = sv.iter();
iter.next_back();
assert_eq!(iter.by_ref().count(), 2);
}
}

// A struct with a bugg implementation of storable where the max_size can
Expand Down

0 comments on commit 7f279a3

Please sign in to comment.