[board] Create const arrays for Shape that return &'static to its enum cases

This commit is contained in:
Eryn Wells 2023-12-31 11:40:54 -08:00
parent 878d771d86
commit 750b16970f
3 changed files with 17 additions and 8 deletions

View file

@ -2,6 +2,7 @@
use crate::Square;
use std::fmt;
use std::slice::Iter;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Color {
@ -33,16 +34,24 @@ pub enum Shape {
}
impl Shape {
pub fn iter() -> impl Iterator<Item = Shape> {
[
pub fn iter() -> Iter<'static, Shape> {
const ALL_SHAPES: [Shape; 6] = [
Shape::Pawn,
Shape::Knight,
Shape::Bishop,
Shape::Rook,
Shape::Queen,
Shape::King,
]
.into_iter()
];
ALL_SHAPES.iter()
}
pub fn promotable() -> Iter<'static, Shape> {
const PROMOTABLE_SHAPES: [Shape; 4] =
[Shape::Queen, Shape::Rook, Shape::Bishop, Shape::Knight];
PROMOTABLE_SHAPES.iter()
}
fn _ascii_representation(&self) -> char {

View file

@ -11,7 +11,7 @@ pub struct Pieces<'a> {
current_shape: Option<Shape>,
shape_iterator: Box<dyn Iterator<Item = Shape>>,
shape_iterator: Box<dyn Iterator<Item = &'static Shape>>,
square_iterator: Option<Box<dyn Iterator<Item = Square>>>,
}
@ -41,7 +41,7 @@ impl<'a> Iterator for Pieces<'a> {
let mut next_nonempty_bitboard: Option<&BitBoard> = None;
while let Some(shape) = self.shape_iterator.next() {
let piece = Piece::new(self.color, shape);
let piece = Piece::new(self.color, *shape);
let bitboard = self.position.bitboard_for_piece(piece);
if bitboard.is_empty() {
@ -49,7 +49,7 @@ impl<'a> Iterator for Pieces<'a> {
}
next_nonempty_bitboard = Some(bitboard);
current_shape = Some(shape);
current_shape = Some(*shape);
break;
}

View file

@ -129,7 +129,7 @@ impl Position {
pub fn piece_on_square(&self, sq: Square) -> Option<PlacedPiece> {
for color in Color::iter() {
for shape in Shape::iter() {
let piece = Piece::new(color, shape);
let piece = Piece::new(color, *shape);
let bb = self.bitboard_for_piece(piece);
if bb.has_piece_at(&sq) {
return Some(PlacedPiece::new(piece, sq));