I'm writing an encoding library and I'd like to convert a slice
into a usize
.
I see a read_uint
method that looks promising, though I'm unsure how to get the register size as a variable so I can put it in the function.
For example I'd like to get 32
on a 32 bit processor, and 64
on a 64 bit processor.
TL;DR there is a good reason for not providing a
read_usize
function, because it is not consistent on different cpu-architectures.This is a bad idea. Normally you have some kind of protocol you are trying to deserialize. This format should be independent from the cpu-architecture and therefore you can't read an usize, because it is cpu-dependent.
Let's assume you have a simple protocol where you first have the size of an array and afterwards
n
elements.Let's suppose the protocol says that your size is 4 byte long. Now you want to do the thing Shepmaster suggested and read the usize dependent on your architecture.
On a x86_64 OS you will now read 8 bytes and therefore swallow the first element in your array.
On a Atmega8 your usize would be 2 bytes and therefore only take the first 2 bytes of your size (which might be zero in case there are less than 65k elements and a BigEndian byte-order).
This is the reason why there is no
read_usize
function and it is correct. You need to decide how long your size is, read the exact amount of bytes from your slice and then usas
to convert that into anusize
.One way is to use
mem::size_of
to get the size of ausize
:Another is to have different functions or function implementations for different architectures:
See also: