new square constructors
This commit is contained in:
parent
de321719f4
commit
ad1c4c57ad
7 changed files with 59 additions and 44 deletions
34
src/board.rs
34
src/board.rs
|
|
@ -264,17 +264,26 @@ impl Square {
|
|||
]
|
||||
}
|
||||
|
||||
pub(crate) const fn from_index(index: u8) -> Option<Self> {
|
||||
pub const fn new(index: u8) -> Option<Self> {
|
||||
if index < 64 {
|
||||
Some(unsafe { Self::transmute(index) })
|
||||
Some(unsafe { Self::new_unchecked(index) })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// ## Safety
|
||||
///
|
||||
/// The caller must ensure that `index < 64`.
|
||||
#[inline]
|
||||
pub fn new(file: File, rank: Rank) -> Self {
|
||||
unsafe { Self::transmute(((rank as u8) << 3) | file as u8) }
|
||||
pub const 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]
|
||||
|
|
@ -290,7 +299,7 @@ impl Square {
|
|||
#[inline]
|
||||
pub fn mirror(self) -> Self {
|
||||
let sq = self as u8;
|
||||
unsafe { Self::transmute(sq & 0b000111 | (!sq & 0b111000)) }
|
||||
unsafe { Self::new_unchecked(sq & 0b000111 | (!sq & 0b111000)) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
|
@ -324,7 +333,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]
|
||||
|
|
@ -342,7 +354,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),
|
||||
|
|
@ -371,12 +383,6 @@ impl Square {
|
|||
Direction::West => file > 0,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) const unsafe fn transmute(value: u8) -> Self {
|
||||
debug_assert!(value < 64);
|
||||
std::mem::transmute(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Square {
|
||||
|
|
@ -434,7 +440,7 @@ impl OptionSquare {
|
|||
unsafe {
|
||||
match self {
|
||||
Self::None => None,
|
||||
_ => Some(Square::transmute(self as u8)),
|
||||
_ => Some(Square::new_unchecked(self as u8)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue