[board, core, bitboard] Clean up casts between Rank, File and BitBoard
Let BitBoard::rank and BitBoard::file take a Rank and File directly, instead of a u8 by reference. And then make the Rank/File::as_index const and return a value rather than a reference. All this allows you to convert between Rank, File, and BitBoard at compile tile (i.e. as a const method) rather than needing to do runtime stuff.
This commit is contained in:
		
							parent
							
								
									588f049290
								
							
						
					
					
						commit
						3684e9b425
					
				
					 3 changed files with 24 additions and 25 deletions
				
			
		| 
						 | 
				
			
			@ -58,16 +58,12 @@ impl BitBoard {
 | 
			
		|||
        BitBoard(bits)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // TODO: Is &u8 really necessary here?
 | 
			
		||||
    pub fn rank(rank: &u8) -> BitBoard {
 | 
			
		||||
        debug_assert!(*rank < 8);
 | 
			
		||||
        library::RANKS[*rank as usize]
 | 
			
		||||
    pub const fn rank(rank: Rank) -> BitBoard {
 | 
			
		||||
        library::RANKS[rank.as_index()]
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // TODO: Is &u8 really necessary here?
 | 
			
		||||
    pub fn file(file: &u8) -> BitBoard {
 | 
			
		||||
        debug_assert!(*file < 8);
 | 
			
		||||
        library::FILES[*file as usize]
 | 
			
		||||
    pub const fn file(file: File) -> BitBoard {
 | 
			
		||||
        library::FILES[file.as_index()]
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn ray(sq: Square, dir: Direction) -> BitBoard {
 | 
			
		||||
| 
						 | 
				
			
			@ -313,7 +309,7 @@ impl From<BitBoard> for u64 {
 | 
			
		|||
 | 
			
		||||
impl From<File> for BitBoard {
 | 
			
		||||
    fn from(value: File) -> Self {
 | 
			
		||||
        library::FILES[*value.as_index() as usize]
 | 
			
		||||
        library::FILES[value.as_index()]
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -325,7 +321,7 @@ impl From<Option<Square>> for BitBoard {
 | 
			
		|||
 | 
			
		||||
impl From<Rank> for BitBoard {
 | 
			
		||||
    fn from(value: Rank) -> Self {
 | 
			
		||||
        library::FILES[*value.as_index() as usize]
 | 
			
		||||
        library::FILES[value.as_index()]
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -466,21 +462,24 @@ mod tests {
 | 
			
		|||
    #[test]
 | 
			
		||||
    #[ignore]
 | 
			
		||||
    fn display_and_debug() {
 | 
			
		||||
        let bb = BitBoard::file(&0) | BitBoard::file(&3) | BitBoard::rank(&7) | BitBoard::rank(&4);
 | 
			
		||||
        let bb = BitBoard::file(File::A)
 | 
			
		||||
            | BitBoard::file(File::D)
 | 
			
		||||
            | BitBoard::rank(Rank::FIVE)
 | 
			
		||||
            | BitBoard::rank(Rank::EIGHT);
 | 
			
		||||
        println!("{}", &bb);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    #[allow(clippy::unreadable_literal)]
 | 
			
		||||
    fn rank() {
 | 
			
		||||
        assert_eq!(BitBoard::rank(&0).0, 0xFF, "Rank 1");
 | 
			
		||||
        assert_eq!(BitBoard::rank(&1).0, 0xFF00, "Rank 2");
 | 
			
		||||
        assert_eq!(BitBoard::rank(&2).0, 0xFF0000, "Rank 3");
 | 
			
		||||
        assert_eq!(BitBoard::rank(&3).0, 0xFF000000, "Rank 4");
 | 
			
		||||
        assert_eq!(BitBoard::rank(&4).0, 0xFF00000000, "Rank 5");
 | 
			
		||||
        assert_eq!(BitBoard::rank(&5).0, 0xFF0000000000, "Rank 6");
 | 
			
		||||
        assert_eq!(BitBoard::rank(&6).0, 0xFF000000000000, "Rank 7");
 | 
			
		||||
        assert_eq!(BitBoard::rank(&7).0, 0xFF00000000000000, "Rank 8");
 | 
			
		||||
        assert_eq!(BitBoard::rank(Rank::ONE).0, 0xFF, "Rank 1");
 | 
			
		||||
        assert_eq!(BitBoard::rank(Rank::TWO).0, 0xFF00, "Rank 2");
 | 
			
		||||
        assert_eq!(BitBoard::rank(Rank::THREE).0, 0xFF0000, "Rank 3");
 | 
			
		||||
        assert_eq!(BitBoard::rank(Rank::FOUR).0, 0xFF000000, "Rank 4");
 | 
			
		||||
        assert_eq!(BitBoard::rank(Rank::FIVE).0, 0xFF00000000, "Rank 5");
 | 
			
		||||
        assert_eq!(BitBoard::rank(Rank::SIX).0, 0xFF0000000000, "Rank 6");
 | 
			
		||||
        assert_eq!(BitBoard::rank(Rank::SEVEN).0, 0xFF000000000000, "Rank 7");
 | 
			
		||||
        assert_eq!(BitBoard::rank(Rank::EIGHT).0, 0xFF00000000000000, "Rank 8");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -63,10 +63,10 @@ fn pawn_pushes(pawn: BitBoard, color: Color, occupancy: BitBoard) -> BitBoard {
 | 
			
		|||
 | 
			
		||||
    match color {
 | 
			
		||||
        Color::White => {
 | 
			
		||||
            let second_rank = BitBoard::rank(&Rank::TWO.into());
 | 
			
		||||
            const SECOND_RANK: BitBoard = BitBoard::rank(Rank::TWO);
 | 
			
		||||
 | 
			
		||||
            let mut pushes = pawn.shift_north_one() & vacancy;
 | 
			
		||||
            if !(pawn & second_rank).is_empty() {
 | 
			
		||||
            if !(pawn & SECOND_RANK).is_empty() {
 | 
			
		||||
                // Double push
 | 
			
		||||
                pushes = pushes | (pushes.shift_north_one() & vacancy);
 | 
			
		||||
            }
 | 
			
		||||
| 
						 | 
				
			
			@ -74,10 +74,10 @@ fn pawn_pushes(pawn: BitBoard, color: Color, occupancy: BitBoard) -> BitBoard {
 | 
			
		|||
            pushes
 | 
			
		||||
        }
 | 
			
		||||
        Color::Black => {
 | 
			
		||||
            let seventh_rank = BitBoard::rank(&Rank::SEVEN.into());
 | 
			
		||||
            const SEVENTH_RANK: BitBoard = BitBoard::rank(Rank::SEVEN);
 | 
			
		||||
 | 
			
		||||
            let mut pushes = pawn.shift_south_one() & vacancy;
 | 
			
		||||
            if !(pawn & seventh_rank).is_empty() {
 | 
			
		||||
            if !(pawn & SEVENTH_RANK).is_empty() {
 | 
			
		||||
                // Double push
 | 
			
		||||
                pushes = pushes | (pushes.shift_south_one() & vacancy);
 | 
			
		||||
            }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -88,8 +88,8 @@ macro_rules! range_bound_struct {
 | 
			
		|||
            }
 | 
			
		||||
 | 
			
		||||
            #[must_use]
 | 
			
		||||
            $vis fn as_index(&self) -> &$repr {
 | 
			
		||||
                &self.0
 | 
			
		||||
            $vis const fn as_index(&self) -> usize {
 | 
			
		||||
                self.0 as usize
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            $vis fn iter(&self) -> impl Iterator<Item = Self> {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue