refactor: have movegen traits use iterables instead of vectors

This commit is contained in:
dogeystamp 2024-10-20 15:22:57 -04:00
parent c669aa6f89
commit 0c5d28b724

View File

@ -290,8 +290,7 @@ impl FromUCIAlgebraic for Move {
/// "Pseudo-legal" here means that moving into check is allowed, and capturing friendly pieces is /// "Pseudo-legal" here means that moving into check is allowed, and capturing friendly pieces is
/// allowed. These will be filtered out in the legal move generation step. /// allowed. These will be filtered out in the legal move generation step.
pub trait PseudoMoveGen { pub trait PseudoMoveGen {
type MoveIterable; fn gen_pseudo_moves(self) -> impl IntoIterator<Item = Move>;
fn gen_pseudo_moves(self) -> Self::MoveIterable;
} }
const DIRS_STRAIGHT: [(isize, isize); 4] = [(0, 1), (1, 0), (-1, 0), (0, -1)]; const DIRS_STRAIGHT: [(isize, isize); 4] = [(0, 1), (1, 0), (-1, 0), (0, -1)];
@ -379,9 +378,7 @@ fn move_slider(
} }
impl PseudoMoveGen for BoardState { impl PseudoMoveGen for BoardState {
type MoveIterable = Vec<Move>; fn gen_pseudo_moves(self) -> impl IntoIterator<Item = Move> {
fn gen_pseudo_moves(self) -> Self::MoveIterable {
let mut ret = Vec::new(); let mut ret = Vec::new();
let pl = self.pl(self.turn); let pl = self.pl(self.turn);
macro_rules! squares { macro_rules! squares {
@ -540,8 +537,7 @@ impl PseudoMoveGen for BoardState {
/// Legal move generation. /// Legal move generation.
pub trait LegalMoveGen { pub trait LegalMoveGen {
type MoveIterable; fn gen_moves(self) -> impl IntoIterator<Item = Move>;
fn gen_moves(self) -> Self::MoveIterable;
} }
#[cfg(test)] #[cfg(test)]
@ -777,7 +773,7 @@ mod tests {
for (fen, expected) in test_cases { for (fen, expected) in test_cases {
let board = BoardState::from_fen(fen).unwrap(); let board = BoardState::from_fen(fen).unwrap();
let mut moves = board.gen_pseudo_moves(); let mut moves: Vec<Move> = board.gen_pseudo_moves().into_iter().collect();
moves.sort_unstable(); moves.sort_unstable();
let moves = moves; let moves = moves;