[board] Implement an infix_op! macro for generating BitBoard operator traits
This commit is contained in:
parent
50a6be2f72
commit
51a265172b
1 changed files with 16 additions and 31 deletions
|
@ -95,23 +95,26 @@ impl fmt::Debug for BitBoard {
|
|||
}
|
||||
}
|
||||
|
||||
impl BitAnd for BitBoard {
|
||||
type Output = BitBoard;
|
||||
macro_rules! infix_op {
|
||||
($trait_type:ident, $func_name:ident, $left_type:ty, $right_type:ty, $op:tt) => {
|
||||
impl $trait_type<$right_type> for $left_type {
|
||||
type Output = BitBoard;
|
||||
|
||||
#[inline]
|
||||
fn bitand(self, rhs: Self) -> Self {
|
||||
BitBoard(self.0 & rhs.0)
|
||||
}
|
||||
#[inline]
|
||||
fn $func_name(self, rhs: $right_type) -> Self::Output {
|
||||
BitBoard(self.0 $op rhs.0)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl BitAnd<&BitBoard> for BitBoard {
|
||||
type Output = BitBoard;
|
||||
infix_op!(BitAnd, bitand, BitBoard, BitBoard, &);
|
||||
infix_op!(BitAnd, bitand, &BitBoard, BitBoard, &);
|
||||
infix_op!(BitAnd, bitand, BitBoard, &BitBoard, &);
|
||||
|
||||
#[inline]
|
||||
fn bitand(self, rhs: &Self) -> Self {
|
||||
BitBoard(self.0 & rhs.0)
|
||||
}
|
||||
}
|
||||
infix_op!(BitOr, bitor, BitBoard, BitBoard, |);
|
||||
infix_op!(BitOr, bitor, &BitBoard, BitBoard, |);
|
||||
infix_op!(BitOr, bitor, BitBoard, &BitBoard, |);
|
||||
|
||||
impl Not for BitBoard {
|
||||
type Output = BitBoard;
|
||||
|
@ -131,24 +134,6 @@ impl Not for &BitBoard {
|
|||
}
|
||||
}
|
||||
|
||||
impl BitOr for BitBoard {
|
||||
type Output = BitBoard;
|
||||
|
||||
#[inline]
|
||||
fn bitor(self, rhs: Self) -> Self {
|
||||
BitBoard(self.0 | rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl BitOr<&BitBoard> for BitBoard {
|
||||
type Output = BitBoard;
|
||||
|
||||
#[inline]
|
||||
fn bitor(self, rhs: &Self) -> Self {
|
||||
BitBoard(self.0 | rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue