Generic Integers V2: It's Time by clarfonthey · Pull Request #3686 · rust-lang/rfcs (original) (raw)
Even if we should probably leave them out of the initial RFC for complexity reasons, I would just cheat with floats, as they rely on system libraries and hardware instructions way more than regular integers. By that, I mean that I'd allow
f<32>
for consistency reasons, but only those that are actually supported would compile; i.e.,f<3>
would throw a compile-time error (it could either be done at monomorphisation time, or disallowing const generics on that one).
The problem with this approach is that any "cheating" becomes permanently stabilised, and thus, it's worth putting in some thought for the design. This isn't to say that f<N>
is a bad design (I personally don't like it, but I won't fault people for wanting to use it), but rather that u<N>
and i<N>
are good designs in several ways that f<N>
is not.
Plus, monomorphisation-time errors were actually one of the big downsides to the original RFC, and I suspect that people haven't really changed their thoughts since then. Effectively, while it's okay to allow some of edge-case monomorphisation-time errors like this RFC includes (for example, asking for u<0xFFFF_FFFF_FFFF>
is a hard error, since it's larger than u32::MAX
), but not extremely-common errors like just asking for f<N>
where N
is anything that isn't 16, 32, 64, or 128.
One potential solution that was proposed for unifying u<N>
, i<N>
, usize
, and isize
was to have some separate ADT that encapsulates signedness and has different options for "size" and N
. This kind of solution feels promising for generic floats since it means that you could have an impl like:
impl<const F: FloatKind> MyTrait for f<F> {
// ...
}
And it would support all float types, forever, and there would be no invalid values for F
since we've explicitly defined it. However, this requires const_adt_params
which is currently unstable.
Overall, I think this RFC is in the right track, but I'd postpone it until we're past Rust 2024.
As stated: yes, RFCs take time to discuss and implement and it's very reasonable to expect people to focus on the 2024 edition for now. However, that doesn't mean that we can't discuss this now, especially since there are bound to be things that were missed that would be good to point out.