Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

chess - Create bitfield from list of enums in Rust using modular-bitfield

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...