Merge rust-bitcoin/rust-bitcoin#3215: Improve const_assert
3c7c8c44b6
Improve const_assert (Tobin C. Harding) Pull request description: Now that we can panic in const context we can improve the `const_assert` macro by adding a message string. Original idea by Kix: https://github.com/rust-bitcoin/rust-bitcoin/pull/2972#discussion_r1726328228 ACKs for top commit: Kixunil: ACK3c7c8c44b6
in the sense that it does what it's supposed to with the only tiny issue being that the `bool` looks weird but not broken in any other way I can think of. apoelstra: ACK3c7c8c44b6
successfully ran local tests Tree-SHA512: 5ff721c0056f87d42c934818da6f780cd945f235291eb4b044752d67405a74f992d7f85853fec129e794ec3fcda1f319cc40daabc6a349d21bbdc977640d2572
This commit is contained in:
commit
753961fdb8
|
@ -37,8 +37,6 @@
|
|||
#![allow(clippy::manual_range_contains)] // More readable than clippy's format.
|
||||
#![allow(clippy::needless_borrows_for_generic_args)] // https://github.com/rust-lang/rust-clippy/issues/12454
|
||||
|
||||
use core::mem;
|
||||
|
||||
// We only support machines with index size of 4 bytes or more.
|
||||
//
|
||||
// Bitcoin consensus code relies on being able to have containers with more than 65536 (2^16)
|
||||
|
@ -49,7 +47,10 @@ use core::mem;
|
|||
// does not equal index size.
|
||||
//
|
||||
// ref: https://github.com/rust-bitcoin/rust-bitcoin/pull/2929#discussion_r1661848565
|
||||
internals::const_assert!(mem::size_of::<usize>() >= 4);
|
||||
internals::const_assert!(
|
||||
core::mem::size_of::<usize>() >= 4;
|
||||
"platforms that have usize less than 32 bits are not supported"
|
||||
);
|
||||
|
||||
#[cfg(bench)]
|
||||
extern crate test;
|
||||
|
|
|
@ -63,7 +63,10 @@ impl_to_u64!(u8, u16, u32, u64);
|
|||
|
||||
impl ToU64 for usize {
|
||||
fn to_u64(self) -> u64 {
|
||||
crate::const_assert!(core::mem::size_of::<usize>() <= 8);
|
||||
crate::const_assert!(
|
||||
core::mem::size_of::<usize>() <= 8;
|
||||
"platforms that have usize larger than 64 bits are not supported"
|
||||
);
|
||||
self as u64
|
||||
}
|
||||
}
|
||||
|
|
|
@ -139,10 +139,16 @@ macro_rules! debug_from_display {
|
|||
/// Asserts a boolean expression at compile time.
|
||||
#[macro_export]
|
||||
macro_rules! const_assert {
|
||||
($x:expr) => {
|
||||
const _: [(); 0 - !$x as usize] = [];
|
||||
($x:expr $(; $message:expr)?) => {
|
||||
const _: bool = {
|
||||
if !$x {
|
||||
// We can't use formatting in const, only concating literals.
|
||||
panic!(concat!("assertion ", stringify!($x), " failed" $(, ": ", $message)?))
|
||||
}
|
||||
$x
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// Derives `From<core::convert::Infallible>` for the given type.
|
||||
///
|
||||
|
|
Loading…
Reference in New Issue