Board: make string formatting stuff const

This commit is contained in:
Simon Gardling 2025-03-20 02:03:11 -04:00
parent c46b8ef8b8
commit 92b982cf35
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
3 changed files with 13 additions and 5 deletions

7
Cargo.lock generated
View File

@ -145,6 +145,12 @@ dependencies = [
"windows-sys", "windows-sys",
] ]
[[package]]
name = "const-str"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e991226a70654b49d34de5ed064885f0bef0348a8e70018b8ff1ac80aa984a2"
[[package]] [[package]]
name = "const_for" name = "const_for"
version = "0.1.5" version = "0.1.5"
@ -449,6 +455,7 @@ dependencies = [
"allocative", "allocative",
"arrayvec", "arrayvec",
"console", "console",
"const-str",
"const_for", "const_for",
"criterion", "criterion",
"crossbeam-channel", "crossbeam-channel",

View File

@ -25,6 +25,7 @@ debug = true
allocative = "0.3" allocative = "0.3"
arrayvec = "0.7" arrayvec = "0.7"
console = "0.15" console = "0.15"
const-str = "0.6"
const_for = "0.1" const_for = "0.1"
crossbeam-channel = "0.5" crossbeam-channel = "0.5"
either = "1.13" either = "1.13"

View File

@ -88,22 +88,22 @@ pub struct Board {
impl fmt::Display for Board { impl fmt::Display for Board {
#[allow(clippy::repeat_once)] // clippy gets mad about when PADDING == 1 #[allow(clippy::repeat_once)] // clippy gets mad about when PADDING == 1
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let horiz_sep_line = "-".repeat((Self::SIZE * 2 + 1) as usize); const HORIZ_SEP_LINE: &str = const_str::repeat!("-", (Board::SIZE * 2 + 1) as usize);
// basically calculates the # of digits BOARD_SIZE needs // basically calculates the # of digits BOARD_SIZE needs
const PADDING: usize = (Board::SIZE - 1).ilog10() as usize + 1; const PADDING: usize = (Board::SIZE - 1).ilog10() as usize + 1;
let space_padding = " ".repeat(PADDING); const SPACE_PADDING: &str = const_str::repeat!(" ", PADDING);
// Print numbers at top so the board can be read more easier // Print numbers at top so the board can be read more easier
write!(f, "{} ", space_padding)?; write!(f, "{} ", SPACE_PADDING)?;
for j in (0..Self::SIZE).rev() { for j in (0..Self::SIZE).rev() {
write!(f, "{:0PADDING$} ", j)?; write!(f, "{:0PADDING$} ", j)?;
} }
writeln!(f)?; writeln!(f)?;
for i in (0..Self::SIZE).rev() { for i in (0..Self::SIZE).rev() {
writeln!(f, "{}{}", space_padding, horiz_sep_line)?; writeln!(f, "{}{}", SPACE_PADDING, HORIZ_SEP_LINE)?;
write!(f, "{:0PADDING$}|", i)?; write!(f, "{:0PADDING$}|", i)?;
for j in (0..Self::SIZE).rev() { for j in (0..Self::SIZE).rev() {
@ -120,7 +120,7 @@ impl fmt::Display for Board {
} }
// put a line at the bottom of the board too // put a line at the bottom of the board too
writeln!(f, " {}", horiz_sep_line)?; writeln!(f, " {}", HORIZ_SEP_LINE)?;
// Print the current score // Print the current score
write!( write!(