From 585c127381d909d10fb64eb162169d109564b57a Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 18 Jul 2022 11:05:38 +0200 Subject: [PATCH] Statically assert zero-cost invariants Since some or all of those invariants will come in handy to ensure we use as little memory as possible, to maximize the speed of the move generation later on. --- src/board/bitboard/mod.rs | 5 +++++ src/board/file.rs | 4 ++++ src/board/rank.rs | 4 ++++ src/board/square.rs | 4 ++++ 4 files changed, 17 insertions(+) diff --git a/src/board/bitboard/mod.rs b/src/board/bitboard/mod.rs index 092dba7..8b716be 100644 --- a/src/board/bitboard/mod.rs +++ b/src/board/bitboard/mod.rs @@ -1,4 +1,6 @@ use super::Square; +use crate::utils::static_assert; + mod iterator; use iterator::*; @@ -63,6 +65,9 @@ impl Bitboard { } } +// Ensure zero-cost (at least size-wise) wrapping. +static_assert!(std::mem::size_of::() == std::mem::size_of::()); + impl Default for Bitboard { fn default() -> Self { Self::EMPTY diff --git a/src/board/file.rs b/src/board/file.rs index 3dac5c8..fa7ba15 100644 --- a/src/board/file.rs +++ b/src/board/file.rs @@ -1,4 +1,5 @@ use super::Bitboard; +use crate::utils::static_assert; /// An enum representing a singular file on a chess board (i.e: the columns). #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -74,6 +75,9 @@ impl File { } } +// Ensure that niche-optimization is in effect. +static_assert!(std::mem::size_of::>() == std::mem::size_of::()); + #[cfg(test)] mod test { use super::*; diff --git a/src/board/rank.rs b/src/board/rank.rs index c2499d7..59374c7 100644 --- a/src/board/rank.rs +++ b/src/board/rank.rs @@ -1,4 +1,5 @@ use super::Bitboard; +use crate::utils::static_assert; /// An enum representing a singular rank on a chess board (i.e: the rows). #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -74,6 +75,9 @@ impl Rank { } } +// Ensure that niche-optimization is in effect. +static_assert!(std::mem::size_of::>() == std::mem::size_of::()); + #[cfg(test)] mod test { use super::*; diff --git a/src/board/square.rs b/src/board/square.rs index 0079bbb..69d8167 100644 --- a/src/board/square.rs +++ b/src/board/square.rs @@ -1,4 +1,5 @@ use super::{Bitboard, File, Rank}; +use crate::utils::static_assert; /// Represent a square on a chessboard. Defined in the same order as the /// [Bitboard](crate::board::Bitboard) squares. @@ -185,6 +186,9 @@ impl std::ops::Sub for Square { } } +// Ensure that niche-optimization is in effect. +static_assert!(std::mem::size_of::>() == std::mem::size_of::()); + #[cfg(test)] mod test { use super::*;