1
0
Fork 0
This commit is contained in:
Paul-Nicolas Madelaine 2025-11-17 22:07:04 +01:00
parent c0e915f1b4
commit 00261d95c1
9 changed files with 102 additions and 96 deletions

View file

@ -1,6 +1,6 @@
use std::iter::ExactSizeIterator;
use std::iter::FusedIterator;
use std::mem::MaybeUninit;
use core::iter::ExactSizeIterator;
use core::iter::FusedIterator;
use core::mem::MaybeUninit;
#[derive(Clone)]
pub(crate) struct ArrayVec<T: Copy, const N: usize> {
@ -37,7 +37,7 @@ impl<T: Copy, const N: usize> ArrayVec<T, N> {
}
#[inline]
pub(crate) fn as_slice_mut(&mut self) -> &mut [T] {
unsafe { std::mem::transmute::<_, &mut [T]>(self.array.get_unchecked_mut(0..self.len)) }
unsafe { core::mem::transmute::<_, &mut [T]>(self.array.get_unchecked_mut(0..self.len)) }
}
}
impl<'l, T: Copy, const N: usize> IntoIterator for &'l ArrayVec<T, N> {

View file

@ -2,8 +2,8 @@
use crate::board::*;
use std::iter::ExactSizeIterator;
use std::iter::FusedIterator;
use core::iter::ExactSizeIterator;
use core::iter::FusedIterator;
/// A set of squares.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
@ -87,46 +87,46 @@ impl Bitboard {
}
}
impl std::ops::BitOr for Bitboard {
impl core::ops::BitOr for Bitboard {
type Output = Self;
#[inline]
fn bitor(self, rhs: Self) -> Self::Output {
Self(self.0 | rhs.0)
}
}
impl std::ops::BitAnd for Bitboard {
impl core::ops::BitAnd for Bitboard {
type Output = Self;
#[inline]
fn bitand(self, rhs: Self) -> Self::Output {
Self(self.0 & rhs.0)
}
}
impl std::ops::BitXor for Bitboard {
impl core::ops::BitXor for Bitboard {
type Output = Self;
#[inline]
fn bitxor(self, rhs: Self) -> Self::Output {
Self(self.0 ^ rhs.0)
}
}
impl std::ops::BitOrAssign for Bitboard {
impl core::ops::BitOrAssign for Bitboard {
#[inline]
fn bitor_assign(&mut self, rhs: Self) {
self.0 |= rhs.0;
}
}
impl std::ops::BitAndAssign for Bitboard {
impl core::ops::BitAndAssign for Bitboard {
#[inline]
fn bitand_assign(&mut self, rhs: Self) {
self.0 &= rhs.0;
}
}
impl std::ops::BitXorAssign for Bitboard {
impl core::ops::BitXorAssign for Bitboard {
#[inline]
fn bitxor_assign(&mut self, rhs: Self) {
self.0 ^= rhs.0;
}
}
impl std::ops::Not for Bitboard {
impl core::ops::Not for Bitboard {
type Output = Self;
#[inline]
fn not(self) -> Self::Output {
@ -182,8 +182,8 @@ impl ExactSizeIterator for Bitboard {
}
}
impl std::fmt::Debug for Bitboard {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Debug for Bitboard {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Bitboard(0x{:016X})", self.0)
}
}

View file

@ -72,7 +72,7 @@ impl Color {
}
}
impl std::ops::Not for Color {
impl core::ops::Not for Color {
type Output = Self;
#[inline]
fn not(self) -> Self::Output {
@ -126,7 +126,7 @@ impl File {
#[inline]
pub const unsafe fn new_unchecked(index: u8) -> Self {
debug_assert!(index < 8);
unsafe { std::mem::transmute(index) }
unsafe { core::mem::transmute(index) }
}
#[inline]
@ -166,9 +166,9 @@ impl File {
}
}
impl std::fmt::Display for File {
impl core::fmt::Display for File {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{}", self.to_char())
}
}
@ -216,7 +216,7 @@ impl Rank {
#[inline]
pub const unsafe fn new_unchecked(index: u8) -> Self {
debug_assert!(index < 8);
unsafe { std::mem::transmute(index) }
unsafe { core::mem::transmute(index) }
}
#[inline]
@ -261,9 +261,9 @@ impl Rank {
}
}
impl std::fmt::Display for Rank {
impl core::fmt::Display for Rank {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{}", self.to_char())
}
}
@ -315,7 +315,7 @@ impl Square {
#[inline]
pub const unsafe fn new_unchecked(index: u8) -> Self {
debug_assert!(index < 64);
unsafe { std::mem::transmute(index) }
unsafe { core::mem::transmute(index) }
}
#[inline]
@ -422,9 +422,9 @@ impl Square {
}
}
impl std::fmt::Display for Square {
impl core::fmt::Display for Square {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.write_str(self.to_str())
}
}
@ -432,14 +432,14 @@ impl std::fmt::Display for Square {
/// An error while parsing a [`Square`].
#[derive(Debug)]
pub struct ParseSquareError;
impl std::fmt::Display for ParseSquareError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for ParseSquareError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("invalid square syntax")
}
}
impl std::error::Error for ParseSquareError {}
impl core::error::Error for ParseSquareError {}
impl std::str::FromStr for Square {
impl core::str::FromStr for Square {
type Err = ParseSquareError;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
@ -484,7 +484,7 @@ impl OptionSquare {
#[inline]
pub(crate) fn from_square(square: Square) -> Self {
unsafe { std::mem::transmute(square) }
unsafe { core::mem::transmute(square) }
}
#[inline]
@ -558,7 +558,7 @@ impl Role {
#[inline]
pub(crate) unsafe fn transmute(i: u8) -> Self {
debug_assert!(i >= 1 && i < 7);
unsafe { std::mem::transmute(i) }
unsafe { core::mem::transmute(i) }
}
}
@ -574,14 +574,14 @@ impl<T> ByRole<T> {
#[inline]
pub const fn get(&self, role: Role) -> &T {
let i = unsafe { (role as usize).unchecked_sub(1) };
unsafe { std::hint::assert_unchecked(i < 6) };
unsafe { core::hint::assert_unchecked(i < 6) };
&self.0[i]
}
#[inline]
pub const fn get_mut(&mut self, role: Role) -> &mut T {
let i = unsafe { (role as usize).unchecked_sub(1) };
unsafe { std::hint::assert_unchecked(i < 6) };
unsafe { core::hint::assert_unchecked(i < 6) };
&mut self.0[i]
}
@ -673,11 +673,11 @@ impl Direction {
#[inline]
const unsafe fn transmute(value: u8) -> Self {
debug_assert!(value < 8);
unsafe { std::mem::transmute(value) }
unsafe { core::mem::transmute(value) }
}
}
impl std::ops::Not for Direction {
impl core::ops::Not for Direction {
type Output = Self;
#[inline]
fn not(self) -> Self::Output {

View file

@ -72,6 +72,10 @@
//! - chess960 and other variants
//! - etc.
#![no_std]
extern crate alloc;
pub(crate) mod array_vec;
pub(crate) mod lookup;
pub(crate) mod magics;

View file

@ -7,9 +7,9 @@ use crate::position::*;
use crate::san::*;
use crate::uci::*;
use std::convert::Infallible;
use std::iter::{ExactSizeIterator, FusedIterator};
use std::ops::ControlFlow;
use core::convert::Infallible;
use core::iter::{ExactSizeIterator, FusedIterator};
use core::ops::ControlFlow;
/// A legal move.
#[must_use]
@ -299,7 +299,7 @@ impl<'l> Moves<'l> {
#[inline]
pub fn sort_by<F>(&mut self, mut compare: F)
where
F: FnMut(Move, Move) -> std::cmp::Ordering,
F: FnMut(Move, Move) -> core::cmp::Ordering,
{
self.array.as_slice_mut().sort_unstable_by(|a, b| {
compare(

View file

@ -8,9 +8,9 @@ use crate::san::*;
use crate::setup::*;
use crate::uci::*;
use std::convert::Infallible;
use std::iter::{ExactSizeIterator, FusedIterator};
use std::ops::ControlFlow;
use core::convert::Infallible;
use core::iter::{ExactSizeIterator, FusedIterator};
use core::ops::ControlFlow;
/// **A chess position.**
///
@ -462,8 +462,8 @@ impl Position {
}
}
impl std::fmt::Debug for Position {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
impl core::fmt::Debug for Position {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
f.debug_tuple("Position")
.field(&TextRecord(self.as_setup()))
.finish()
@ -629,10 +629,10 @@ impl Position {
.chain(theirs.bishop().map(|sq| lookup::bishop(sq, blockers)))
.chain(theirs.rook().map(|sq| lookup::rook(sq, blockers)))
.chain(theirs.knight().map(|sq| lookup::knight(sq)))
.chain(std::iter::once(
.chain(core::iter::once(
theirs.pawn().trans(!forward).trans(Direction::East),
))
.chain(std::iter::once(
.chain(core::iter::once(
theirs.pawn().trans(!forward).trans(Direction::West),
))
.reduce_or()
@ -659,7 +659,7 @@ impl Position {
.trans_unchecked(Direction::East)
};
if global_mask_to.contains(to) {
moves.extend(std::iter::once(RawMove {
moves.extend(core::iter::once(RawMove {
kind: MoveType::CastleShort,
from,
to,
@ -680,7 +680,7 @@ impl Position {
.trans_unchecked(Direction::West)
};
if global_mask_to.contains(to) {
moves.extend(std::iter::once(RawMove {
moves.extend(core::iter::once(RawMove {
kind: MoveType::CastleLong,
from,
to,
@ -823,7 +823,7 @@ impl Position {
& (!pinned | lookup::segment(king_square, to))
{
moves.en_passant_is_legal()?;
moves.extend(std::iter::once(RawMove {
moves.extend(core::iter::once(RawMove {
kind: MoveType::EnPassant,
from,
to,
@ -1089,7 +1089,7 @@ impl MoveGen<Infallible> for MateMoveGenImpl {
struct MoveAndPromote<I: Iterator<Item = RawMove> + ExactSizeIterator + FusedIterator> {
role: u8,
inner: I,
cur: std::mem::MaybeUninit<RawMove>,
cur: core::mem::MaybeUninit<RawMove>,
}
impl<I> MoveAndPromote<I>
where
@ -1100,7 +1100,7 @@ where
Self {
role: 1,
inner,
cur: std::mem::MaybeUninit::uninit(),
cur: core::mem::MaybeUninit::uninit(),
}
}
}

View file

@ -48,8 +48,8 @@ pub(crate) enum SanSuffix {
Checkmate,
}
impl std::fmt::Display for San {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
impl core::fmt::Display for San {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self.inner {
SanInner::Castle(CastlingSide::Short) => write!(f, "O-O")?,
SanInner::Castle(CastlingSide::Long) => write!(f, "O-O-O")?,
@ -88,13 +88,13 @@ impl std::fmt::Display for San {
}
}
impl std::fmt::Debug for San {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_tuple("San").field(&self.to_string()).finish()
impl core::fmt::Debug for San {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "San(\"{self}\")")
}
}
impl std::str::FromStr for San {
impl core::str::FromStr for San {
type Err = ParseSanError;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
@ -105,12 +105,12 @@ impl std::str::FromStr for San {
/// A syntax error when parsing [`San`] notation.
#[derive(Debug)]
pub struct ParseSanError;
impl std::fmt::Display for ParseSanError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for ParseSanError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("invalid SAN syntax")
}
}
impl std::error::Error for ParseSanError {}
impl core::error::Error for ParseSanError {}
/// An error while converting [`San`] notation to a playable [`Move`].
#[derive(Debug, PartialEq, Eq)]
@ -120,8 +120,8 @@ pub enum InvalidSan {
/// There is more than one move on the position that matches the SAN notation.
Ambiguous,
}
impl std::fmt::Display for InvalidSan {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for InvalidSan {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let details = match self {
Self::Illegal => "illegal move",
Self::Ambiguous => "ambiguous move",
@ -129,7 +129,7 @@ impl std::fmt::Display for InvalidSan {
write!(f, "invalid SAN ({details})")
}
}
impl std::error::Error for InvalidSan {}
impl core::error::Error for InvalidSan {}
impl San {
/// Tries to convert SAN notation to a playable move.

View file

@ -7,6 +7,8 @@ use crate::board::*;
use crate::lookup;
use crate::position::*;
use alloc::string::String;
/// **A builder type for chess positions.**
///
/// This type is useful to edit a position without having to ensure it stays legal at every step.
@ -152,9 +154,9 @@ impl Setup {
}
/// Writes the text record of the position.
pub fn write_text_record<W>(&self, w: &mut W) -> std::fmt::Result
pub fn write_text_record<W>(&self, w: &mut W) -> core::fmt::Result
where
W: std::fmt::Write,
W: core::fmt::Write,
{
for rank in Rank::all().into_iter().rev() {
let mut count = 0;
@ -526,14 +528,14 @@ impl TryFrom<Setup> for Position {
}
pub(crate) struct TextRecord<'a>(pub(crate) &'a Setup);
impl<'a> std::fmt::Display for TextRecord<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl<'a> core::fmt::Display for TextRecord<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.0.write_text_record(f)
}
}
impl<'a> std::fmt::Debug for TextRecord<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use std::fmt::Write;
impl<'a> core::fmt::Debug for TextRecord<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
use core::fmt::Write;
f.write_char('"')?;
self.0.write_text_record(f)?;
f.write_char('"')?;
@ -541,8 +543,8 @@ impl<'a> std::fmt::Debug for TextRecord<'a> {
}
}
impl std::fmt::Debug for Setup {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
impl core::fmt::Debug for Setup {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
f.debug_tuple("Setup").field(&TextRecord(&self)).finish()
}
}
@ -555,9 +557,9 @@ pub struct IllegalPosition {
pub setup: Setup,
pub reasons: IllegalPositionReasons,
}
impl std::fmt::Display for IllegalPosition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use std::fmt::Write;
impl core::fmt::Display for IllegalPosition {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
use core::fmt::Write;
let setup = &self.setup;
write!(f, "`{}` is illegal:", &TextRecord(setup))?;
let mut first = true;
@ -572,14 +574,14 @@ impl std::fmt::Display for IllegalPosition {
Ok(())
}
}
impl std::error::Error for IllegalPosition {}
impl core::error::Error for IllegalPosition {}
/// A set of [`IllegalPositionReason`]s.
#[derive(Clone, Copy)]
pub struct IllegalPositionReasons(u8);
impl std::fmt::Debug for IllegalPositionReasons {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Debug for IllegalPositionReasons {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_list().entries(*self).finish()
}
}
@ -607,7 +609,7 @@ impl Iterator for IllegalPositionReasons {
} else {
let reason = 1 << self.0.trailing_zeros();
self.0 &= !reason;
Some(unsafe { std::mem::transmute::<u8, IllegalPositionReason>(reason) })
Some(unsafe { core::mem::transmute::<u8, IllegalPositionReason>(reason) })
}
}
}
@ -659,8 +661,8 @@ impl IllegalPositionReason {
}
}
impl std::fmt::Display for IllegalPositionReason {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for IllegalPositionReason {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.to_str())
}
}
@ -670,9 +672,9 @@ impl std::fmt::Display for IllegalPositionReason {
pub struct ParseRecordError {
pub byte: usize,
}
impl std::fmt::Display for ParseRecordError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for ParseRecordError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "invalid text record (at byte {})", self.byte)
}
}
impl std::error::Error for ParseRecordError {}
impl core::error::Error for ParseRecordError {}

View file

@ -22,9 +22,9 @@ pub struct UciMove {
pub promotion: Option<Role>,
}
impl std::fmt::Display for UciMove {
impl core::fmt::Display for UciMove {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{}{}", self.from, self.to)?;
if let Some(promotion) = self.promotion {
write!(f, "{}", promotion.to_char_lowercase())?;
@ -33,13 +33,13 @@ impl std::fmt::Display for UciMove {
}
}
impl std::fmt::Debug for UciMove {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
f.debug_tuple("UciMove").field(&self.to_string()).finish()
impl core::fmt::Debug for UciMove {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
write!(f, "UciMove(\"{self}\")")
}
}
impl std::str::FromStr for UciMove {
impl core::str::FromStr for UciMove {
type Err = ParseUciMoveError;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
@ -50,12 +50,12 @@ impl std::str::FromStr for UciMove {
/// A syntax error when parsing a [`UciMove`].
#[derive(Debug)]
pub struct ParseUciMoveError;
impl std::fmt::Display for ParseUciMoveError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for ParseUciMoveError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("invalid UCI notation")
}
}
impl std::error::Error for ParseUciMoveError {}
impl core::error::Error for ParseUciMoveError {}
/// An error when converting [`UciMove`] notation to a playable [`Move`].
#[derive(Debug)]
@ -63,12 +63,12 @@ pub enum InvalidUciMove {
/// The is no move on the position that matches the UCI notation.
Illegal,
}
impl std::fmt::Display for InvalidUciMove {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for InvalidUciMove {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("illegal UCI move")
}
}
impl std::error::Error for InvalidUciMove {}
impl core::error::Error for InvalidUciMove {}
impl UciMove {
/// Tries to convert UCI notation to a playable move.