repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by bugabinga

fix(core): crc table by const recursion; a mutated const loop can no longer hang rustc under capped lints

7453b1336c1bf70c767a3d089d2e44a92d93847a

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (1)

+35 / −18 text lines

Diff layout

smith-core/src/frame.rs

Modified

+35 / −18 text lines

381381 !crc
384const CRC32_TABLE: [u32; 256] = {
385 let mut table = [0u32; 256];
386 let mut i: u32 = 0;
387 while i < 256 {
388 let mut value = i;
389 let mut bit = 0;
390 while bit < 8 {
391 value = if value & 1 == 1 {
392 0xEDB8_8320 ^ (value >> 1)
393 } else {
394 value >> 1
395 };
396 bit += 1;
397 }
398 table[i as usize] = value;
399 i += 1;
384/// Recursion, not loops: a mutated step in a `const` loop makes rustc spin
385/// forever under capped lints; a mutated recursion fails to compile.
386const CRC32_TABLE: [u32; 256] = crc32_table([0; 256], 0);
388#[expect(
389 clippy::large_types_passed_by_value,
390 reason = "const evaluation threads the table through the recursion by value"
391)]
392const fn crc32_table(table: [u32; 256], row: u32) -> [u32; 256] {
393 if row == 16 {
394 return table;
400395 }
401 table
402};
396 crc32_table(crc32_row(table, row * 16, 0), row + 1)
397}
399/// Sixteen entries per row keep const-eval recursion well under its frame cap.
400const fn crc32_row(mut table: [u32; 256], start: u32, offset: u32) -> [u32; 256] {
401 if offset == 16 {
402 return table;
403 }
404 let index = start + offset;
405 table[index as usize] = crc32_entry(index, 0);
406 crc32_row(table, start, offset + 1)
407}
409const fn crc32_entry(value: u32, bit: u32) -> u32 {
410 if bit == 8 {
411 return value;
412 }
413 let next = if value & 1 == 1 {
414 0xEDB8_8320 ^ (value >> 1)
415 } else {
416 value >> 1
417 };
418 crc32_entry(next, bit + 1)
419}
404421/// A `Vec<u8>` writer that stops accepting bytes beyond a limit,
405422/// bounding allocation during encoding.