-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Stabilize const for integer {to,from}_{be,le,ne}_bytes methods #69373
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2195,7 +2195,7 @@ let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes(); | |
assert_eq!(bytes, ", $be_bytes, "); | ||
```"), | ||
#[stable(feature = "int_to_from_bytes", since = "1.32.0")] | ||
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")] | ||
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")] | ||
#[inline] | ||
pub const fn to_be_bytes(self) -> [u8; mem::size_of::<Self>()] { | ||
self.to_be().to_ne_bytes() | ||
|
@@ -2215,7 +2215,7 @@ let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes(); | |
assert_eq!(bytes, ", $le_bytes, "); | ||
```"), | ||
#[stable(feature = "int_to_from_bytes", since = "1.32.0")] | ||
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")] | ||
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")] | ||
#[inline] | ||
pub const fn to_le_bytes(self) -> [u8; mem::size_of::<Self>()] { | ||
self.to_le().to_ne_bytes() | ||
|
@@ -2250,7 +2250,8 @@ assert_eq!( | |
); | ||
```"), | ||
#[stable(feature = "int_to_from_bytes", since = "1.32.0")] | ||
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")] | ||
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")] | ||
#[allow_internal_unstable(const_transmute)] | ||
#[inline] | ||
pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] { | ||
// SAFETY: integers are plain old datatypes so we can always transmute them to | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would use the union based implementation as done in the aforementioned PR instead of the transmute based one with the macro, as that seems more hacky. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Centril This code doesn't compile. Transmute or not you'll either need that hack macro or fix #[allow_internal_unstable(const_fn)] // <-- no effect
const fn h(a: u64) -> [u8; 8] {
union U {
a: u64,
b: [u8; 8],
}
let u = U { a };
unsafe {
u.b //~ ERROR [E0723]: accessing union fields is unstable
}
} (Also, a union doesn't check the type size. Using a union is equivalent to doing transmute_copy which is more dangerous than transmute) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The macro doesn't do the same thing as The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whether "fixing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, changing the attribute's name to not be confusing won't make it work for non-language features anyway. My point was that there's no reason to use a macro, because it's the same as adding the feature at the crate level, just unnecessarily complicated. Overall I am pretty confused at how this system is supposed to work and I don't think I want to merge this PR until we hear from @oli-obk. Also, we don't need to wait for a change to the attribute to trickle into beta because we can always There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If anything this probably shows we might want
While Anyway this is all just speculation, if nothing else, I want to use a different name that's clearly about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine just naming it Anyway, as said at the start of this post, I'm fine renaming it. Especially since people are confused by const fn having the same attribute as macro_rules. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I converted from transmute to union as suggested by @Centril, and that incidentally allowed this to build as unions are language features not library features. However, now I'm reading the point by @kennytm that a union doesn't check the type size, so if that causes an issue I can revert that bit. (I don't think it's an issue here, the union is between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea the change looks alright. We'll want to move back to transmute at some point, so I added the const-hack label to this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Fwiw, this was not incidental at all; that's why I suggested it. ;) |
||
|
@@ -2284,7 +2285,7 @@ fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), | |
} | ||
```"), | ||
#[stable(feature = "int_to_from_bytes", since = "1.32.0")] | ||
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")] | ||
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")] | ||
#[inline] | ||
pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self { | ||
Self::from_be(Self::from_ne_bytes(bytes)) | ||
|
@@ -2317,7 +2318,7 @@ fn read_le_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), | |
} | ||
```"), | ||
#[stable(feature = "int_to_from_bytes", since = "1.32.0")] | ||
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")] | ||
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")] | ||
#[inline] | ||
pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self { | ||
Self::from_le(Self::from_ne_bytes(bytes)) | ||
|
@@ -2360,7 +2361,8 @@ fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), | |
} | ||
```"), | ||
#[stable(feature = "int_to_from_bytes", since = "1.32.0")] | ||
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")] | ||
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")] | ||
#[allow_internal_unstable(const_transmute)] | ||
#[inline] | ||
pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self { | ||
// SAFETY: integers are plain old datatypes so we can always transmute to them | ||
|
@@ -4132,7 +4134,7 @@ let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes(); | |
assert_eq!(bytes, ", $be_bytes, "); | ||
```"), | ||
#[stable(feature = "int_to_from_bytes", since = "1.32.0")] | ||
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")] | ||
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")] | ||
#[inline] | ||
pub const fn to_be_bytes(self) -> [u8; mem::size_of::<Self>()] { | ||
self.to_be().to_ne_bytes() | ||
|
@@ -4152,7 +4154,7 @@ let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes(); | |
assert_eq!(bytes, ", $le_bytes, "); | ||
```"), | ||
#[stable(feature = "int_to_from_bytes", since = "1.32.0")] | ||
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")] | ||
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")] | ||
#[inline] | ||
pub const fn to_le_bytes(self) -> [u8; mem::size_of::<Self>()] { | ||
self.to_le().to_ne_bytes() | ||
|
@@ -4187,7 +4189,8 @@ assert_eq!( | |
); | ||
```"), | ||
#[stable(feature = "int_to_from_bytes", since = "1.32.0")] | ||
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")] | ||
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")] | ||
#[allow_internal_unstable(const_transmute)] | ||
#[inline] | ||
pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] { | ||
// SAFETY: integers are plain old datatypes so we can always transmute them to | ||
|
@@ -4221,7 +4224,7 @@ fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), | |
} | ||
```"), | ||
#[stable(feature = "int_to_from_bytes", since = "1.32.0")] | ||
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")] | ||
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")] | ||
#[inline] | ||
pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self { | ||
Self::from_be(Self::from_ne_bytes(bytes)) | ||
|
@@ -4254,7 +4257,7 @@ fn read_le_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), | |
} | ||
```"), | ||
#[stable(feature = "int_to_from_bytes", since = "1.32.0")] | ||
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")] | ||
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")] | ||
#[inline] | ||
pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self { | ||
Self::from_le(Self::from_ne_bytes(bytes)) | ||
|
@@ -4297,7 +4300,8 @@ fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), | |
} | ||
```"), | ||
#[stable(feature = "int_to_from_bytes", since = "1.32.0")] | ||
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")] | ||
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")] | ||
#[allow_internal_unstable(const_transmute)] | ||
#[inline] | ||
pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self { | ||
// SAFETY: integers are plain old datatypes so we can always transmute to them | ||
|
Uh oh!
There was an error while loading. Please reload this page.