From 4933959533384ef53fb394d1a2fabb6c023d8b94 Mon Sep 17 00:00:00 2001 From: Elizabeth Myers Date: Fri, 15 Mar 2024 01:26:39 -0700 Subject: [PATCH] Implement assert for bits trait Signed-off-by: Elizabeth Myers --- src/util/bits.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util/bits.rs b/src/util/bits.rs index 877cf1a..f485dc3 100644 --- a/src/util/bits.rs +++ b/src/util/bits.rs @@ -16,12 +16,12 @@ use std::mem::size_of; use num::PrimInt; -// NOTE: do not implement this type for size_of:: > u32::MAX -// (size_of::() regrettably is not a compile-time expression, so we can't do a static assert) +// NOTE: do not implement this trait for size_of:: > u32::MAX pub trait IntBitUtil: Sized + PrimInt { // Count the minimum number of bits required to represent this number #[allow(clippy::cast_possible_truncation)] fn bit_length(&self) -> u32 { + assert!(size_of::() <= u32::MAX as usize); (size_of::() - (self.leading_zeros() as usize)) as u32 } }