[board] Implement Mailbox::from_iter as a reduce (aka fold) over the incoming iterator

This commit is contained in:
Eryn Wells 2025-05-02 14:48:37 -07:00
parent 72fd938238
commit 46b19ff616

View file

@ -48,22 +48,20 @@ impl From<[Option<Piece>; Square::NUM]> for Mailbox {
impl FromIterator<PlacedPiece> for Mailbox {
fn from_iter<T: IntoIterator<Item = PlacedPiece>>(iter: T) -> Self {
let mut mailbox = Self::new();
for placed_piece in iter {
mailbox.set(placed_piece.piece(), placed_piece.square());
}
mailbox
iter.into_iter()
.fold(Self::new(), |mut mailbox, placed_piece| {
mailbox.set(placed_piece.piece(), placed_piece.square());
mailbox
})
}
}
impl FromIterator<(Square, Piece)> for Mailbox {
fn from_iter<T: IntoIterator<Item = (Square, Piece)>>(iter: T) -> Self {
let mut mailbox = Self::new();
for (square, piece) in iter {
mailbox.set(piece, square);
}
mailbox
iter.into_iter()
.fold(Self::new(), |mut mailbox, (square, piece)| {
mailbox.set(piece, square);
mailbox
})
}
}