This is my code in C:
#if UINTPTR_MAX == 0xFFFFu
typedef uint16_t GRAINS_UINTPTR;
#elif UINTPTR_MAX == 0xFFFFFFFFu
typedef uint32_t GRAINS_UINTPTR;
#elif UINTPTR_MAX == 0xFFFFFFFFFFFFFFFFu
typedef uint64_t GRAINS_UINTPTR;
#endif
How would one implement this in Rust?
#[cfg(target_pointer_width)]
is the answer to the title, but the code in question is just usize::MAX
.
1 Like
The type you’re looking for is usize
. Instead of GRAINS_UINTPTR
you can just use usize
, which is a pointer-sized integer.
The size of this primitive (usize
) is how many bytes it takes to reference any location in memory. For example, on a 32 bit target, this is 4 bytes and on a 64 bit target, this is 8 bytes.
(There has been a lot of discussion over whether usize
is size_t
or uintptr_t
that hasn’t really been resolved yet. AFAICT the “more correct” usage is usize = uintptr_t
, but since it has “size” in the name and is also used for indexing, people also assume usize = size_t
(and on mainstream platforms, uintptr_t = size_t
)).
2 Likes