-
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
Implement as_ne_bytes() for integers and floats #76610
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
It seems like these should probably return arrays rather than slices. Those can then be coerced to a slice by the caller if needed, whereas the reverse requires a runtime check in general. I am wondering if providing r? @LukasKalbertodt for unstable API signoff, though probably makes sense to wait for the conversion to arrays |
Assuming that some form of rust-lang/rfcs#2981 will happen, does this need a distinct API? |
Modified
I can add that, but I cannot really think of a use case for writing directly to an integer/a float by byte - usually
Interesting, I wasn't aware of that RFC. No, I don't think a distinct API will be needed once the RFC is merged, but I assume that it will be a while before the RFC is implemented, so it can be worthwhile to implement |
Even if this happens, I still think having dedicated methods for some of these "basic" transmutation would be preferable from a UX standpoint. |
If so then it seems useful to have a signature converting an array of numbers as well. (As in, either keep an array or add the slice-to-slice api). That would in particular make it possible to use the standard
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
I'm fine with merging these methods as unstable. Before stabilization we can reconsider if we actually need them or whether the safe transmute RFC is sufficient.
A few things:
- Please create a new dedicated tracking issue (or let me know if you prefer me to open it) and merge both feature names into
num_as_ne_bytes
(or so). - The
transmutes
can be replaced by pointer casts (self as *const Self as *const [u8; N]
). I don't know if this is less dangerous thantransmute
, buttransmute
docs mention it as an alternative that should be preferred. - Not sure if it's worth first transmuting the floats into ints and then into the array. Why not transmute/cast them directly?
☔ The latest upstream changes (presumably #76327) made this pull request unmergeable. Please resolve the merge conflicts. Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:
|
The PR should be ready now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
@bors r+ |
📌 Commit 3c582db has been approved by |
☀️ Test successful - checks-actions, checks-azure |
This is related to issue #64464.
I am pretty sure that these functions are actually const-ify-able, and technically as_bits() can also be implemented for floats, but I might need some comments on both.