Skip to content

Commit

Permalink
chore: add ColorType and a macro for making it
Browse files Browse the repository at this point in the history
  • Loading branch information
raklaptudirm committed Dec 25, 2024
1 parent 1a18b20 commit e8f477d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 44 deletions.
18 changes: 3 additions & 15 deletions games/src/ataxx/piece.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::ops;

use crate::interface::representable_type;
use crate::interface::ColoredPieceType;
use crate::interface::RepresentableType;
use crate::interface::{color_type, representable_type};

representable_type!(
color_type!(
/// Color represents all the possible colors that an ataxx piece can have,
/// specifically, Black and White.
enum Color: u8 { Black "x", White "o", }
enum Color { Black "x", White "o", }
);

impl ops::Not for Color {
type Output = Color;

/// not implements the not unary operator (!) which switches the current Color
/// to its opposite, i.e. [`Color::Black`] to [`Color::White`] and vice versa.
fn not(self) -> Self::Output {
unsafe { Color::unsafe_from(self as usize ^ 1) }
}
}

representable_type!(
/// Piece represents the types of pieces in ataxx, namely Piece and Block.
enum Piece: u8 { Piece "x", Block "■", }
Expand Down
18 changes: 3 additions & 15 deletions games/src/chess/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::ops;

use crate::interface::representable_type;
use crate::interface::ColoredPieceType;
use crate::interface::RepresentableType;
use crate::interface::{color_type, representable_type};

representable_type!(
color_type!(
/// Color represents all the possible colors that an ataxx piece can have,
/// specifically, Black and White.
enum Color: u8 { White "w", Black "b", }
enum Color { White "w", Black "b", }
);

impl ops::Not for Color {
type Output = Color;

/// not implements the not unary operator (!) which switches the current Color
/// to its opposite, i.e. [`Color::Black`] to [`Color::White`] and vice versa.
fn not(self) -> Self::Output {
unsafe { Color::unsafe_from(self as usize ^ 1) }
}
}

representable_type!(
/// Piece represents the types of pieces in ataxx, namely Piece and Block.
enum Piece: u8 {
Expand Down
31 changes: 31 additions & 0 deletions games/src/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,37 @@ pub enum TypeParseError {
RangeError(String),
}

macro_rules! color_type {
($(#[doc = $doc:expr])* enum $type:tt {
$first:tt $first_repr:expr,
$second:tt $second_repr:expr,
}) => {


crate::interface::representable_type! {
$(#[doc = $doc])*
enum $type: u8 {
$first $first_repr, $second $second_repr,
}
}

impl std::ops::Not for $type {
type Output = Self;

fn not(self) -> Self::Output {
unsafe { Self::unsafe_from(self as usize ^ 1) }
}
}

impl crate::interface::ColorType for $type {
fn first() -> Self {
Self::$first
}
}
}
}
pub(crate) use color_type;

macro_rules! representable_type {
($(#[doc = $doc:expr])* enum $type:tt: $base:tt {
$($variant:tt $repr:expr,)*
Expand Down
6 changes: 5 additions & 1 deletion games/src/interface/piece.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::RepresentableType;
pub trait ColoredPieceType: RepresentableType<u8>
where
Self::Piece: RepresentableType<u8>,
Self::Color: RepresentableType<u8> + Not,
Self::Color: ColorType,
{
/// The type for the Piece of the ColoredPiece.
type Piece;
Expand All @@ -31,3 +31,7 @@ where
#[must_use]
fn color(self) -> Self::Color;
}

pub trait ColorType: RepresentableType<u8> + Not {
fn first() -> Self;
}
16 changes: 3 additions & 13 deletions games/src/isolation/piece.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,16 @@

use std::ops;

use crate::interface::representable_type;
use crate::interface::ColoredPieceType;
use crate::interface::RepresentableType;
use crate::interface::{color_type, representable_type};

representable_type!(
color_type!(
/// Color represents all the possible colors that an ataxx piece can have,
/// specifically, Black and White.
enum Color: u8 { White "w", Black "b", }
enum Color { White "w", Black "b", }
);

impl ops::Not for Color {
type Output = Color;

/// not implements the not unary operator (!) which switches the current Color
/// to its opposite, i.e. [`Color::Black`] to [`Color::White`] and vice versa.
fn not(self) -> Self::Output {
unsafe { Color::unsafe_from(self as usize ^ 1) }
}
}

representable_type!(
/// Piece represents the types of pieces in ataxx, namely Piece and Block.
enum Piece: u8 { Pawn "p", Tile "-", }
Expand Down

0 comments on commit e8f477d

Please sign in to comment.