Compare commits

...

3 commits

Author SHA1 Message Date
Bruno BELANYI 66e5109157 Statically assert zero-cost invariants
All checks were successful
continuous-integration/drone/push Build is passing
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.
2022-07-18 11:05:41 +02:00
Bruno BELANYI ca4603ff02 Add 'static_assert' macro 2022-07-18 11:04:52 +02:00
Bruno BELANYI d132e3779e Enable 'doCheck' in nix package 2022-07-18 10:04:47 +02:00
8 changed files with 50 additions and 0 deletions

View file

@ -144,6 +144,8 @@
seer = naersk-lib.buildPackage {
src = self;
doCheck = true;
passthru = {
inherit my-rust;
};

View file

@ -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::<Bitboard>() == std::mem::size_of::<u64>());
impl Default for Bitboard {
fn default() -> Self {
Self::EMPTY

View file

@ -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::<Option<File>>() == std::mem::size_of::<File>());
#[cfg(test)]
mod test {
use super::*;

View file

@ -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::<Option<Rank>>() == std::mem::size_of::<Rank>());
#[cfg(test)]
mod test {
use super::*;

View file

@ -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<Bitboard> for Square {
}
}
// Ensure that niche-optimization is in effect.
static_assert!(std::mem::size_of::<Option<Square>>() == std::mem::size_of::<Square>());
#[cfg(test)]
mod test {
use super::*;

View file

@ -1 +1,2 @@
pub mod board;
pub mod utils;

2
src/utils/mod.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod static_assert;
pub use static_assert::*;

View file

@ -0,0 +1,28 @@
//! Static assert.
/// Assert a condition at compile-time.
///
/// See [RFC 2790] for potential addition into Rust itself.
///
/// [RFC 2790]: https://github.com/rust-lang/rfcs/issues/2790
///
/// # Examples
///
/// ```
/// use seer::utils::static_assert;
///
/// static_assert!(42 > 0);
/// ```
#[macro_export]
macro_rules! static_assert {
($condition:expr) => {
// Based on the latest one in `rustc`'s one before it was [removed].
//
// [removed]: https://github.com/rust-lang/rust/commit/c2dad1c6b9f9636198d7c561b47a2974f5103f6d
#[allow(dead_code)]
const _: () = [()][!($condition) as usize];
};
}
// I want it namespaced, even though it is exported to the root of the crate by `#[macro_export]`.
pub use static_assert;