Skip to content

Commit 6f3968f

Browse files
committed
fix: const fn's that don't take arguments should just be consts
1 parent ea8a3a0 commit 6f3968f

3 files changed

Lines changed: 25 additions & 37 deletions

File tree

src/board.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ pub const ROW_COUNT: usize = 8;
1515
#[derive(Copy, Clone, PartialEq, Eq)]
1616
pub struct Board(pub [[Option<Piece>; ROW_COUNT]; COL_COUNT]);
1717
impl Board {
18-
#[must_use]
19-
pub const fn empty() -> Self {
20-
Self([[None; ROW_COUNT]; COL_COUNT])
21-
}
18+
pub const EMPTY: Self = Self([[None; ROW_COUNT]; COL_COUNT]);
2219

2320
pub(crate) fn threatening_moves_by(
2421
&self,
@@ -60,13 +57,11 @@ impl Board {
6057
self[start] = None;
6158
}
6259

63-
#[must_use]
64-
pub const fn new() -> Self {
65-
#[allow(clippy::wildcard_imports)]
60+
pub const NEW: Self = const {
6661
use crate::coord::Square as S;
6762
use crate::piece::Piece as P;
6863

69-
let mut board = Self::empty();
64+
let mut board = Self::EMPTY;
7065

7166
board[S::A1] = Some(P::WHITE_ROOK);
7267
board[S::B1] = Some(P::WHITE_KNIGHT);
@@ -105,7 +100,7 @@ impl Board {
105100
board[S::H7] = Some(P::BLACK_PAWN);
106101

107102
board
108-
}
103+
};
109104

110105
#[must_use]
111106
pub(crate) fn piece_counts(&self) -> PieceCounts {
@@ -120,7 +115,7 @@ impl Board {
120115
}
121116
impl const Default for Board {
122117
fn default() -> Self {
123-
Self::new()
118+
Self::NEW
124119
}
125120
}
126121
impl const core::ops::Index<Square> for Board {

src/game.rs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,12 @@ impl IndexMut<Piece> for PieceCounts {
147147
pub struct FullMoveCount(pub NonZeroU64); // non-zero & unsigned because this always starts at 1 and cant decrease
148148
impl const Default for FullMoveCount {
149149
fn default() -> Self {
150-
Self::new()
150+
Self::NEW
151151
}
152152
}
153153
impl FullMoveCount {
154-
#[must_use]
155-
pub const fn new() -> Self {
156-
const { Self(NonZeroU64::new(1).expect("1 to not be 0")) }
157-
}
154+
pub const NEW: Self = const { Self(NonZeroU64::new(1).expect("1 to not be 0")) };
155+
158156
pub const fn increase(&mut self) {
159157
self.0 = self
160158
.0
@@ -452,25 +450,20 @@ impl CastlingRights {
452450
black_queenside,
453451
}
454452
}
455-
#[must_use]
456-
pub const fn all_available() -> Self {
457-
Self::new(
458-
CastlingRight::Available,
459-
CastlingRight::Available,
460-
CastlingRight::Available,
461-
CastlingRight::Available,
462-
)
463-
}
464453

465-
#[must_use]
466-
pub const fn none_available() -> Self {
467-
Self::new(
468-
CastlingRight::Unavailable,
469-
CastlingRight::Unavailable,
470-
CastlingRight::Unavailable,
471-
CastlingRight::Unavailable,
472-
)
473-
}
454+
pub const ALL_AVAILABLE: Self = Self::new(
455+
CastlingRight::Available,
456+
CastlingRight::Available,
457+
CastlingRight::Available,
458+
CastlingRight::Available,
459+
);
460+
461+
pub const NONE_AVAILABLE: Self = Self::new(
462+
CastlingRight::Unavailable,
463+
CastlingRight::Unavailable,
464+
CastlingRight::Unavailable,
465+
CastlingRight::Unavailable,
466+
);
474467

475468
pub const ALL: [Self; 16] = [
476469
Self::new(O, O, O, O),
@@ -496,7 +489,7 @@ const O: CastlingRight = CastlingRight::Unavailable;
496489

497490
impl const Default for CastlingRights {
498491
fn default() -> Self {
499-
Self::all_available()
492+
Self::ALL_AVAILABLE
500493
}
501494
}
502495

src/notation/fen.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl CastlingRights {
133133
#[must_use]
134134
fn from_fen_repr(value: &[AsciiChar]) -> Self {
135135
if value == [AsciiChar::HyphenMinus] {
136-
return Self::none_available();
136+
return Self::NONE_AVAILABLE;
137137
}
138138
Self {
139139
white_kingside: value.contains(&AsciiChar::CapitalK).into(), // `K`
@@ -145,7 +145,7 @@ impl CastlingRights {
145145

146146
#[must_use]
147147
fn to_fen_repr(self) -> Vec<AsciiChar> {
148-
if self == Self::none_available() {
148+
if self == Self::NONE_AVAILABLE {
149149
return vec![AsciiChar::HyphenMinus];
150150
}
151151
let mut out = vec![];
@@ -321,7 +321,7 @@ impl Board {
321321
.map(Board)
322322
.map_err(|_| BoardFromFenError::IllegalColDimensions)?;
323323

324-
let mut new_board = Self::empty();
324+
let mut new_board = Self::EMPTY;
325325

326326
//TODO: un-fuck this
327327
for square in Square::ALL {

0 commit comments

Comments
 (0)