1
Fork 0

Add #![feature(const_float_bits_conv)] for f64::to_bits and friends

This commit is contained in:
Dylan MacKenzie 2020-08-22 12:24:35 -07:00
parent 527a685e40
commit 3cd450ec5d
3 changed files with 33 additions and 16 deletions

View file

@ -73,6 +73,7 @@
#![feature(const_discriminant)] #![feature(const_discriminant)]
#![feature(const_checked_int_methods)] #![feature(const_checked_int_methods)]
#![feature(const_euclidean_int_methods)] #![feature(const_euclidean_int_methods)]
#![feature(const_float_bits_conv)]
#![feature(const_overflowing_int_methods)] #![feature(const_overflowing_int_methods)]
#![feature(const_int_unchecked_arith)] #![feature(const_int_unchecked_arith)]
#![feature(const_int_pow)] #![feature(const_int_pow)]

View file

@ -652,8 +652,9 @@ impl f32 {
/// ///
/// ``` /// ```
#[stable(feature = "float_bits_conv", since = "1.20.0")] #[stable(feature = "float_bits_conv", since = "1.20.0")]
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
#[inline] #[inline]
pub fn to_bits(self) -> u32 { pub const fn to_bits(self) -> u32 {
// SAFETY: `u32` is a plain old datatype so we can always transmute to it // SAFETY: `u32` is a plain old datatype so we can always transmute to it
unsafe { mem::transmute(self) } unsafe { mem::transmute(self) }
} }
@ -695,8 +696,9 @@ impl f32 {
/// assert_eq!(v, 12.5); /// assert_eq!(v, 12.5);
/// ``` /// ```
#[stable(feature = "float_bits_conv", since = "1.20.0")] #[stable(feature = "float_bits_conv", since = "1.20.0")]
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
#[inline] #[inline]
pub fn from_bits(v: u32) -> Self { pub const fn from_bits(v: u32) -> Self {
// SAFETY: `u32` is a plain old datatype so we can always transmute from it // SAFETY: `u32` is a plain old datatype so we can always transmute from it
// It turns out the safety issues with sNaN were overblown! Hooray! // It turns out the safety issues with sNaN were overblown! Hooray!
unsafe { mem::transmute(v) } unsafe { mem::transmute(v) }
@ -712,8 +714,9 @@ impl f32 {
/// assert_eq!(bytes, [0x41, 0x48, 0x00, 0x00]); /// assert_eq!(bytes, [0x41, 0x48, 0x00, 0x00]);
/// ``` /// ```
#[stable(feature = "float_to_from_bytes", since = "1.40.0")] #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
#[inline] #[inline]
pub fn to_be_bytes(self) -> [u8; 4] { pub const fn to_be_bytes(self) -> [u8; 4] {
self.to_bits().to_be_bytes() self.to_bits().to_be_bytes()
} }
@ -727,8 +730,9 @@ impl f32 {
/// assert_eq!(bytes, [0x00, 0x00, 0x48, 0x41]); /// assert_eq!(bytes, [0x00, 0x00, 0x48, 0x41]);
/// ``` /// ```
#[stable(feature = "float_to_from_bytes", since = "1.40.0")] #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
#[inline] #[inline]
pub fn to_le_bytes(self) -> [u8; 4] { pub const fn to_le_bytes(self) -> [u8; 4] {
self.to_bits().to_le_bytes() self.to_bits().to_le_bytes()
} }
@ -755,8 +759,9 @@ impl f32 {
/// ); /// );
/// ``` /// ```
#[stable(feature = "float_to_from_bytes", since = "1.40.0")] #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
#[inline] #[inline]
pub fn to_ne_bytes(self) -> [u8; 4] { pub const fn to_ne_bytes(self) -> [u8; 4] {
self.to_bits().to_ne_bytes() self.to_bits().to_ne_bytes()
} }
@ -769,8 +774,9 @@ impl f32 {
/// assert_eq!(value, 12.5); /// assert_eq!(value, 12.5);
/// ``` /// ```
#[stable(feature = "float_to_from_bytes", since = "1.40.0")] #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
#[inline] #[inline]
pub fn from_be_bytes(bytes: [u8; 4]) -> Self { pub const fn from_be_bytes(bytes: [u8; 4]) -> Self {
Self::from_bits(u32::from_be_bytes(bytes)) Self::from_bits(u32::from_be_bytes(bytes))
} }
@ -783,8 +789,9 @@ impl f32 {
/// assert_eq!(value, 12.5); /// assert_eq!(value, 12.5);
/// ``` /// ```
#[stable(feature = "float_to_from_bytes", since = "1.40.0")] #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
#[inline] #[inline]
pub fn from_le_bytes(bytes: [u8; 4]) -> Self { pub const fn from_le_bytes(bytes: [u8; 4]) -> Self {
Self::from_bits(u32::from_le_bytes(bytes)) Self::from_bits(u32::from_le_bytes(bytes))
} }
@ -808,8 +815,9 @@ impl f32 {
/// assert_eq!(value, 12.5); /// assert_eq!(value, 12.5);
/// ``` /// ```
#[stable(feature = "float_to_from_bytes", since = "1.40.0")] #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
#[inline] #[inline]
pub fn from_ne_bytes(bytes: [u8; 4]) -> Self { pub const fn from_ne_bytes(bytes: [u8; 4]) -> Self {
Self::from_bits(u32::from_ne_bytes(bytes)) Self::from_bits(u32::from_ne_bytes(bytes))
} }

View file

@ -666,8 +666,9 @@ impl f64 {
/// ///
/// ``` /// ```
#[stable(feature = "float_bits_conv", since = "1.20.0")] #[stable(feature = "float_bits_conv", since = "1.20.0")]
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
#[inline] #[inline]
pub fn to_bits(self) -> u64 { pub const fn to_bits(self) -> u64 {
// SAFETY: `u64` is a plain old datatype so we can always transmute to it // SAFETY: `u64` is a plain old datatype so we can always transmute to it
unsafe { mem::transmute(self) } unsafe { mem::transmute(self) }
} }
@ -709,8 +710,9 @@ impl f64 {
/// assert_eq!(v, 12.5); /// assert_eq!(v, 12.5);
/// ``` /// ```
#[stable(feature = "float_bits_conv", since = "1.20.0")] #[stable(feature = "float_bits_conv", since = "1.20.0")]
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
#[inline] #[inline]
pub fn from_bits(v: u64) -> Self { pub const fn from_bits(v: u64) -> Self {
// SAFETY: `u64` is a plain old datatype so we can always transmute from it // SAFETY: `u64` is a plain old datatype so we can always transmute from it
// It turns out the safety issues with sNaN were overblown! Hooray! // It turns out the safety issues with sNaN were overblown! Hooray!
unsafe { mem::transmute(v) } unsafe { mem::transmute(v) }
@ -726,8 +728,9 @@ impl f64 {
/// assert_eq!(bytes, [0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); /// assert_eq!(bytes, [0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
/// ``` /// ```
#[stable(feature = "float_to_from_bytes", since = "1.40.0")] #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
#[inline] #[inline]
pub fn to_be_bytes(self) -> [u8; 8] { pub const fn to_be_bytes(self) -> [u8; 8] {
self.to_bits().to_be_bytes() self.to_bits().to_be_bytes()
} }
@ -741,8 +744,9 @@ impl f64 {
/// assert_eq!(bytes, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]); /// assert_eq!(bytes, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]);
/// ``` /// ```
#[stable(feature = "float_to_from_bytes", since = "1.40.0")] #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
#[inline] #[inline]
pub fn to_le_bytes(self) -> [u8; 8] { pub const fn to_le_bytes(self) -> [u8; 8] {
self.to_bits().to_le_bytes() self.to_bits().to_le_bytes()
} }
@ -769,8 +773,9 @@ impl f64 {
/// ); /// );
/// ``` /// ```
#[stable(feature = "float_to_from_bytes", since = "1.40.0")] #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
#[inline] #[inline]
pub fn to_ne_bytes(self) -> [u8; 8] { pub const fn to_ne_bytes(self) -> [u8; 8] {
self.to_bits().to_ne_bytes() self.to_bits().to_ne_bytes()
} }
@ -783,8 +788,9 @@ impl f64 {
/// assert_eq!(value, 12.5); /// assert_eq!(value, 12.5);
/// ``` /// ```
#[stable(feature = "float_to_from_bytes", since = "1.40.0")] #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
#[inline] #[inline]
pub fn from_be_bytes(bytes: [u8; 8]) -> Self { pub const fn from_be_bytes(bytes: [u8; 8]) -> Self {
Self::from_bits(u64::from_be_bytes(bytes)) Self::from_bits(u64::from_be_bytes(bytes))
} }
@ -797,8 +803,9 @@ impl f64 {
/// assert_eq!(value, 12.5); /// assert_eq!(value, 12.5);
/// ``` /// ```
#[stable(feature = "float_to_from_bytes", since = "1.40.0")] #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
#[inline] #[inline]
pub fn from_le_bytes(bytes: [u8; 8]) -> Self { pub const fn from_le_bytes(bytes: [u8; 8]) -> Self {
Self::from_bits(u64::from_le_bytes(bytes)) Self::from_bits(u64::from_le_bytes(bytes))
} }
@ -822,8 +829,9 @@ impl f64 {
/// assert_eq!(value, 12.5); /// assert_eq!(value, 12.5);
/// ``` /// ```
#[stable(feature = "float_to_from_bytes", since = "1.40.0")] #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
#[inline] #[inline]
pub fn from_ne_bytes(bytes: [u8; 8]) -> Self { pub const fn from_ne_bytes(bytes: [u8; 8]) -> Self {
Self::from_bits(u64::from_ne_bytes(bytes)) Self::from_bits(u64::from_ne_bytes(bytes))
} }