Skip to content

Commit

Permalink
Merge rust-bitcoin/rust-bitcoin#1000: Replace runtime size check with…
Browse files Browse the repository at this point in the history
… compile time check

7a3bb7d Replace runtime size check with compile time check (Tobin C. Harding)

Pull request description:

  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.

  ## Note

  This PR is the first patch from a [recently closed PR](rust-bitcoin/rust-bitcoin#953). Props to @elichai for the macro idea in the review of that PR.

ACKs for top commit:
  Kixunil:
    ACK 7a3bb7d
  apoelstra:
    ACK 7a3bb7d

Tree-SHA512: cfd4dcf6c66e06796cab6dc49445f0f8c5d4e686893a17735420dccedd75ad7c632d240a5ab92ee47ce459b799daeaf3fdf9c6b77c1b81b09e87197a9f86c5ba
  • Loading branch information
apoelstra committed Jun 20, 2022
2 parents 8525a4e + d75a8d2 commit 6fbe648
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
Expand Up @@ -242,7 +242,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
Expand Up @@ -593,3 +593,12 @@ macro_rules! write_err {
}
}
}

/// 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
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 6fbe648

Please sign in to comment.