jordanbray/chess

Board cannot be the key to a HashMap

Closed this issue · 4 comments

I want to use a board state (represented by the Board struct in this library) as a key to a HashMap. This is for a chess engine I am writing for fun.

std::collections::Hashmap requires that its key be Eq and Hash. Both should be doable, yet are not in this library. Checking the struct definition and cross-referencing with cargo doc --open in my project reveals that the following:

  • the only type which needs Eq and Hash as well is Bitboard
  • Color, and Square are already Hash and Eq
  • CastleRights has Hash, but no Eq (only PartialEq)

As Bitboard is just a wrapper around a u64, it seems as simple as adding Hash, Eq into the #[derive]. Once that is done, it is probably a similar matter for Board and CastleRights.

To-do List / Summary (in no particular order)

  • Hash for Bitboard
  • Eq for Bitboard
  • Hash for Board
  • Eq for CastleRights
  • Eq for Board

@jordanbray If I understand this correctly, Bitboard and CastleRights would just #[derive(Hash, Eq)], while Board would #[derive(Eq)] and have an impl Hash that just returns self.hash?

CacheTable has pretty strict requirements (I consider Copy "strict" as it means only "lightweight" types should be used) and I would be more comfortable using the Rust standard HashMap.

@jordanbray PR #45 ready for review.