Skip to content

Commit

Permalink
impl Arbitrary for fundamental types
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Sep 15, 2024
1 parent 6f07cd5 commit 1056edd
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ alloc = []
std = ["alloc", "btoi/std", "nohash-hasher?/std"]
variant = []
nohash-hasher = ["dep:nohash-hasher"]
arbitrary = ["dep:arbitrary"]

[[bench]]
name = "benches"
Expand All @@ -30,6 +31,7 @@ bitflags = "2.0.0"
btoi = { version = "0.4", default-features = false }
arrayvec = { version = "0.7", default-features = false }
nohash-hasher = { version = "0.2", default-features = false, optional = true }
arbitrary = { version = "1.3.2", optional = true, features = ["derive"] }

[dev-dependencies]
csv = "1.3"
Expand Down
1 change: 1 addition & 0 deletions src/bitboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::square::{File, Rank, Square};
/// // . 1 . . . 1 . .
/// ```
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct Bitboard(pub u64);

impl Bitboard {
Expand Down
2 changes: 2 additions & 0 deletions src/castling_side.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{

/// `KingSide` (O-O) or `QueenSide` (O-O-O).
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum CastlingSide {
KingSide,
QueenSide,
Expand Down Expand Up @@ -101,6 +102,7 @@ impl ops::BitXorAssign<bool> for CastlingSide {

/// Container with values for each [`CastlingSide`].
#[derive(Copy, Clone, Default, Eq, PartialEq, Debug, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[repr(C)]
pub struct ByCastlingSide<T> {
pub king_side: T,
Expand Down
2 changes: 2 additions & 0 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
/// `White` or `Black`.
#[allow(missing_docs)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum Color {
Black = 0,
White = 1,
Expand Down Expand Up @@ -213,6 +214,7 @@ try_color_from_int_impl! { u8 i8 u16 i16 u32 i32 u64 i64 u128 i128 usize isize }

/// Container with values for each [`Color`].
#[derive(Copy, Clone, Default, Eq, PartialEq, Debug, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct ByColor<T> {
pub black: T,
pub white: T,
Expand Down
2 changes: 2 additions & 0 deletions src/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{color::Color, types::Piece, util::out_of_range_error};
/// ```
#[allow(missing_docs)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum Role {
Pawn = 1,
Knight = 2,
Expand Down Expand Up @@ -161,6 +162,7 @@ try_role_from_int_impl! { u8 i8 u16 i16 u32 i32 u64 i64 u128 i128 usize isize }

/// Container with values for each [`Role`].
#[derive(Copy, Clone, Default, Eq, PartialEq, Debug, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[repr(C)]
pub struct ByRole<T> {
pub pawn: T,
Expand Down
3 changes: 3 additions & 0 deletions src/square.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ macro_rules! try_from_int_impl {
/// A file of the chessboard.
#[allow(missing_docs)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[repr(u8)]
pub enum File {
A = 0,
Expand Down Expand Up @@ -151,6 +152,7 @@ try_from_int_impl! { File, 0, 8, u8 i8 u16 i16 u32 i32 u64 i64 u128 i128 usize i
/// A rank of the chessboard.
#[allow(missing_docs)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[repr(u8)]
pub enum Rank {
First = 0,
Expand Down Expand Up @@ -281,6 +283,7 @@ impl std::error::Error for ParseSquareError {}
#[rustfmt::skip]
#[allow(missing_docs)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[repr(u8)]
pub enum Square {
A1 = 0, B1, C1, D1, E1, F1, G1, H1,
Expand Down
1 change: 1 addition & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
/// A piece with [`Color`] and [`Role`].
#[allow(missing_docs)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct Piece {
pub color: Color,
pub role: Role,
Expand Down
1 change: 1 addition & 0 deletions src/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::{

/// Discriminant of [`VariantPosition`].
#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy, Default)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum Variant {
/// See [`Chess`].
#[default]
Expand Down
3 changes: 2 additions & 1 deletion src/zobrist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ pub trait ZobristValue: BitXorAssign + Default + Copy {

macro_rules! zobrist_value_impl {
($t:ident, $proxy:ty, $bits:literal) => {
#[derive(Default, Copy, Clone, Eq)]
#[doc = "A [`ZobristValue`] with "]
#[doc = stringify!($bits)]
#[doc = " bits."]
#[derive(Default, Copy, Clone, Eq)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct $t(pub $proxy);

impl PartialEq for $t {
Expand Down

0 comments on commit 1056edd

Please sign in to comment.