From 66e5109157c9189722031c93dd0c62791f6f0b91 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 ac3e91e..1a64929 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)] @@ -70,6 +71,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 ffc5314..a3c783a 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)] @@ -70,6 +71,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 437d2e6..e8588eb 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. @@ -179,6 +180,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::*;