// Eryn Wells use chessfriend_core::{Piece, PlacedPiece, Square}; use std::iter::FromIterator; #[derive(Clone, Debug, Eq, Hash, PartialEq)] pub(crate) struct Mailbox([Option; Square::NUM]); impl Mailbox { pub(crate) fn new() -> Self { Self::default() } pub(crate) fn get(&self, square: Square) -> Option { self.0[square as usize] } pub(crate) fn set(&mut self, piece: Piece, square: Square) { self.0[square as usize] = Some(piece); } pub(crate) fn remove(&mut self, square: Square) { self.0[square as usize] = None; } pub(crate) fn iter(&self) -> impl Iterator { self.0 .into_iter() .flatten() // Remove the Nones .zip(0u8..) // Enumerate with u8 instead of usize .map(|(piece, index)| { PlacedPiece::new(piece, unsafe { Square::from_index_unchecked(index) }) }) } } impl Default for Mailbox { fn default() -> Self { Self([None; Square::NUM]) } } impl From<[Option; Square::NUM]> for Mailbox { fn from(value: [Option; Square::NUM]) -> Self { Mailbox(value) } } impl FromIterator for Mailbox { fn from_iter>(iter: T) -> Self { let mut mailbox = Self::new(); for placed_piece in iter { mailbox.set(placed_piece.piece(), placed_piece.square()); } mailbox } } impl FromIterator<(Square, Piece)> for Mailbox { fn from_iter>(iter: T) -> Self { let mut mailbox = Self::new(); for (square, piece) in iter { mailbox.set(piece, square); } mailbox } }