-
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
as_ne_bytes for primitive types #64464
Comments
In this case I believe you can do this via a cast, e.g. It does seem viable that we could provide something like this, though. |
The only method that can possibly be implemented is
I daresay I've never heard anyone mention over-alignment before as a concern to even think about! (except in the context of deallocation, which doesn't matter here) |
It is possible to workaround that by creating a struct, which is a reversed iterator, and return that struct instead for non-ne functions. That might seem to be too much work for such a simple function though... Or we can just implement |
I think the benefits of an iterator over the existing |
Implemented in #76610 for both floats and integers, under the feature gate |
Implement as_ne_bytes() for integers and floats This is related to issue rust-lang#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.
Why is a borrow useful? Can’t the calling code assign the result of |
It gets problematic when generics are involved: trait NumberAsBytes {
fn as_bytes(&self) -> &[u8];
}
// fn as_bytes(&self) -> &[u8] { self.as_ne_bytes() }
impl_num_as_bytes!(i8, u8, /* ... */, i64, u64); A |
If a library wants to be general-purpose enough to expose such a trait, can’t it afford to do the unsafe conversion by itself? Does this really need to be in the standard library? |
Recently I've encountered a problem, when I wanted to return a slice of bytes in an integer:
(This is an oversimplification of the original code, which is generic over every integer type along with other stuff)
Obviously, since
to_le_bytes(*num)
is dropped when the function returns, this piece of code failed the borrow check. Hence my need foras_le_bytes(num)
, asnum
is actually alive all this time!The text was updated successfully, but these errors were encountered: