Skip to content

Commit

Permalink
Fix off-by-one in varint size calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Palladinium committed Aug 22, 2023
1 parent 672f0ee commit 67a7e74
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/varint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ pub const fn varint_max<T: Sized>() -> usize {
// How many data bits do we need for this type?
let bits = core::mem::size_of::<T>() * BITS_PER_BYTE;

// We add (BITS_PER_BYTE - 1), to ensure any integer divisions
// We add (BITS_PER_VARINT_BYTE - 1), to ensure any integer divisions
// with a remainder will always add exactly one full byte, but
// an evenly divided number of bits will be the same
let roundup_bits = bits + (BITS_PER_BYTE - 1);
let roundup_bits = bits + (BITS_PER_VARINT_BYTE - 1);

// Apply division, using normal "round down" integer division
roundup_bits / BITS_PER_VARINT_BYTE
Expand Down

0 comments on commit 67a7e74

Please sign in to comment.