Skip to content

Add checked BitBoard::to_maybe_square(); document to_square() on empty (#91) - #92

Open
Booyaka101 wants to merge 2 commits into
jordanbray:mainfrom
Booyaka101:fix/91-bitboard-to-maybe-square
Open

Booyaka101 wants to merge 2 commits into
jordanbray:mainfrom
Booyaka101:fix/91-bitboard-to-maybe-square

Conversation

@Booyaka101

Copy link
Copy Markdown

Addresses #91.

On current main, BitBoard::new(0).to_square() returns A1, not the invalid a9 from the report — Square::new already masks with & 63, so the out-of-range square is gone. What remains is that to_square() silently returns A1 for an empty BitBoard, which is indistinguishable from a BitBoard with only A1 set.

This adds BitBoard::to_maybe_square() -> Option<Square>, the checked counterpart of to_square() (mirroring the existing from_maybe_square), returning None when the BitBoard is empty. to_square()'s documentation now states its non-empty precondition and points to the checked method.

Additive and non-breaking. Doctests on both methods serve as tests; the full suite (36 unit + 142 doc) passes. to_maybe_square takes &self to match the existing to_square/to_size convention.

On current main, BitBoard::new(0).to_square() returns A1, not the invalid
"a9" from the report -- Square::new already masks with & 63. What remains is
that to_square() silently returns A1 for an empty BitBoard, which is
indistinguishable from a BitBoard with only A1 set.

Add BitBoard::to_maybe_square() -> Option<Square>, the checked counterpart of
to_square() (mirroring the existing from_maybe_square), returning None when
empty. to_square()'s documentation now states its non-empty precondition and
points to the checked method.

Additive and non-breaking. to_maybe_square uses &self to match the existing
to_square/to_size convention.

Addresses jordanbray#91.
Comment thread src/bitboard.rs Outdated
if self.0 == 0 {
None
} else {
Some(Square::new(self.0.trailing_zeros() as u8))

@karolzwolak karolzwolak Aug 3, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
Some(Square::new(self.0.trailing_zeros() as u8))
Some(self.to_square())

to avoid duplicating the code?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch, applied in dd0d66b. Both are #[inline] so it generates the same code. Suite still green: 36 unit + 142 doc.

Per review: the non-empty branch was open-coding to_square()'s body.  Both
are #[inline], so this is the same code generation.

Co-authored-by: Karol Zwolak <58865794+karolzwolak@users.noreply.github.com>

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.

2 participants