From 2300b285ef6a12a8ebfbbce0439be95465714ba0 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Sat, 6 Jul 2024 13:56:19 +1000 Subject: [PATCH 1/3] units: Remove compile time pointer width check The `units` crate does not contain consensus logic and since our requirement to only support 32-bit and 64-bit machines is due to consensus logic we do not need to enforce the `target_pointer_width` in the `units` crate. Remove the compile time check on pointer width from the `units` crate. --- units/src/lib.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/units/src/lib.rs b/units/src/lib.rs index abd1875f8..addedf8d5 100644 --- a/units/src/lib.rs +++ b/units/src/lib.rs @@ -14,13 +14,6 @@ #![allow(clippy::needless_borrows_for_generic_args)] // https://github.com/rust-lang/rust-clippy/issues/12454 #![no_std] -// Disable 16-bit support at least for now as we can't guarantee it yet. -#[cfg(target_pointer_width = "16")] -compile_error!( - "rust-bitcoin currently only supports architectures with pointers wider than 16 bits, let us - know if you want 16-bit support. Note that we do NOT guarantee that we will implement it!" -); - #[cfg(feature = "alloc")] extern crate alloc; From 49a6acc1a0129b9403f44d20880e44624def21ec Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Sat, 6 Jul 2024 14:37:56 +1000 Subject: [PATCH 2/3] internals: Remove double parenthesis in const_assert The current `const_assert` macro is unused in the code base. We would like to use it differently to how it was initially designed, remove the parenthesis so it can be called directly in a module. --- internals/src/macros.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internals/src/macros.rs b/internals/src/macros.rs index 6b9aa6aaf..9a4b57238 100644 --- a/internals/src/macros.rs +++ b/internals/src/macros.rs @@ -139,9 +139,9 @@ macro_rules! debug_from_display { /// Asserts a boolean expression at compile time. #[macro_export] macro_rules! const_assert { - ($x:expr) => {{ + ($x:expr) => { const _: [(); 0 - !$x as usize] = []; - }}; + }; } /// Derives `From` for the given type. From c427d8b21388951718f21f00d9f02da22d9a62f7 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Sat, 6 Jul 2024 13:58:57 +1000 Subject: [PATCH 3/3] bitcoin: Compile time assert on index size Currently we enforce that our code only runs on machines with a certain pointer width (32 or 64 by failing to compile if pointer size width is 16). One of the underlying reasons is because of requirements in consensus code in Bitcoin Core which requires containers with more than 2^16 (65536) items [0]. We can better express our requirements by asserting on Rust's index size (the `usize` type). As a side benefit, there is active work [1] to make Rust support architectures where pointer width != idex size. With this patch applied `rust-bitcoin` will function correctly even if that work progresses. - [0] https://github.com/rust-bitcoin/rust-bitcoin/pull/2929#discussion_r1659399813 - [1] https://github.com/rust-lang/rust/issues/65473 --- bitcoin/src/lib.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/bitcoin/src/lib.rs b/bitcoin/src/lib.rs index e5e25f53f..95a7eb84b 100644 --- a/bitcoin/src/lib.rs +++ b/bitcoin/src/lib.rs @@ -37,12 +37,19 @@ #![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 -// Disable 16-bit support at least for now as we can't guarantee it yet. -#[cfg(target_pointer_width = "16")] -compile_error!( - "rust-bitcoin currently only supports architectures with pointers wider than 16 bits, let us - know if you want 16-bit support. Note that we do NOT guarantee that we will implement it!" -); +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) +// entries in them so we cannot support consensus logic on machines that only have 16-bit memory +// addresses. +// +// We specifically do not use `target_pointer_width` because of the possibility that pointer width +// does not equal index size. +// +// ref: https://github.com/rust-bitcoin/rust-bitcoin/pull/2929#discussion_r1661848565 +internals::const_assert!(mem::size_of::() >= 4); #[cfg(bench)] extern crate test;