Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Generic integers

Metadata
ContactClar Fon
StatusAccepted
What and whyAllow writing code that’s generic over the size of integers
compiler championBee
lang championScott McMurray
types championOli Scherer
Tracking issuerust-lang/goals#871
Teamscompiler, lang, libs, types
Task ownersClar 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:

  1. Implementations of traits for integer types is particularly painful and requires using macros to output implementations for all relevant integer types.
  2. This also results in rustdoc output being particular awful, e.g. see Shl.
  3. 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> and i<N> types in the compiler, even if N is 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 u32 or i32 if 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_bitint and std::ffi::c_bituint types which replicate the current ABI for these types in C. Unfortunately, the ABI for _BitInt(128) and __int128 are not the same on x86_64, which indicates there is value in offering this for Rust as well.
  • Offering more values of N specifically where N is 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 with repr(packed), without determining how stuff like u0, i1, and u23 should operate.
  • Internally offering a version of u<N> and i<N> that can also generalize over usize and isize, 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 N being accepted, and allowing dedicated conversion between e.g. u1 and bool.
  • 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

TaskOwner(s)Notes
ImplementationClar Fon
Incorporate feedback/changes into RFCClar Fon

Team asks

TeamSupport levelNotes
libsSmallReview code changes
compilerMediumReview code changes
langMediumReview/make decisions on unresolved details
typesMediumCombination 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.

PurposeCostFundedSponsor(s)
DevelopmentUnknownNo