Skip to content

Commit ba9588e

Browse files
Add const_float_bits_conv for f64::to_bits and friends
1 parent 215f2d3 commit ba9588e

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

src/libcore/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
#![feature(constctlz)]
8585
#![feature(const_panic)]
8686
#![feature(const_fn_union)]
87+
#![feature(const_float_bits_conv)]
8788
#![feature(const_generics)]
8889
#![feature(const_ptr_offset_from)]
8990
#![feature(const_result)]

src/libcore/num/f32.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,9 @@ impl f32 {
650650
///
651651
/// ```
652652
#[stable(feature = "float_bits_conv", since = "1.20.0")]
653+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
653654
#[inline]
654-
pub fn to_bits(self) -> u32 {
655+
pub const fn to_bits(self) -> u32 {
655656
// SAFETY: `u32` is a plain old datatype so we can always transmute to it
656657
unsafe { mem::transmute(self) }
657658
}
@@ -693,8 +694,9 @@ impl f32 {
693694
/// assert_eq!(v, 12.5);
694695
/// ```
695696
#[stable(feature = "float_bits_conv", since = "1.20.0")]
697+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
696698
#[inline]
697-
pub fn from_bits(v: u32) -> Self {
699+
pub const fn from_bits(v: u32) -> Self {
698700
// SAFETY: `u32` is a plain old datatype so we can always transmute from it
699701
// It turns out the safety issues with sNaN were overblown! Hooray!
700702
unsafe { mem::transmute(v) }
@@ -710,8 +712,9 @@ impl f32 {
710712
/// assert_eq!(bytes, [0x41, 0x48, 0x00, 0x00]);
711713
/// ```
712714
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
715+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
713716
#[inline]
714-
pub fn to_be_bytes(self) -> [u8; 4] {
717+
pub const fn to_be_bytes(self) -> [u8; 4] {
715718
self.to_bits().to_be_bytes()
716719
}
717720

@@ -725,8 +728,9 @@ impl f32 {
725728
/// assert_eq!(bytes, [0x00, 0x00, 0x48, 0x41]);
726729
/// ```
727730
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
731+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
728732
#[inline]
729-
pub fn to_le_bytes(self) -> [u8; 4] {
733+
pub const fn to_le_bytes(self) -> [u8; 4] {
730734
self.to_bits().to_le_bytes()
731735
}
732736

@@ -753,8 +757,9 @@ impl f32 {
753757
/// );
754758
/// ```
755759
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
760+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
756761
#[inline]
757-
pub fn to_ne_bytes(self) -> [u8; 4] {
762+
pub const fn to_ne_bytes(self) -> [u8; 4] {
758763
self.to_bits().to_ne_bytes()
759764
}
760765

@@ -767,8 +772,9 @@ impl f32 {
767772
/// assert_eq!(value, 12.5);
768773
/// ```
769774
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
775+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
770776
#[inline]
771-
pub fn from_be_bytes(bytes: [u8; 4]) -> Self {
777+
pub const fn from_be_bytes(bytes: [u8; 4]) -> Self {
772778
Self::from_bits(u32::from_be_bytes(bytes))
773779
}
774780

@@ -781,8 +787,9 @@ impl f32 {
781787
/// assert_eq!(value, 12.5);
782788
/// ```
783789
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
790+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
784791
#[inline]
785-
pub fn from_le_bytes(bytes: [u8; 4]) -> Self {
792+
pub const fn from_le_bytes(bytes: [u8; 4]) -> Self {
786793
Self::from_bits(u32::from_le_bytes(bytes))
787794
}
788795

@@ -806,8 +813,9 @@ impl f32 {
806813
/// assert_eq!(value, 12.5);
807814
/// ```
808815
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
816+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
809817
#[inline]
810-
pub fn from_ne_bytes(bytes: [u8; 4]) -> Self {
818+
pub const fn from_ne_bytes(bytes: [u8; 4]) -> Self {
811819
Self::from_bits(u32::from_ne_bytes(bytes))
812820
}
813821
}

src/libcore/num/f64.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -664,8 +664,9 @@ impl f64 {
664664
///
665665
/// ```
666666
#[stable(feature = "float_bits_conv", since = "1.20.0")]
667+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
667668
#[inline]
668-
pub fn to_bits(self) -> u64 {
669+
pub const fn to_bits(self) -> u64 {
669670
// SAFETY: `u64` is a plain old datatype so we can always transmute to it
670671
unsafe { mem::transmute(self) }
671672
}
@@ -707,8 +708,9 @@ impl f64 {
707708
/// assert_eq!(v, 12.5);
708709
/// ```
709710
#[stable(feature = "float_bits_conv", since = "1.20.0")]
711+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
710712
#[inline]
711-
pub fn from_bits(v: u64) -> Self {
713+
pub const fn from_bits(v: u64) -> Self {
712714
// SAFETY: `u64` is a plain old datatype so we can always transmute from it
713715
// It turns out the safety issues with sNaN were overblown! Hooray!
714716
unsafe { mem::transmute(v) }
@@ -724,8 +726,9 @@ impl f64 {
724726
/// assert_eq!(bytes, [0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
725727
/// ```
726728
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
729+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
727730
#[inline]
728-
pub fn to_be_bytes(self) -> [u8; 8] {
731+
pub const fn to_be_bytes(self) -> [u8; 8] {
729732
self.to_bits().to_be_bytes()
730733
}
731734

@@ -739,8 +742,9 @@ impl f64 {
739742
/// assert_eq!(bytes, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]);
740743
/// ```
741744
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
745+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
742746
#[inline]
743-
pub fn to_le_bytes(self) -> [u8; 8] {
747+
pub const fn to_le_bytes(self) -> [u8; 8] {
744748
self.to_bits().to_le_bytes()
745749
}
746750

@@ -767,8 +771,9 @@ impl f64 {
767771
/// );
768772
/// ```
769773
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
774+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
770775
#[inline]
771-
pub fn to_ne_bytes(self) -> [u8; 8] {
776+
pub const fn to_ne_bytes(self) -> [u8; 8] {
772777
self.to_bits().to_ne_bytes()
773778
}
774779

@@ -781,8 +786,9 @@ impl f64 {
781786
/// assert_eq!(value, 12.5);
782787
/// ```
783788
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
789+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
784790
#[inline]
785-
pub fn from_be_bytes(bytes: [u8; 8]) -> Self {
791+
pub const fn from_be_bytes(bytes: [u8; 8]) -> Self {
786792
Self::from_bits(u64::from_be_bytes(bytes))
787793
}
788794

@@ -795,8 +801,9 @@ impl f64 {
795801
/// assert_eq!(value, 12.5);
796802
/// ```
797803
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
804+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
798805
#[inline]
799-
pub fn from_le_bytes(bytes: [u8; 8]) -> Self {
806+
pub const fn from_le_bytes(bytes: [u8; 8]) -> Self {
800807
Self::from_bits(u64::from_le_bytes(bytes))
801808
}
802809

@@ -820,8 +827,9 @@ impl f64 {
820827
/// assert_eq!(value, 12.5);
821828
/// ```
822829
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
830+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
823831
#[inline]
824-
pub fn from_ne_bytes(bytes: [u8; 8]) -> Self {
832+
pub const fn from_ne_bytes(bytes: [u8; 8]) -> Self {
825833
Self::from_bits(u64::from_ne_bytes(bytes))
826834
}
827835
}

0 commit comments

Comments
 (0)