[position] Streamline the implementation of castling_{king,rook}

Instead of using .and_then with an if/else, use .filter and pass the predicate.
Saves a few lines.
This commit is contained in:
Eryn Wells 2025-05-21 08:26:34 -07:00
parent feaa81bbd8
commit 85c1a395c4

View file

@ -69,23 +69,15 @@ impl Position {
}
pub(crate) fn castling_king(&self, square: Square) -> Option<Piece> {
self.get_piece(square).and_then(|piece| {
if piece.color == self.board.active_color && piece.is_king() {
Some(piece)
} else {
None
}
})
let active_color = self.board.active_color;
self.get_piece(square)
.filter(|piece| piece.color == active_color && piece.is_king())
}
pub(crate) fn castling_rook(&self, square: Square) -> Option<Piece> {
self.get_piece(square).and_then(|piece| {
if piece.color == self.board.active_color && piece.is_rook() {
Some(piece)
} else {
None
}
})
let active_color = self.board.active_color;
self.get_piece(square)
.filter(|piece| piece.color == active_color && piece.is_rook())
}
}