Generating random `usize`s; is there a footgun here I'm not considering?
⚓ Rust 📅 2025-12-08 👤 surdeus 👁️ 1To generate a random usize, I wrote this function:
use rand::Rng;
fn random_usize<R: Rng>(rng: &mut R) -> usize {
#[cfg(target_pointer_width = "64")]
let bytes: [u8; 8] = rng.random();
#[cfg(target_pointer_width = "32")]
let bytes: [u8; 4] = rng.random();
#[cfg(target_pointer_width = "16")]
let bytes: [u8; 2] = rng.random();
usize::from_ne_bytes(bytes)
}
I chose to use usize::from_ne_bytes instead of using TryFrom::try_from or as casts because I would like to avoid:
.expect("Infallible")- panics from accidental misuse (ie, lazy copy pasting of
cfgattributes and forgetting to change the pointer width) - silent truncation
- infecting code with
Results for an operation that, to my understanding, should be infallible (given it is gated behindcfgattributes for correctness)
...and any other form of incorrectness I might be forgetting here.
From what I can gather by messing around on Compiler Explorer, it's a compile-time error if I were to get the number of bytes wrong or forget to implement the pointer-width of the target. Are there any footguns I'm failing to consider here? Given what I'm after, is there a better way to do this (ie, one where I won't forget a pointer-width even if it isn't relevant to my current platform, such as some sort of exhaustive match on target_pointer_width)?
Any insight is appreciated.
3 posts - 2 participants
🏷️ Rust_feed