Generic integers
| Metadata | |
|---|---|
| Contact | Clar Fon |
| Status | Accepted |
| What and why | Allow writing code that’s generic over the size of integers |
| compiler champion | Bee |
| lang champion | Scott McMurray |
| types champion | Oli Scherer |
| Tracking issue | rust-lang/goals#871 |
| Teams | compiler, lang, libs, types |
| Task owners | Clar Fon |
Summary
Implement an MVP of the generic integers RFC, adding u<N> and i<N> types equivalent to the existing integer types such as u16 and i32, and refining the RFC in the process.
Motivation
The status quo
Since this goal already has a written RFC, the motivation for this goal is better worded in the RFC’s motivation section.
But, in short:
- Implementations of traits for integer types is particularly painful and requires using macros to output implementations for all relevant integer types.
- This also results in
rustdocoutput being particular awful, e.g. seeShl. - And, because this is literally more code, it adds a load of extra work for the compiler to compile, reference, and optimize these implementations, even though a majority of them will not actually be used downstream for any given project.
The full RFC also goes into more detail about other benefits of this implementation long-term, but these are the most relevant examples for this goal.
What we propose to do about it
This goal would tackle both a language experiment and compiler experiment at the same time. Effectively, the minimum parts we want to have in place are:
- Having the ability to represent
u<N>andi<N>types in the compiler, even ifNis limited to the current values of 8, 16, 32, 64, and 128. - Allowing type inference to use the generic parameter of integer types as well, to potentially improve the current fallback on integers to assume
u32ori32if no better option exists. - Updating the standard library implementation to use these types to improve documentation and code generation.
There are also a few reasonable extensions to this that might become part of the MVP, like:
- Creating wrapper
std::ffi::c_bitintandstd::ffi::c_bituinttypes which replicate the current ABI for these types in C. Unfortunately, the ABI for_BitInt(128)and__int128are not the same on x86_64, which indicates there is value in offering this for Rust as well. - Offering more values of
Nspecifically whereNis a multiple of 8. This means we would make a decision on how the size and alignment of these types should operate, and how they interface withrepr(packed), without determining how stuff likeu0,i1, andu23should operate. - Internally offering a version of
u<N>andi<N>that can also generalize overusizeandisize, since these are distinct types from their same-size counterparts. This would allow further simplifying the standard library’s code and potentially be available to end users once ADT const generics are stable.
At least initially, the goal will not be to implement the full RFC and some of its stretch goals, namely:
- Non-byte-rounded values of
Nbeing accepted, and allowing dedicated conversion between e.g.u1andbool. - Bitfields, and a “bit-packed” representation for structs.
- Range-based integer types, e.g.
int<0..=100>for percentages.
Work items over the next year
Team asks
| Team | Support level | Notes |
|---|---|---|
| libs | Small | Review code changes |
| compiler | Medium | Review code changes |
| lang | Medium | Review/make decisions on unresolved details |
| types | Medium | Combination of above |
Funding
Minimum funding would enable the developer for this feature (likely Clar Fon) to actually spend time on this, since it otherwise would not happen any time soon.
| Purpose | Cost | Funded | Sponsor(s) |
|---|---|---|---|
| Development | Unknown | No |