-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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. |
I don't think they're reasonable names:
|
I would be happy with anything that isn't wildly longer. (These are meant to replace Personally I think |
Nominating to discuss the names in order to resolve this issue for stabilization. |
I'm just going to push one more time for my names, I still think that if nothing else they accomplish a good compromise between length, not re-using the 'cast' terminology, doesn't introduce a new word for programmers to know since bits should be familiar to someone using this, and making clear the type of the result. Even if someone isn't sure what they do one glance at the documentation will cement it for them. |
@joshtriplett could this be included in the discussion? |
Those seem potentially reasonable and worth considering. |
@tgross35 Writing a patch for that now. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
…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 |
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: