Skip to content

fix(grid): stop iterating a grid without columns - #843

Open
tachsin wants to merge 1 commit into
evenfurther:mainfrom
tachsin:fix/grid-zero-width
Open

tachsin wants to merge 1 commit into
evenfurther:mainfrom
tachsin:fix/grid-zero-width

Conversation

@tachsin

@tachsin tachsin commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Fixes #842.

Iterating a Grid without columns never returned: Grid::new(0, 5).edges() in any state, and iter() / into_iter() once such a grid is dense, for instance after fill() or after shrinking a full grid with resize(0, h).

All three walks step x along a row and move to the next row when x comes back to width. With a width of zero that never happens, so y never advances and the y == height exit is never reached. They now stop straight away when there are no columns, since such a grid has no vertices and no edges.

Test

a_grid_without_columns_iterates_to_nothing in tests/grid.rs covers edges(), iter() and into_iter(), both on Grid::new(0, 5) after fill() and on a full 3x3 grid shrunk to 0x3, and checks vertices_len() along the way. It hangs on current main and passes with this change.

I also checked the rest of Grid against a plain set of vertices through a few thousand random add_vertex, remove_vertex, add_borders, resize, fill and clear operations, in both diagonal modes, comparing vertices, edges and neighbours after each step. Everything else agreed, so this is limited to the zero-width case.

The edge iterator, and the dense walks of GridIterator and
GridIntoIterator, step x along a row and move to the next row when x
comes back to width. With a width of zero x never does, so y never
advances and the walk never ends.

A grid without columns has no vertices, so all three now yield nothing.

Fixes evenfurther#842
Comment thread src/grid.rs
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?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Grid iterators never return on a grid without columns

2 participants