Skip to content

Commit

Permalink
Replace runtime size check with compile time check
Browse files Browse the repository at this point in the history
Add a macro `const_assert` that uses some const declaration trickery to
trigger a compile time error if a boolean expression is false.

Replace runtime checks using `debug_assert_eq!` with the newly defined
`const_asert!` macro.
  • Loading branch information
tcharding committed May 24, 2022
1 parent 324fa0f commit 58eef54
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/consensus/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ macro_rules! decoder_fn {
($name:ident, $val_type:ty, $readfn:ident, $byte_len: expr) => {
#[inline]
fn $name(&mut self) -> Result<$val_type, Error> {
debug_assert_eq!(::core::mem::size_of::<$val_type>(), $byte_len); // size_of isn't a constfn in 1.22
const_assert!(::core::mem::size_of::<$val_type>() == $byte_len);
let mut val = [0; $byte_len];
self.read_exact(&mut val[..]).map_err(Error::Io)?;
Ok(endian::$readfn(&val))
Expand Down
9 changes: 9 additions & 0 deletions src/internal_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,12 @@ macro_rules! user_enum {
}
);
}

/// Asserts a boolean expression at compile time.
macro_rules! const_assert {
($x:expr) => {
{
const _: [(); 0 - !$x as usize] = [];
}
};
}
4 changes: 2 additions & 2 deletions src/util/endian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ macro_rules! define_be_to_array {
($name: ident, $type: ty, $byte_len: expr) => {
#[inline]
pub fn $name(val: $type) -> [u8; $byte_len] {
debug_assert_eq!(::core::mem::size_of::<$type>(), $byte_len); // size_of isn't a constfn in 1.22
const_assert!(::core::mem::size_of::<$type>() == $byte_len);
let mut res = [0; $byte_len];
for i in 0..$byte_len {
res[i] = ((val >> ($byte_len - i - 1)*8) & 0xff) as u8;
Expand All @@ -41,7 +41,7 @@ macro_rules! define_le_to_array {
($name: ident, $type: ty, $byte_len: expr) => {
#[inline]
pub fn $name(val: $type) -> [u8; $byte_len] {
debug_assert_eq!(::core::mem::size_of::<$type>(), $byte_len); // size_of isn't a constfn in 1.22
const_assert!(::core::mem::size_of::<$type>() == $byte_len);
let mut res = [0; $byte_len];
for i in 0..$byte_len {
res[i] = ((val >> i*8) & 0xff) as u8;
Expand Down

0 comments on commit 58eef54

Please sign in to comment.