diff --git a/src/array_vec.rs b/src/array_vec.rs index ad11407..dec6f0f 100644 --- a/src/array_vec.rs +++ b/src/array_vec.rs @@ -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 { @@ -37,7 +37,7 @@ impl ArrayVec { } #[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 { diff --git a/src/bitboard.rs b/src/bitboard.rs index 3c7f392..f1b98f5 100644 --- a/src/bitboard.rs +++ b/src/bitboard.rs @@ -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) } } diff --git a/src/board.rs b/src/board.rs index 36cd23c..2230f20 100644 --- a/src/board.rs +++ b/src/board.rs @@ -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 { @@ -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 ByRole { #[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 { diff --git a/src/lib.rs b/src/lib.rs index 51c7a8e..2f39977 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/moves.rs b/src/moves.rs index eff1936..a383efe 100644 --- a/src/moves.rs +++ b/src/moves.rs @@ -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(&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( diff --git a/src/position.rs b/src/position.rs index 732c0ed..a22b5b2 100644 --- a/src/position.rs +++ b/src/position.rs @@ -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 for MateMoveGenImpl { struct MoveAndPromote + ExactSizeIterator + FusedIterator> { role: u8, inner: I, - cur: std::mem::MaybeUninit, + cur: core::mem::MaybeUninit, } impl MoveAndPromote where @@ -1100,7 +1100,7 @@ where Self { role: 1, inner, - cur: std::mem::MaybeUninit::uninit(), + cur: core::mem::MaybeUninit::uninit(), } } } diff --git a/src/san.rs b/src/san.rs index 473e6c7..bdf09bf 100644 --- a/src/san.rs +++ b/src/san.rs @@ -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 { @@ -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. diff --git a/src/setup.rs b/src/setup.rs index 7aa5e21..f181f00 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -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(&self, w: &mut W) -> std::fmt::Result + pub fn write_text_record(&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 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::(reason) }) + Some(unsafe { core::mem::transmute::(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 {} diff --git a/src/uci.rs b/src/uci.rs index 5a2bceb..979132d 100644 --- a/src/uci.rs +++ b/src/uci.rs @@ -22,9 +22,9 @@ pub struct UciMove { pub promotion: Option, } -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 { @@ -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.