jordanbray / chess

A rust library to manage chess move generation
https://jordanbray.github.io/chess/
MIT License
231 stars 54 forks source link

Fix CastleRights unsound interface #61

Open terrorfisch opened 3 years ago

terrorfisch commented 3 years ago

See #53.

This PR adds an internal unsafe function. It might not be required because the compiler maybe can prove that the bound is always met. I did not check that.

analog-hors commented 3 years ago

The compiler should probably be able to prove that the index is within range for almost all uses of new_unchecked. The only usage that probably can't be checked is the lookup of CASTLES_PER_SQUARE. I think that can actually just be a table of CastleRights instead, forgoing the transmute. I think this is another reason for Square to be an enum, since this allows the compiler to trivially prove that Square lookups are always sound and elide the bounds checks. (I haven't tested any of these assumptions yet, but the restricted range of enums usually allows such optimizations.)