Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of the From trait for the BitBoard struct starting from a slice of cells #1

Open
fpiantini opened this issue Oct 31, 2021 · 0 comments

Comments

@fpiantini
Copy link
Member

We wants to implement the from trait for the BitBoard structure starting from Vec<Cell> to be able to write code like this:

let bb1 = BitBoard::from([Cell::D1, Cell::D2]);
let bb2 = BitBoard::from(&[Cell::A1, Cell::H8]);

A possible implementaion is for example the following one (info on this reddit post):

/// From trait for the BitBoard struct starting from a slice of [Cell]s.
///
/// Converts a slice of cells to a [BitBoard] with all the cells of the slice set to busy (1) status.
///
/// # Arguments
///
/// * `cells` - A [Cell] slice with the cells to be set on the [BitBoard]
/// # Example
///```
/// # use abbadingo::bitboard::*;
/// # use abbadingo::bbdefines::*;
/// let mut bb = BitBoard::new();
/// bb.set_file(File::FileD);
/// assert_eq!(bb, BitBoard::from([Cell::D1, Cell::D2, Cell::D3, Cell::D4,
///                                Cell::D5, Cell::D6, Cell::D7, Cell::D8]));
///```
impl<'a, T: AsRef<[Cell]>> From<T> for BitBoard {
    fn from(cells: T) -> Self {
        let mut bb = BitBoard::new();
        for c in cells.as_ref().to_vec() {
            bb.set_cell(c);
        }
        bb
    }
}

The problem is that the above code is not compatible with the From<BitBoardState> trait and with other hypotetical From traits like for example From<Cell>. The issue seems due to the problem of "Trait Specialization"

See also: https://users.rust-lang.org/t/generic-trait-and-specialization/33745

fpiantini added a commit that referenced this issue Oct 31, 2021
…rom<BitBoardState> trait necessary for Army. See issue #1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant