Is there a Ser/De crate that represents the data in the smallest size possible?

81 points rnottaken

For instance, take a chess piece. There are 2 colours

enum PieceColor {
    Black,
    White,
}

That can be represented with one bit.

And there are 6 kinds of pieces (actually 8, because of castling moves)

enum PieceKind {
    Pawn,
    Bishop,
    Knight,
    Queen,
    UnmovedRook,
    Rook,
    UnmovedKing,
    King,
}

This can be represented with 3 bits.

A piece is then defined by

struct ChessPiece {
    color: PieceColor,
    kind: PieceKind,
}

Now on a board, we have either a piece or nothing. So we need another bit to represent an empty field. A board is 8 * 8 = 64 fields represented by:

struct ChessBoard {
    fields: [[Option<ChessPiece>; 8]; 8]
}

So the whole board could be represented in 64 * (1+3+1) = 320 bits = 40 bytes. Is there a Ser/De crate that can find this minimal solution?