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
- Parent 1: b5f8d57e2240
There are many coding harnesses - but this one is fast
7453b1336c1bf70c767a3d089d2e44a92d93847a
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
+35 / −18 text lines
+35 / −18 text lines
const CRC32_TABLE: [u32; 256] = { let mut table = [0u32; 256]; let mut i: u32 = 0; while i < 256 { let mut value = i; let mut bit = 0; while bit < 8 { value = if value & 1 == 1 { 0xEDB8_8320 ^ (value >> 1) } else { value >> 1 }; bit += 1; } table[i as usize] = value; i += 1;/// Recursion, not loops: a mutated step in a `const` loop makes rustc spin/// forever under capped lints; a mutated recursion fails to compile.const CRC32_TABLE: [u32; 256] = crc32_table([0; 256], 0);#[expect( clippy::large_types_passed_by_value, reason = "const evaluation threads the table through the recursion by value")]const fn crc32_table(table: [u32; 256], row: u32) -> [u32; 256] { if row == 16 { return table; crc32_table(crc32_row(table, row * 16, 0), row + 1)}/// Sixteen entries per row keep const-eval recursion well under its frame cap.const fn crc32_row(mut table: [u32; 256], start: u32, offset: u32) -> [u32; 256] { if offset == 16 { return table; } let index = start + offset; table[index as usize] = crc32_entry(index, 0); crc32_row(table, start, offset + 1)}const fn crc32_entry(value: u32, bit: u32) -> u32 { if bit == 8 { return value; } let next = if value & 1 == 1 { 0xEDB8_8320 ^ (value >> 1) } else { value >> 1 }; crc32_entry(next, bit + 1)}