[bitboard,core,position] Address a bunch of clippy warnings

This commit is contained in:
Eryn Wells 2024-03-14 17:00:46 -07:00
parent d0abbd8f93
commit 27a36565f3
8 changed files with 63 additions and 32 deletions

View file

@ -89,7 +89,7 @@ mod tests {
#[test]
fn leading_complex() {
let mut scanner = LeadingBitScanner::new(0b11000101);
let mut scanner = LeadingBitScanner::new(0b_1100_0101);
assert_eq!(scanner.next(), Some(7));
assert_eq!(scanner.next(), Some(6));
assert_eq!(scanner.next(), Some(2));
@ -112,7 +112,7 @@ mod tests {
#[test]
fn trailing_complex() {
let mut scanner = TrailingBitScanner::new(0b11000101);
let mut scanner = TrailingBitScanner::new(0b_1100_0101);
assert_eq!(scanner.next(), Some(0));
assert_eq!(scanner.next(), Some(2));
assert_eq!(scanner.next(), Some(6));

View file

@ -146,18 +146,23 @@ impl BitBoard {
}
impl BitBoard {
/// Return an Iterator over the occupied squares, starting from the leading
/// (most-significant bit) end of the field.
/// Returns an Iterator over the occupied squares.
///
/// The Iterator yields squares starting from the leading (most-significant bit) end of the
/// board to the trailing (least-significant bit) end.
#[must_use]
pub fn occupied_squares(&self) -> impl Iterator<Item = Square> {
LeadingBitScanner::new(self.0).map(|idx| unsafe { Square::from_index(idx as u8) })
}
/// Return an Iterator over the occupied squares, starting from the trailing
/// (least-significant bit) end of the field.
#[must_use]
pub fn occupied_squares_trailing(&self) -> impl Iterator<Item = Square> {
TrailingBitScanner::new(self.0).map(|idx| unsafe { Square::from_index(idx as u8) })
}
#[must_use]
pub fn first_occupied_square(&self) -> Option<Square> {
let leading_zeros = self.0.leading_zeros() as u8;
if leading_zeros < Square::NUM as u8 {
@ -167,6 +172,7 @@ impl BitBoard {
}
}
#[must_use]
pub fn first_occupied_square_trailing(&self) -> Option<Square> {
let trailing_zeros = self.0.trailing_zeros() as u8;
if trailing_zeros < Square::NUM as u8 {

View file

@ -12,17 +12,21 @@ use crate::BitBoard;
use chessfriend_core::{Color, Direction, Square};
use std::sync::OnceLock;
#[allow(clippy::identity_op)]
#[allow(clippy::erasing_op)]
pub(crate) const RANKS: [BitBoard; 8] = [
BitBoard(0xFF << 0 * 8),
BitBoard(0xFF << 1 * 8),
BitBoard(0xFF << 2 * 8),
BitBoard(0xFF << 3 * 8),
BitBoard(0xFF << 4 * 8),
BitBoard(0xFF << 5 * 8),
BitBoard(0xFF << 6 * 8),
BitBoard(0xFF << 7 * 8),
BitBoard(0xFF << (0 * 8)),
BitBoard(0xFF << (1 * 8)),
BitBoard(0xFF << (2 * 8)),
BitBoard(0xFF << (3 * 8)),
BitBoard(0xFF << (4 * 8)),
BitBoard(0xFF << (5 * 8)),
BitBoard(0xFF << (6 * 8)),
BitBoard(0xFF << (7 * 8)),
];
#[allow(clippy::identity_op)]
#[allow(clippy::erasing_op)]
pub(crate) const FILES: [BitBoard; 8] = [
BitBoard(0x0101010101010101 << 0),
BitBoard(0x0101010101010101 << 1),

View file

@ -3,8 +3,9 @@
use crate::{errors::TryFromCharError, try_from_string};
use std::fmt;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub enum Color {
#[default]
White = 0,
Black = 1,
}
@ -16,7 +17,9 @@ impl Color {
Color::ALL.iter()
}
pub fn other(&self) -> Color {
/// The other color
#[must_use]
pub const fn other(&self) -> Color {
match self {
Color::White => Color::Black,
Color::Black => Color::White,
@ -24,12 +27,6 @@ impl Color {
}
}
impl Default for Color {
fn default() -> Self {
Color::White
}
}
impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(

View file

@ -34,7 +34,7 @@ impl Shape {
PROMOTABLE_SHAPES.iter()
}
const fn to_ascii(&self) -> char {
const fn to_ascii(self) -> char {
match self {
Shape::Pawn => 'P',
Shape::Knight => 'N',
@ -79,7 +79,7 @@ impl Into<char> for Shape {
impl fmt::Display for Shape {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let self_char: char = self.into();
write!(f, "{}", self_char)
write!(f, "{self_char}")
}
}
@ -91,6 +91,7 @@ pub struct Piece {
macro_rules! piece_constructor {
($func_name:ident, $type:tt) => {
#[must_use]
pub fn $func_name(color: Color) -> Piece {
Piece {
color,
@ -102,6 +103,7 @@ macro_rules! piece_constructor {
macro_rules! is_shape {
($func_name:ident, $shape:ident) => {
#[must_use]
pub fn $func_name(&self) -> bool {
self.shape == Shape::$shape
}
@ -109,6 +111,7 @@ macro_rules! is_shape {
}
impl Piece {
#[must_use]
pub fn new(color: Color, shape: Shape) -> Piece {
Piece { color, shape }
}
@ -120,10 +123,12 @@ impl Piece {
piece_constructor!(queen, Queen);
piece_constructor!(king, King);
#[must_use]
pub fn color(&self) -> Color {
self.color
}
#[must_use]
pub fn shape(&self) -> Shape {
self.shape
}
@ -135,7 +140,8 @@ impl Piece {
is_shape!(is_queen, Queen);
is_shape!(is_king, King);
pub fn to_ascii(&self) -> char {
#[must_use]
pub fn to_ascii(self) -> char {
let ch = self.shape.to_ascii();
match self.color {
Color::White => ch,
@ -143,7 +149,8 @@ impl Piece {
}
}
fn to_unicode(&self) -> char {
#[must_use]
fn to_unicode(self) -> char {
match (self.color, self.shape) {
(Color::Black, Shape::Pawn) => '♟',
(Color::Black, Shape::Knight) => '♞',
@ -175,6 +182,7 @@ pub struct PlacedPiece {
macro_rules! is_shape {
($func_name:ident, $shape:ident) => {
#[must_use]
pub fn $func_name(&self) -> bool {
self.piece().shape == Shape::$shape
}
@ -182,26 +190,35 @@ macro_rules! is_shape {
}
impl PlacedPiece {
#[must_use]
pub const fn new(piece: Piece, square: Square) -> PlacedPiece {
PlacedPiece { piece, square }
}
/// The [Piece] itself
#[inline]
#[must_use]
pub fn piece(&self) -> &Piece {
&self.piece
}
/// The square the piece is on
#[inline]
#[must_use]
pub fn square(&self) -> Square {
self.square
}
/// The piece's [Color]
#[inline]
#[must_use]
pub fn color(&self) -> Color {
self.piece.color
}
/// The piece's [Shape]
#[inline]
#[must_use]
pub fn shape(&self) -> Shape {
self.piece.shape
}
@ -213,6 +230,7 @@ impl PlacedPiece {
is_shape!(is_queen, Queen);
is_shape!(is_king, King);
#[must_use]
pub fn is_kingside_rook(&self) -> bool {
self.is_rook()
&& match self.color() {
@ -221,6 +239,7 @@ impl PlacedPiece {
}
}
#[must_use]
pub fn is_queenside_rook(&self) -> bool {
self.is_rook()
&& match self.color() {

View file

@ -1,10 +1,9 @@
// Eryn Wells <eryn@erynwells.me>
use crate::sight::SliderRayToSquareExt;
use chessfriend_bitboard::BitBoard;
use chessfriend_core::Shape;
use crate::sight::SliderRayToSquareExt;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CheckingPieces {
bitboards: [BitBoard; 5],
@ -23,12 +22,12 @@ impl CheckingPieces {
}
}
/// The number of checking pieces.
pub fn count(&self) -> u32 {
self.bitboards.iter().map(BitBoard::population_count).sum()
}
/// A BitBoard representing the set of pieces that must be captured to
/// resolve check.
/// A BitBoard representing the set of pieces that must be captured to resolve check.
pub fn capture_mask(&self) -> BitBoard {
if self.count() == 0 {
BitBoard::FULL
@ -39,8 +38,8 @@ impl CheckingPieces {
}
}
/// A BitBoard representing the set of squares to which a player can move a
/// piece to block a checking piece.
/// A BitBoard representing the set of squares to which a player can move a piece to block a
/// checking piece.
pub fn push_mask(&self, king: &BitBoard) -> BitBoard {
let target = king.first_occupied_square().unwrap();

View file

@ -78,7 +78,7 @@ impl PieceBitBoards {
}
pub(super) fn place_piece(&mut self, piece: &PlacedPiece) -> Result<(), PlacePieceError> {
self.place_piece_with_strategy(piece, Default::default())
self.place_piece_with_strategy(piece, PlacePieceStrategy::default())
}
pub(super) fn place_piece_with_strategy(

View file

@ -138,22 +138,27 @@ impl Position {
})
}
/// Return a BitBoard representing the set of squares containing a piece.
/// A [BitBoard] representing the set of squares containing a piece.
#[inline]
#[must_use]
pub(crate) fn occupied_squares(&self) -> &BitBoard {
&self.pieces.all_pieces()
}
#[inline]
#[must_use]
pub(crate) fn friendly_pieces(&self) -> &BitBoard {
self.pieces.all_pieces_of_color(self.color_to_move)
}
#[inline]
#[must_use]
pub(crate) fn opposing_pieces(&self) -> &BitBoard {
self.pieces.all_pieces_of_color(self.color_to_move.other())
}
#[inline]
#[must_use]
pub(crate) fn all_pieces(&self) -> (&BitBoard, &BitBoard) {
(self.friendly_pieces(), self.opposing_pieces())
}
@ -161,6 +166,7 @@ impl Position {
/// Return a BitBoard representing the set of squares containing a piece.
/// This set is the inverse of `occupied_squares`.
#[inline]
#[must_use]
pub(crate) fn empty_squares(&self) -> BitBoard {
!self.occupied_squares()
}