#[derive(Debug)]
pub struct VarintUsize(pub usize);
pub type VarintBuf = [u8; VarintUsize::varint_usize_max()];
impl VarintUsize {
pub fn to_buf<'a, 'b>(&'a self, out: &'b mut VarintBuf) -> &'b mut [u8] {
let mut value = self.0;
for i in 0..Self::varint_usize_max() {
out[i] = (value & 0x7F) as u8;
value >>= 7;
if value != 0 {
out[i] |= 0x80;
} else {
return &mut out[..=i];
}
}
debug_assert_eq!(value, 0);
&mut out[..]
}
pub const fn new_buf() -> VarintBuf {
[0u8; Self::varint_usize_max()]
}
pub const fn varint_usize_max() -> usize {
const BITS_PER_BYTE: usize = 8;
const BITS_PER_VARINT_BYTE: usize = 7;
let bits = core::mem::size_of::<usize>() * BITS_PER_BYTE;
let roundup_bits = bits + (BITS_PER_BYTE - 1);
roundup_bits / BITS_PER_VARINT_BYTE
}
}