Problem
I've just caught my self writing dynamic assertion that depends on a constant usize::MAX
.
I wrote:
u128::try_from(letters.len())
.expect("No suppor for platform with `max pointer value` >= 2**128.")
where letters.len()
is an instance of usize
.
Instead, I'd like my code to fail to compile on so rare (if existing) platforms with "pointers size >= 2**128".
I already know/read:
I've read a similar question (suggested by community).
It shows methods to assert concrete size of pointer.
eg. #[cfg(not(target_pointer_width = "64"))]
I want my code to be very flexible. It's enough for usize::MAX<=u128::MAX && usize::MIN>=u128::MIN
to be true
.
Small reflection (maybe solution)
After some thinking, I concoct a solution that works quite good, both in my head & on the currently tested Debian with x64 architecture and Rust 1.60.
Code: const _:()= assert!(usize::BITS<=u128::BITS);
Do you know any cleaner & edge-case-proof resolution?