From 808222eef1a2a489dab7556f185c6df13396093c Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 29 Dec 2023 15:37:38 -0800 Subject: [PATCH] [board] Clean up the formatting of BitBoards Use debug_tuple in the fmt::Debug implementation. Implement fmt::Binary, fmt::UpperHex, and fmt::LowerHex by delegating to u64's version. --- board/src/bitboard/bitboard.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/board/src/bitboard/bitboard.rs b/board/src/bitboard/bitboard.rs index 113b53c..746ad6f 100644 --- a/board/src/bitboard/bitboard.rs +++ b/board/src/bitboard/bitboard.rs @@ -47,10 +47,32 @@ impl BitBoard { } } +impl fmt::Binary for BitBoard { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // Delegate to u64's implementation of Binary. + fmt::Binary::fmt(&self.0, f) + } +} + +impl fmt::LowerHex for BitBoard { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // Delegate to u64's implementation of LowerHex. + fmt::LowerHex::fmt(&self.0, f) + } +} + +impl fmt::UpperHex for BitBoard { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // Delegate to u64's implementation of UpperHex. + fmt::UpperHex::fmt(&self.0, f) + } +} + impl fmt::Debug for BitBoard { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { - let bits = self.0; - write!(f, "BitBoard({bits:064b})") + f.debug_tuple("BitBoard") + .field(&format_args!("{:064b}", self.0)) + .finish() } }