I'm new to Rust. As my first project I decided to create a little library that transfers chess notations (PGN and FEN) into as little of room as possible.
My current implementation is as follows:
mod data {
use modular_bitfield::{
BitfieldSpecifier,
bitfield,
};
#[derive(BitfieldSpecifier)]
pub enum Color {
White,
Black,
}
#[derive(BitfieldSpecifier)]
#[bits=3]
pub enum Rank {
Pawn,
Knight,
Bishop,
Rook,
Queen,
King,
None,
}
#[bitfield]
pub struct Piece {
rank: Rank,
color: Color
}
#[bitfield]
pub struct Board {
state: [[Piece; 8]; 8]
}
}
I'm having two problems
The first error it gives me is that bitfield is not specified for the type: [[Piece; 8]; 8]
The second is that piece struct has to be represented in at least 8 bits (at least that's what the error says). But because I have an array of N=8*8 of them, it will take up way too much room.
If I change Rank and Piece to:
#[derive(BitfieldSpecifier)]
#[bits=3]
pub enum Rank {
Pawn(Color),
Knight(Color),
Bishop(Color),
Rook(Color),
Queen(Color),
King(Color),
None,
}
I get the following error:
error[E0605]: non-primitive cast: `Rank` as `usize`
--> src/lib.rs:43:9
|
43 | None,
| ^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
Also if I give None a color, I still get the same error. If I delete None and hand an Option type to the array, I'm afraid it might go up in size, AND I still get the same error: its trait is not satisfied for [[Option; 8]; 8]
I also tried using tuples instead of structs, but that didn't help either
question from:
https://stackoverflow.com/questions/66061559/create-bitfield-from-list-of-enums-in-rust-using-modular-bitfield