Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,9 @@ impl Iterator for GridIntoIterator {
fn next(&mut self) -> Option<Self::Item> {
if self.grid.dense {
loop {
if self.y == self.grid.height {
// With no columns, `x` would never come back to `width` and the walk would
// never move on to the next row.
if self.y == self.grid.height || self.grid.width == 0 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it a bit strange to redo the self.grid.width == 0 at every iteration of the loop instead of once at the beginning?

return None;
}
let r = (self.grid.has_vertex((self.x, self.y))).then_some((self.x, self.y));
Expand Down Expand Up @@ -646,7 +648,9 @@ impl Iterator for GridIterator<'_> {
fn next(&mut self) -> Option<Self::Item> {
if self.grid.dense {
loop {
if self.y == self.grid.height {
// With no columns, `x` would never come back to `width` and the walk would
// never move on to the next row.
if self.y == self.grid.height || self.grid.width == 0 {
return None;
}
let r = (self.grid.has_vertex((self.x, self.y))).then_some((self.x, self.y));
Expand Down Expand Up @@ -698,7 +702,9 @@ impl Iterator for EdgesIterator<'_> {

fn next(&mut self) -> Option<Self::Item> {
loop {
if self.y == self.grid.height {
// With no columns, `x` would never come back to `width` and the walk would never
// move on to the next row.
if self.y == self.grid.height || self.grid.width == 0 {
return None;
}
let x = self.x;
Expand Down
21 changes: 21 additions & 0 deletions tests/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,3 +666,24 @@ fn iter_full_grid_yields_all_vertices() {
via_into_iter.sort_unstable();
assert_eq!(via_into_iter, expected);
}

/// A grid without columns has no vertices. Walking it used to step `x` until it came back to
/// `width`, which with a width of zero never happens, so these loops never ended.
#[test]
fn a_grid_without_columns_iterates_to_nothing() {
let mut g = Grid::new(0, 5);
assert_eq!(g.edges().count(), 0);
g.fill();
assert_eq!(g.iter().count(), 0);
assert_eq!(g.edges().count(), 0);
assert_eq!(g.into_iter().count(), 0);

// The same state reached by shrinking a full grid.
let mut g = Grid::new(3, 3);
g.fill();
assert!(g.resize(0, 3));
assert_eq!(g.iter().count(), 0);
assert_eq!(g.edges().count(), 0);
assert_eq!(g.vertices_len(), 0);
assert_eq!(g.into_iter().count(), 0);
}
Loading