-
Notifications
You must be signed in to change notification settings - Fork 13k
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. |
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: