Skip to content

Commit c427d8b

Browse files
committed
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] rust-bitcoin#2929 (comment) - [1] rust-lang/rust#65473
1 parent 49a6acc commit c427d8b

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

bitcoin/src/lib.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,19 @@
3737
#![allow(clippy::manual_range_contains)] // More readable than clippy's format.
3838
#![allow(clippy::needless_borrows_for_generic_args)] // https://github.com/rust-lang/rust-clippy/issues/12454
3939

40-
// Disable 16-bit support at least for now as we can't guarantee it yet.
41-
#[cfg(target_pointer_width = "16")]
42-
compile_error!(
43-
"rust-bitcoin currently only supports architectures with pointers wider than 16 bits, let us
44-
know if you want 16-bit support. Note that we do NOT guarantee that we will implement it!"
45-
);
40+
use core::mem;
41+
42+
// We only support machines with index size of 4 bytes or more.
43+
//
44+
// Bitcoin consensus code relies on being able to have containers with more than 65536 (2^16)
45+
// entries in them so we cannot support consensus logic on machines that only have 16-bit memory
46+
// addresses.
47+
//
48+
// We specifically do not use `target_pointer_width` because of the possibility that pointer width
49+
// does not equal index size.
50+
//
51+
// ref: https://github.com/rust-bitcoin/rust-bitcoin/pull/2929#discussion_r1661848565
52+
internals::const_assert!(mem::size_of::<usize>() >= 4);
4653

4754
#[cfg(bench)]
4855
extern crate test;

0 commit comments

Comments
 (0)