-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Tracking Issue for integer_sign_cast
#125882
Comments
…lacrum Implement feature `integer_sign_cast` Tracking issue: rust-lang#125882 Since this is my first time making a library addition I wasn't sure where to place the new code relative to existing code. I decided to place it near the top where there are already some other basic bitwise manipulation functions. If there is an official guideline for the ordering of functions, please let me know.
Rollup merge of rust-lang#125884 - Rua:integer_sign_cast, r=Mark-Simulacrum Implement feature `integer_sign_cast` Tracking issue: rust-lang#125882 Since this is my first time making a library addition I wasn't sure where to place the new code relative to existing code. I decided to place it near the top where there are already some other basic bitwise manipulation functions. If there is an official guideline for the ordering of functions, please let me know.
I think that I think something like |
There wouldn't be any overflows, as I understand the term. The number of bits stays the same, which is expressly part of the design of the function. |
It does not communicate what happens to values of unsigned integers that do not fit into the target integer values. We're reinterpreting the bits as an unsigned integer, but |
@tbu- ah now I see your meaning Your suggestion of |
What about |
IMO: A consistent API is most important, so |
After thinking about this for a while, and looking at how I'm actually using this function, I think I agree with the suggestion to use |
I personally find So as much as I don't care for the naming, it would be consistent with that. With that said, casting a sign and reinterpreting the type entirely feel like two different things that don't necessarily have precedential impact. An integer and a float share effectively zero in common when using |
Do you happen to have any examples of this usage in public repositories? |
@aapoalas While not using this feature directly, the time-rs/time repository uses an API from |
I’m not too keen on (re some other suggestions: |
What about something like Hints that it is bitwise and doesn't introduce a new term (I take it as a given that someone performing this operation already knows the term "bits"), doesn't try to put one or the other as the canonical representation, still uses the familiar from/to jargon, and is a bit less verbose than some other wordier proposals |
I keep finding myself needing this for dealing with C APIs and ioctls. I don't like the |
I do like |
@Rua I'm definitely not liking the "wrapping" or "unwrapping" terminology. They're not at all wrappers. They're just different ways of interpreting the same data |
It's not wrappers as in wrapping around another thing. It's wrapping as in allowing integer wrap-around. The same as methods like |
I didn't think at all in that direction, I see what you mean but it doesn't immediately click |
Hi, I would like to ask is there something planned for |
👍 for cast_signed, cast_unsigned because it’s more consistent with other uses of the word cast in std (bit-preserving reinterpretation) vs as or to (which in many cases don’t preserve the underlying representation) |
I think it's likely that we will get similar functions with different overflow semantics (e.g. panicking, saturating, etc.), so I'd go with |
Now that you mention it, I would love a saturating version as well and have written short implementations of that already, since it's more or less trivial in comparison (without unsafe code that is). Future proofing like that seems like a decent idea. |
These have been around for a while, they're useful for eliminating uses of Shall we stabilize these? @rfcbot merge |
Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
…nsigned, r=dtolnay Add `cast_signed` and `cast_unsigned` methods for `NonZero` types Requested in rust-lang#125882 . Note that this keeps the same names as the methods currently present on other integer types. If we want to rename them, we can rename them all at the same time.
We discussed this in the libs-api meeting and decided to keep the names as-is ( |
Rollup merge of rust-lang#136511 - joshtriplett:nonzero-cast-signed-unsigned, r=dtolnay Add `cast_signed` and `cast_unsigned` methods for `NonZero` types Requested in rust-lang#125882 . Note that this keeps the same names as the methods currently present on other integer types. If we want to rename them, we can rename them all at the same time.
FCP includes the methods added to |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
…Noratrieb Stabilize (and const-stabilize) `integer_sign_cast` Tracking issue: rust-lang#125882 Closes: rust-lang#125882 FCP completed: rust-lang#125882 (comment)
Uff, I missed the FCP, but I'm wondering a bit why But I guess there's precedent in |
I've stalled the stabilization PR a bit. (I hope that was not an evil thing to do, after all, if not for me, it might not have even been opened yet.) Let's probably hear what t-libs-api has to say about this, and then I will update the PR accordingly. Once again, apologies for violating the usual process, but I hope this is in spirit with what FCP's were intended for. |
Full support for as_signed and as_unsigned as alternatives; no objection. |
Personally I'd still prefer (I don't feel strongly either way) |
We discussed this in today's @rust-lang/libs-api meeting. There were supporters for both
Based on that, we're continuing with the FCP as passed, for @GrigorenkoPV, could you please go ahead with the PR? |
|
I like It also means there's the possibility of |
|
Rollup merge of rust-lang#137026 - GrigorenkoPV:integer_sign_cast, r=jhpratt Stabilize (and const-stabilize) `integer_sign_cast` Tracking issue: rust-lang#125882 Closes: rust-lang#125882 FCP completed: rust-lang#125882 (comment)
Feature gate:
#![feature(integer_sign_cast)]
This is a tracking issue for explicit signedness casting methods for integer primitive types. Libs discussion: rust-lang/libs-team#359.
Public API
(for
N
in [8
,16
,32
,64
,128
,size
]):Steps / History
integer_sign_cast
#125884Unresolved Questions
Mostly bikeshedding regarding naming, as mentioned in the Libs discussion. The current proposal follows the naming of
cast_const
andcast_mut
for pointers.Alternatively, these could be implemented as
from_bits
andto_bits
methods for the signed types.Footnotes
https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html ↩
The text was updated successfully, but these errors were encountered: