diff --git a/generator/src/util.rs b/generator/src/util.rs index 8fee296..0a5f55b 100644 --- a/generator/src/util.rs +++ b/generator/src/util.rs @@ -1,14 +1,15 @@ -pub fn smallest_index(n: usize) -> usize { - for &x in [8, 16, 32].iter() { - if n < (1 << x) { - return x / 8; +/// Figure out whether we need `u8` (1 byte), `u16` (2 bytes) or `u32` (4 bytes) to store all +/// numbers. Returns the number of bytes +pub fn smallest_type>(x: I) -> usize { + let n = x.max().unwrap_or(0); + for (max, bytes) in [(u8::MAX as u32, 1), (u16::MAX as u32, 2)] { + if n <= max { + return bytes; } } - 64 -} -pub fn smallest_type>(x: I) -> usize { - smallest_index(x.max().unwrap_or(0) as usize) + 4 } + pub fn smallest_u>(x: I) -> String { format!("u{}", 8 * smallest_type(x)) }