Best type alias for uXX type corresponding to usize
⚓ Rust 📅 2026-03-14 👤 surdeus 👁️ 5I need to do something like
#[cfg(target_pointer_width = "64")]
pub type PlatformWord = u64;
#[cfg(not(target_pointer_width = "64"))]
pub type PlatformWord = u32;
because I need a uXX type identical in size to usize. usize does not participate to Into/From conversions except for trivial cases, and I need to write trait bound like usize: Into<u32>.
The (debatable) logic is that target_pointer_width is a "natural" type for the architecture.
Possibilities are: PlatformWord, TargetWord, ArchWord, HostWord, but I'm leaning recently towards Usize.
I'm sure someone else had the same problem, so I'd love to hear suggestions and comments. This is just cosmetic, of course.
EDIT: I forgot to say that in the intended usage, values of type PlatformWord are basically never indices. So in an ideal world there would be a best integer type for a platform, with size possibly different from usize, and I'd use that.
NativeWord is another contender.
1 post - 1 participant
🏷️ Rust_feed