# Should u64 implement From\<usize\>?

**URL:** <https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474>\
**Category:** libs\
**Created:** [December 12, 2019, 7:38pm UTC](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474 "2019-12-12T19:38:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![tkaitchuck](https://avatars.discourse-cdn.com/v4/letter/t/eb8c5e/32.png) [@tkaitchuck](https://internals.rust-lang.org/u/tkaitchuck)\
**Post date:** [December 12, 2019, 7:38pm UTC](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474/1 "2019-12-12T19:38:08Z")

</div>

This is somewhat reviving of an [old thread](https://internals.rust-lang.org/t/numeric-into-should-not-require-everyone-to-support-16-bit-and-128-bit-usize/3609)

As of today there are no supported architectures with \>64 bit pointers. There aren't any on the horizon and that doesn't seem likely to change in the foreseeable future.

At the time of the above thread there wasn't a way to have write architecture specific trait implementations. There now is. So we could implement From for u64 today, and if a 128 bit architecture is introduced in the future, the implementation can be made architecture specific.

Obviously this would have the consequence of causing code making the assumption that this conversion can be preformed lossless to not build on 128bit architectures. However I think this is preferable to what is happening now where this trait is not available. I don't think because the trait is unavailable people are actually coding to the assumption that a u64 might not hold a usize, instead people write **try\_into().unwarp()** which would result in a failure at runtime or worse **as u64** which would hide the bug all together.

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [December 12, 2019, 7:56pm UTC](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474/2 "2019-12-12T19:56:49Z")

</div>

> [@tkaitchuck](#):
>
> There aren't any on the horizon and that doesn't seem likely to change in the foreseeable future.

There's a definition for 128-bit RISC-V. I'm not aware of specific products planned based on that definition, but it exists.

I don't, however, think we should make code on 64-bit platforms substantially harder on the basis of such hypothetical platforms. (We also, for instance, don't support 8-bit platforms, though such platforms exist.)

> [@tkaitchuck](#):
>
> At the time of the above thread there wasn't a way to have write architecture specific trait implementations. There now is.

Could you provide more information or links about this?

That also raises the reasonable question of whether we should provide architecture-specific trait impls for 32-bit platforms. (I don't think we should.)

This also brings the "portability lint" to mind. Could we have a mechanism by which you _can't_ rely on architecture-specific impls unless you declare that you will only run on architectures providing such impls?

---

<div class="post-metadata">

**Author:** ![tkaitchuck](https://avatars.discourse-cdn.com/v4/letter/t/eb8c5e/32.png) [@tkaitchuck](https://internals.rust-lang.org/u/tkaitchuck)\
**Post date:** [December 12, 2019, 8:13pm UTC](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474/3 "2019-12-12T20:13:05Z")

</div>

> [@josh](#):
>
> Could you provide more information or links about this?

Sure, you can annotate a declaration with: `#[cfg(not(target_pointer_width = "128"))]` indicating that the code that is being annotated should only be compiled in if the pointer width is not 128 bits. Today this will have no effect as everything is \<128, but if such a target were added the code would be excluded when building to that target.

> [@josh](#):
>
> That also raises the reasonable question of whether we should provide architecture-specific trait impls for 32-bit platforms. (I don't think we should.)

Yes, agreed. In general we should not do that all over the place, because it would make it easy to write code which is accidentally architecture dependent. In this specific case, I think it makes sense because there just aren't 128 bit targets, and that assumption is likely already being made. That being said, if I am wrong and suddenly there is a mainstream 128 bit architecture, it might make sense to backtrack and remove the trait implementation in the next edition to eliminate the inconsistency.

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [December 12, 2019, 8:24pm UTC](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474/4 "2019-12-12T20:24:32Z")

</div>

> [@tkaitchuck](#):
>
> That being said, if I am wrong and suddenly there is a mainstream 128 bit architecture, it might make sense to backtrack and remove the trait implementation in the next edition to eliminate the inconsistency.

I don't think we have any mechanism that would allow limiting trait impls by edition, or any other backwards-compatible means of eliminating a trait impl.

Could we have some mechanism for a module to opt into using such a trait, by way of an error-by-default lint that can be disabled for code that can safely ignore hypothetical future 128-bit platforms?

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [December 12, 2019, 9:42pm UTC](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474/5 "2019-12-12T21:42:45Z")

</div>

Edit: I've been informed that Rust's `usize` is `uintptr_t`, and **not** `size_t`. I've assumed the inverse (it has "size" in the name! and bindgen and libc alias `size_t` to `usize`), so all my unsafe/FFI code is already broken on 128-bit Rust ☹

* * *

Here's [a presentation about 128-bit RISC-V](https://riscv.org/wp-content/uploads/2016/11/Tue1530-128bit-Addr-RISC-V-Wallach-Micron.pdf):

> We do NOT want 128 bit flat addressing

The motivation there is in making _address space_ literally global. This is different from handling \>64-bit _sizes_ of data. Emphasis mine:

> A full 64-bit address space will last for 500 years if allocated at the rate of one gigabyte per second. **We believe that 64 bits is enough "for all time" on a single computer** , enough for a long time on a small network, and not enough for very long at all on the global network.”

 ![Screenshot 2019-12-12 at 21.35.44](https://us1.discourse-cdn.com/flex002/uploads/rustlang/original/2X/3/39a25fee5d1c2014b2f993b4874e847a49af0293.png)

Even 128-bit RISC-V limits byte offsets to 64-bit.

Rust is likely going to need to deal with 128-bit _pointers_ or even [256-bit permission-tagged pointers](https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/cheri-faq.html) for the CHERI architecture, but that affects size of `uintptr_t`, which is different from `size_t`.

---

<div class="post-metadata">

**Author:** ![gnzlbg](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gnzlbg/32/3858_2.png) [@gnzlbg](https://internals.rust-lang.org/u/gnzlbg)\
**Post date:** [December 13, 2019, 11:59am UTC](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474/6 "2019-12-13T11:59:45Z")

</div>

@tkaitchuck

> [@tkaitchuck](#):
>
> As of today there are no supported architectures with \>64 bit pointers. There aren't any on the horizon and that doesn't seem likely to change in the foreseeable future.

There is a branch of rustc that supports CHERI, which has 128-bit wide pointers, such that `usize` is 128-bit wide.

> Obviously this would have the consequence of causing code making the assumption that this conversion can be preformed lossless to not build on 128bit architectures.

That's the job of the portability lint. I think it is fine for the `From` implementations from `usize` to be gated on `cfg(target_pointer_width)`, we gate many things in hardware-dependent ways in libcore already (libcore is target\_os / target\_env independent, but it is not target\_arch independent).

@josh

> (We also, for instance, don't support 8-bit platforms, though such platforms exist.)

AFAICT we can support without issues all 8-bit platforms with 16-bit pointes, which is pretty much most of them. I'm not sure it makes sense to program a target with 8-bit pointers in anything but assembly (if you happened to even have a stack there, your stack pointer would be 8-bits as well..).

---

<div class="post-metadata">

**Author:** ![tkaitchuck](https://avatars.discourse-cdn.com/v4/letter/t/eb8c5e/32.png) [@tkaitchuck](https://internals.rust-lang.org/u/tkaitchuck)\
**Post date:** [December 13, 2019, 6:07pm UTC](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474/7 "2019-12-13T18:07:18Z")

</div>

> [@gnzlbg](#):
>
> There is a branch of rustc that supports CHERI, which has 128-bit wide pointers, such that `usize` is 128-bit wide.

Ah. I didn't realize that would affect usize. That sortof makes the use of usize awkward for lengths/indexing of Vec, String, etc.

It sounds like the correct solution is to not implement From and try to make this fact more widely known so that less code makes this assumption. (Hopefully this thread will help)

---

<div class="post-metadata">

**Author:** ![gnzlbg](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gnzlbg/32/3858_2.png) [@gnzlbg](https://internals.rust-lang.org/u/gnzlbg)\
**Post date:** [December 13, 2019, 6:31pm UTC](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474/8 "2019-12-13T18:31:46Z")

</div>

> [@tkaitchuck](#):
>
> That sortof makes the use of usize awkward for lengths/indexing of Vec, String, etc.

Currently `usize` is used for both things, a pointer-sized integer, and for indexing. As long as that continues to be the case, targets where both these sizes do not match like CHERI or RV128 will be awkward to use from Rust, but that's kind of an orthogonal problem to the one being discussed here (whether it makes sense to `impl From<usize> for u64` for the targets for which that is correct).

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [December 16, 2019, 11:08pm UTC](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474/9 "2019-12-16T23:08:40Z")

</div>

Per [[1212.0703] The Cost of Address Translation](https://arxiv.org/abs/1212.0703),

> While 64 bit addresses are sufficient to address any memory that can ever be constructed according to known physics, there are other practical reasons to consider longer addresses.

It does seem like we should at least have `u128: From<usize>`...

---

<div class="post-metadata">

**Author:** ![chrisd](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/chrisd/32/7232_2.png) [@chrisd](https://internals.rust-lang.org/u/chrisd)\
**Post date:** [December 17, 2019, 11:25am UTC](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474/10 "2019-12-17T11:25:06Z")

</div>

> [@gnzlbg](#):
>
> (whether it makes sense to `impl From<usize> for u64` for the targets for which that is correct).

Surely, as per scottmcm's link, it would make sense for all platforms if `usize` were defined only as an array index? Pulling double duty as both `uintptr_t` and `size_t` is what leads to this being an issue, no?

---

<div class="post-metadata">

**Author:** ![gnzlbg](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gnzlbg/32/3858_2.png) [@gnzlbg](https://internals.rust-lang.org/u/gnzlbg)\
**Post date:** [December 17, 2019, 12:05pm UTC](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474/11 "2019-12-17T12:05:59Z")

</div>

Since `usize` is defined to be `uintptr_t` (not `size_t`, nor both [0]), the `From<usize> for u64` impl is correct for all targets with \< 128-bit pointers, and we can just provide it there.

Some people think that the current definition of `usize` is a mistake, but that's a different discussion than the one about whether the `impl` could be provided or not.

* * *

[0] It is used in Rust as both, but it is defined to be `uintptr_t`. For all targets for which `uintptr_t != size_t`, `uintptr_t` size is always greater than `size_t`, so using a larger integer for indexing is correct, even though it isn't optimal.

---

<div class="post-metadata">

**Author:** ![dhardy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dhardy/32/2399_2.png) [@dhardy](https://internals.rust-lang.org/u/dhardy)\
**Post date:** [December 17, 2019, 3:25pm UTC](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474/12 "2019-12-17T15:25:02Z")

</div>

> [@gnzlbg](#):
>
> Currently `usize` is used for both things, a pointer-sized integer, and for indexing.

For what it's worth (not much), I am one of those who think this should not have been the case. Almost all non-byte `Vec`s would be perfectly fine with a `u32` index. This also caused an issue in the rand project: [reproducibility of usize samples across architectures](https://github.com/rust-random/rand/issues/805).

But this is somewhat off-topic, so I'll stop there.

---

<div class="post-metadata">

**Author:** ![bill\_myers](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bill_myers/32/4085_2.png) [@bill\_myers](https://internals.rust-lang.org/u/bill_myers)\
**Post date:** [December 17, 2019, 8:20pm UTC](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474/13 "2019-12-17T20:20:15Z")

</div>

I think what we really need are "[ui]size8/16/32/64/128" types in libcore, that have max(K, USIZE\_BITS) bits and can be converted from [ui]N and [ui]size.

And maybe also uptr and uptrN types.

---

<div class="post-metadata">

**Author:** ![gnzlbg](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gnzlbg/32/3858_2.png) [@gnzlbg](https://internals.rust-lang.org/u/gnzlbg)\
**Post date:** [December 18, 2019, 3:36pm UTC](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474/14 "2019-12-18T15:36:47Z")

</div>

> [@dhardy](#):
>
> But this is somewhat off-topic, so I'll stop there.

Maybe we should branch this discussion into a different thread. I think it would be worth it to at least identify which current pains this is causing, and which constraints we have on solutions.

---

<div class="post-metadata">

**Author:** ![dhardy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dhardy/32/2399_2.png) [@dhardy](https://internals.rust-lang.org/u/dhardy)\
**Post date:** [December 18, 2019, 3:41pm UTC](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474/15 "2019-12-18T15:41:15Z")

</div>

> [@bill\_myers](#):
>
> that have max(K, USIZE\_BITS) bits

You want your software to break on 32-bit CPUs?

> [@gnzlbg](#):
>
> Maybe we should branch this discussion into a different thread.

I don't have anything else to add, and don't see how changing the index type of `Vec` and slices is possible at this point, so it seems like a dead end. But if anyone wants ideas for a new language, make `Vec` templated over the index type (but defaulting to `u32`)?

---

<div class="post-metadata">

**Author:** ![bill\_myers](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bill_myers/32/4085_2.png) [@bill\_myers](https://internals.rust-lang.org/u/bill_myers)\
**Post date:** [December 18, 2019, 3:51pm UTC](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474/16 "2019-12-18T15:51:44Z")

</div>

> [@dhardy](#):
>
> You want your software to break on 32-bit CPUs?

Not sure what you mean.

usize32 would be 32-bit on 32-bit CPUs (and 16-bit CPUs), and if that's not enough then you should of course use usize64 which would be 64-bit on 32-bit CPUs, and so on.

---

<div class="post-metadata">

**Author:** ![dhardy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dhardy/32/2399_2.png) [@dhardy](https://internals.rust-lang.org/u/dhardy)\
**Post date:** [December 18, 2019, 3:57pm UTC](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474/17 "2019-12-18T15:57:12Z")

</div>

Sorry, I confused min/max. But the same logic holds: software may break because types are not the same on different architectures. (Besides which the main reason to use `u8` / `u16` is to save memory in structs/slices; forcing these types to be 64-bits on common machines would hit the memory bandwidth hard in some use-cases.)

---

<div class="post-metadata">

**Author:** ![SimonSapin](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/simonsapin/32/3158_2.png) [@SimonSapin](https://internals.rust-lang.org/u/SimonSapin)\
**Post date:** [December 20, 2019, 10:51pm UTC](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474/18 "2019-12-20T22:51:13Z")

</div>

> [@josh](#):
>
> We also, for instance, don't support 8-bit platforms, though such platforms exist.

Do they really exist? So called “8-bit architectures” manipulate _data_ in 8-bit units, but that doesn’t mean pointers are 8 bits. That would only leave 256 addressable bytes. For example [Intel 8008 - Wikipedia](https://en.wikipedia.org/wiki/Intel_8008) says addresses are 14 bits on that platform.

---

<div class="post-metadata">

**Author:** ![Tom-Phinney](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tom-phinney/32/3299_2.png) [@Tom-Phinney](https://internals.rust-lang.org/u/Tom-Phinney)\
**Post date:** [December 20, 2019, 11:21pm UTC](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474/19 "2019-12-20T23:21:27Z")

</div>

Even the first semiconductor microcomputer chips, such as [the Mostek 6502](https://en.wikipedia.org/wiki/MOS_Technology_6502), which preceded Intel's 808x, had addressing larger than their 8-bit data path.

---

<div class="post-metadata">

**Author:** ![SimonSapin](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/simonsapin/32/3158_2.png) [@SimonSapin](https://internals.rust-lang.org/u/SimonSapin)\
**Post date:** [December 26, 2019, 9:07am UTC](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474/20 "2019-12-26T09:07:38Z")

</div>

According to [https://en.wikipedia.org/wiki/Atmel\_AVR\_instruction\_set#Memory\_addressing\_instructions](https://en.wikipedia.org/wiki/Atmel_AVR_instruction_set#Memory_addressing_instructions) some AVR chips only have 256 bytes or less of "data address space" so the stack pointer register may only physically store 8 bits. However as far as I can tell `usize` would still be 16 bits on those chips because it’s still the same instruction set as on larger ones, and because "program ROM" is larger and also addressable. (Consider taking the address of a `static` item, or casting an `fn()` function pointer.)

[Next page](https://internals.rust-lang.org/t/should-u64-implement-from-usize/11474.md?page=2)
