Skip to content

Commit

Permalink
bits: Make as_usize_bits() a const fn and test it a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Jan 19, 2025
1 parent b99b471 commit 4409154
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl BitLength<usize> {
feature = "alloc"
))]
#[inline]
pub fn as_usize_bytes_rounded_up(&self) -> usize {
pub const fn as_usize_bytes_rounded_up(&self) -> usize {
// Equivalent to (self.0 + 7) / 8, except with no potential for
// overflow and without branches.

Expand Down Expand Up @@ -130,3 +130,10 @@ impl TryFrom<BitLength<u64>> for BitLength<core::num::NonZeroU64> {
value.try_into().map(BitLength)
}
}

const _TEST_AS_USIZE_BYTES_ROUNDED_UP_EVEN: () =
assert!(BitLength::from_bits(8192).as_usize_bytes_rounded_up() == 8192 / 8);
const _TEST_AS_USIZE_BYTES_ROUNDED_UP_ONE_BIT_HIGH: () =
assert!(BitLength::from_bits(8192 + 1).as_usize_bytes_rounded_up() == (8192 / 8) + 1);
const _TEST_AS_USIZE_BYTES_ROUNDED_UP_SEVEN_BITS_HIGH: () =
assert!(BitLength::from_bits(8192 + 7).as_usize_bytes_rounded_up() == (8192 / 8) + 1);

0 comments on commit 4409154

Please sign in to comment.