1
0
Fork 0

new constructors

This commit is contained in:
Paul-Nicolas Madelaine 2025-10-19 14:28:36 +02:00
parent 808cde1c42
commit 494de58804
6 changed files with 92 additions and 56 deletions

View file

@ -110,6 +110,17 @@ impl File {
]
}
#[inline]
pub fn new(index: u8) -> Option<Self> {
(index < 8).then(|| unsafe { Self::new_unchecked(index) })
}
#[inline]
pub unsafe fn new_unchecked(index: u8) -> Self {
debug_assert!(index < 8);
std::mem::transmute(index)
}
#[inline]
pub fn to_char(self) -> char {
self.to_ascii() as char
@ -128,7 +139,10 @@ impl File {
#[inline]
pub(crate) fn from_ascii(c: u8) -> Option<Self> {
(c <= b'h')
.then(|| c.checked_sub(b'a').map(|i| unsafe { Self::transmute(i) }))
.then(|| {
c.checked_sub(b'a')
.map(|i| unsafe { Self::new_unchecked(i) })
})
.flatten()
}
@ -136,12 +150,6 @@ impl File {
pub(crate) fn bitboard(self) -> Bitboard {
Bitboard(0x0101010101010101 << (self as u8))
}
#[inline]
pub(crate) unsafe fn transmute(value: u8) -> Self {
debug_assert!(value < 8);
std::mem::transmute(value)
}
}
impl std::fmt::Display for File {
@ -182,6 +190,17 @@ impl Rank {
]
}
#[inline]
pub fn new(index: u8) -> Option<Self> {
(index < 8).then(|| unsafe { Self::new_unchecked(index) })
}
#[inline]
pub unsafe fn new_unchecked(index: u8) -> Self {
debug_assert!(index < 8);
std::mem::transmute(index)
}
#[inline]
pub fn to_char(self) -> char {
self.to_ascii() as char
@ -194,7 +213,7 @@ impl Rank {
#[inline]
pub fn mirror(self) -> Self {
unsafe { Self::transmute(7_u8.unchecked_sub(self as u8)) }
unsafe { Self::new_unchecked(7_u8.unchecked_sub(self as u8)) }
}
#[inline]
@ -205,19 +224,16 @@ impl Rank {
#[inline]
pub(crate) fn from_ascii(c: u8) -> Option<Self> {
(c <= b'8')
.then(|| c.checked_sub(b'1').map(|i| unsafe { Self::transmute(i) }))
.then(|| {
c.checked_sub(b'1')
.map(|i| unsafe { Self::new_unchecked(i) })
})
.flatten()
}
#[inline]
pub(crate) fn bitboard(self) -> Bitboard {
Bitboard(0xFF << ((self as u64) << 3))
}
#[inline]
pub(crate) unsafe fn transmute(value: u8) -> Self {
debug_assert!(value < 8);
std::mem::transmute(value)
Bitboard(0xFF << ((self as u8) << 3))
}
}
@ -262,23 +278,34 @@ impl Square {
}
#[inline]
pub fn new(file: File, rank: Rank) -> Self {
unsafe { Self::transmute(((rank as u8) << 3) | file as u8) }
pub fn new(index: u8) -> Option<Self> {
(index < 64).then(|| unsafe { Self::new_unchecked(index) })
}
#[inline]
pub unsafe fn new_unchecked(index: u8) -> Self {
debug_assert!(index < 64);
std::mem::transmute(index)
}
#[inline]
pub fn from_coords(file: File, rank: Rank) -> Self {
unsafe { Self::new_unchecked(((rank as u8) << 3) | file as u8) }
}
#[inline]
pub fn file(self) -> File {
unsafe { File::transmute((self as u8) & 7) }
unsafe { File::new_unchecked((self as u8) & 7) }
}
#[inline]
pub fn rank(self) -> Rank {
unsafe { Rank::transmute((self as u8) >> 3) }
unsafe { Rank::new_unchecked((self as u8) >> 3) }
}
#[inline]
pub fn mirror(self) -> Self {
Self::new(self.file(), self.rank().mirror())
Self::from_coords(self.file(), self.rank().mirror())
}
#[inline]
@ -312,7 +339,10 @@ impl Square {
#[inline]
pub(crate) fn from_ascii(s: &[u8; 2]) -> Option<Self> {
let [f, r] = *s;
Some(Self::new(File::from_ascii(f)?, Rank::from_ascii(r)?))
Some(Self::from_coords(
File::from_ascii(f)?,
Rank::from_ascii(r)?,
))
}
#[inline]
@ -329,7 +359,7 @@ impl Square {
debug_assert!(self.check_trans(direction));
let i = self as u8;
unsafe {
Self::transmute(match direction {
Self::new_unchecked(match direction {
Direction::East => i.unchecked_add(1),
Direction::NorthEast => i.unchecked_add(9),
Direction::North => i.unchecked_add(8),
@ -356,12 +386,6 @@ impl Square {
Direction::West => self.file() > File::A,
}
}
#[inline]
pub(crate) unsafe fn transmute(value: u8) -> Self {
debug_assert!(value < 64);
std::mem::transmute(value)
}
}
impl std::fmt::Display for Square {
@ -419,7 +443,7 @@ impl OptionSquare {
unsafe {
match self {
Self::None => None,
_ => Some(Square::transmute(self as u8)),
_ => Some(Square::new_unchecked(self as u8)),
}
}
}