-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Signed↔Unsigned integer methods #453
Comments
I like this, especially since I often use |
We discussed this in today's @rust-lang/libs-api meeting. We would definitely like to ship at least the always-bit-cast version as Note that the former exist on nightly today as (We didn't have a clear consensus in the meeting about whether we want the checked variants to return We'd like to know if those two variants would be sufficient, or if people also strongly want the versions that bit-cast in release and panic in debug. Do people have use cases that really want that variant? (And if so, do people have a good name for that variant?) |
Of these, I find the saturating case most useful in my own code but also with narrowing casts of the same signedness. Just to make sure, is
Tangential, but reminds me of an ACP I never got around to writing for float equivalents of min/max-representable integer for |
Oh wow, I totally missed the I don't have a great survey of whether people want the debug-vs-release version. I'm just thinking that ever time I can think of where I wanted something like It makes me think of places like rust-lang/rust#130229, where if the "default, easy-to-write thing" checked, we wouldn't have had the question because of course TL/DR: I think the intent difference of "look, I expect this to be positive, the thing I'm calling just wants a signed type" vs "yes, I'm smuggling intentionally-negative numbers in my unsigned type" is important enough to express in the functions. |
@joshtriplett I'm having trouble following
I would intuitively expect that something named If anything, I'd expect impl u32 {
pub const unsafe fn to_signed_unchecked(self) -> i32 {
let x = self as i32;
unsafe { hint::assume(x >= 0) };
x
}
} |
Surely you meant
Personally I wished |
Oh, I see my problem -- I looked for |
Proposal
Problem statement
One common pattern of numeric conversions is having a signed value and needing it in the unsigned type, or vice versa. Things like
isize::MAX as usize
, for example, or before we added.add(i)
needing.offset(i as isize)
.Motivating examples or use cases
Unfortunately, doing it like that isn't necessarily great. In particular, if the source value changes to something wider, it'll silently start truncating. Not to mention that it's a big length jump from
i as usize
tousize::try_from(i).unwrap()
to get a checked version, so people will generally do the short thing where we can't give them any help in debug mode becauseas
is defined to truncate, so for all we know the300_i32 as u8
might be intentional. Or maybe it turns out thati as usize
was actually converting from au16
, not doing a signedness change at all.It would be nice to have a way to express just that you want it as the other-signedness type, nothing more.
Solution sketch
Alternatives
as
problems by telling people to use a trait likenum::WrappingFrom
trait for conversions between integers rfcs#3703 instead ofas
as
-equivalent one, saying thattry_into
is fine for the checkingwrapping_to_signed
,checked_to_signed
,saturating_to_signed
instead of justreinterpret_signed
.u32::SIGNED_MAX
to stop needingi32::MAX as u32
.Links and related work
I vaguely remember some recent conversations about this, but haven't found it :/
Here's an old thread discussing something similar: https://internals.rust-lang.org/t/pre-rfc-add-methods-like-42u32-to-signed-to-the-standard-library/12173?u=scottmcm
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: