[board, position] Return replaced piece when placing a piece with PlacePieceStrategy::Replace

When place_piece() is called with PlacePieceStrategy::Replace, return the replaced
piece in the Ok() variant of the Result as an Option<Piece>.

Plumb this new return type through Board and Position.
This commit is contained in:
Eryn Wells 2025-06-02 15:54:00 -07:00
parent 09fbe1be22
commit cae93cb090
3 changed files with 7 additions and 5 deletions

View file

@ -80,7 +80,7 @@ impl Board {
piece: Piece,
square: Square,
strategy: PlacePieceStrategy,
) -> Result<(), PlacePieceError> {
) -> Result<Option<Piece>, PlacePieceError> {
self.pieces.place(piece, square, strategy)
}

View file

@ -106,9 +106,11 @@ impl PieceSet {
piece: Piece,
square: Square,
strategy: PlacePieceStrategy,
) -> Result<(), PlacePieceError> {
) -> Result<Option<Piece>, PlacePieceError> {
let existing_piece = self.mailbox.get(square);
if strategy == PlacePieceStrategy::PreserveExisting {
if let Some(piece) = self.mailbox.get(square) {
if let Some(piece) = existing_piece {
return Err(PlacePieceError::ExisitingPiece { piece, square });
}
}
@ -120,7 +122,7 @@ impl PieceSet {
self.shape_occupancy[shape as usize].set(square);
self.mailbox.set(piece, square);
Ok(())
Ok(existing_piece)
}
pub(crate) fn remove(&mut self, square: Square) -> Option<Piece> {