Add 'Square' constructor from 'File', 'Rank'

This commit is contained in:
Bruno BELANYI 2022-07-16 14:23:19 +02:00
parent c3c3684150
commit 6501466d3e

View file

@ -34,6 +34,13 @@ impl Square {
Self::H1, Self::H2, Self::H3, Self::H4, Self::H5, Self::H6, Self::H7, Self::H8, Self::H1, Self::H2, Self::H3, Self::H4, Self::H5, Self::H6, Self::H7, Self::H8,
]; ];
/// Construct a [Square] from a [File] and [Rank].
#[inline(always)]
pub fn new(file: File, rank: Rank) -> Self {
// SAFETY: we know the value is in-bounds
unsafe { Self::from_index_unchecked(file.index() * 8 + rank.index()) }
}
/// Iterate over all squares in order. /// Iterate over all squares in order.
pub fn iter() -> impl Iterator<Item = Square> { pub fn iter() -> impl Iterator<Item = Square> {
Self::ALL.iter().cloned() Self::ALL.iter().cloned()
@ -179,6 +186,14 @@ mod test {
use crate::board::file::*; use crate::board::file::*;
use crate::board::rank::*; use crate::board::rank::*;
#[test]
fn new() {
assert_eq!(Square::new(File::A, Rank::First), Square::A1);
assert_eq!(Square::new(File::A, Rank::Second), Square::A2);
assert_eq!(Square::new(File::B, Rank::First), Square::B1);
assert_eq!(Square::new(File::H, Rank::Eighth), Square::H8);
}
#[test] #[test]
fn file() { fn file() {
assert_eq!(Square::A1.file(), File::A); assert_eq!(Square::A1.file(), File::A);