Skip to content

Commit f39230e

Browse files
committed
Add float conversions to and from bytes
Use the same API as for integers. Fixes rust-lang#57492.
1 parent cd45b19 commit f39230e

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

src/libcore/num/f32.rs

+76
Original file line numberDiff line numberDiff line change
@@ -474,4 +474,80 @@ impl f32 {
474474
// It turns out the safety issues with sNaN were overblown! Hooray!
475475
unsafe { mem::transmute(v) }
476476
}
477+
478+
/// Return the floating point value as a byte array in big-endian byte order.
479+
///
480+
/// # Examples
481+
///
482+
/// ```
483+
/// assert_eq!(0.0f32.to_be_bytes(), [0b0000_0000, 0b0000_0000, 0b0000_0000, 0b0000_0000]);
484+
/// assert_eq!(1.0f32.to_be_bytes(), [0b0111_1111, 0b1000_0000, 0b0000_0000, 0b0000_0000]);
485+
/// ```
486+
#[unstable(feature = "float_to_from_bytes", issue = "0")]
487+
#[rustc_const_unstable(feature = "float_to_from_bytes_const")]
488+
#[inline]
489+
pub const fn to_be_bytes(self) -> [u8; 4] {
490+
self.to_bits().to_be_bytes()
491+
}
492+
493+
/// Return the floating point value as a byte array in little-endian byte order.
494+
///
495+
/// # Examples
496+
///
497+
/// ```
498+
/// assert_eq!(0.0f32.to_le_bytes(), [0b0000_0000, 0b0000_0000, 0b0000_0000, 0b0000_0000]);
499+
/// assert_eq!(1.0f32.to_le_bytes(), [0b0000_0000, 0b0000_0000, 0b1000_0000, 0b0111_1111]);
500+
/// ```
501+
#[unstable(feature = "float_to_from_bytes", issue = "0")]
502+
#[rustc_const_unstable(feature = "float_to_from_bytes_const")]
503+
#[inline]
504+
pub const fn to_le_bytes(self) -> [u8; 4] {
505+
self.to_bits().to_le_bytes()
506+
}
507+
508+
/// Return the floating point value as a byte array in native byte order.
509+
///
510+
///
511+
/// As the target platform's native endianness is used, portable code
512+
/// should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate, instead.
513+
///
514+
/// # Examples
515+
///
516+
/// ```
517+
/// assert_eq!(
518+
/// u32::from_ne_bytes(0.0f32.to_ne_bytes()),
519+
/// 0b0000_0000_0000_0000_0000_0000_0000_0000,
520+
/// );
521+
/// assert_eq!(
522+
/// u32::from_ne_bytes(1.0f32.to_ne_bytes()),
523+
/// 0b0111_1111_1000_0000_0000_0000_0000_0000,
524+
/// );
525+
/// ```
526+
#[unstable(feature = "float_to_from_bytes", issue = "0")]
527+
#[rustc_const_unstable(feature = "float_to_from_bytes_const")]
528+
#[inline]
529+
pub const fn to_ne_bytes(self) -> [u8; 4] {
530+
self.to_bits().to_ne_bytes()
531+
}
532+
533+
#[unstable(feature = "float_to_from_bytes", issue = "0")]
534+
#[rustc_const_unstable(feature = "float_to_from_bytes_const")]
535+
#[inline]
536+
pub const fn from_be_bytes(bytes: [u8; 4]) -> Self {
537+
Self::from_bits(u32::from_be_bytes(bytes))
538+
}
539+
540+
#[unstable(feature = "float_to_from_bytes", issue = "0")]
541+
#[rustc_const_unstable(feature = "float_to_from_bytes_const")]
542+
#[inline]
543+
pub const fn from_le_bytes(bytes: [u8; 4]) -> Self {
544+
Self::from_bits(u32::from_le_bytes(bytes))
545+
}
546+
547+
#[unstable(feature = "float_to_from_bytes", issue = "0")]
548+
#[rustc_const_unstable(feature = "float_to_from_bytes_const")]
549+
#[inline]
550+
pub const fn from_ne_bytes(bytes: [u8; 4]) -> Self {
551+
Self::from_bits(u32::from_ne_bytes(bytes))
552+
}
477553
}

src/libcore/num/f64.rs

+42
Original file line numberDiff line numberDiff line change
@@ -487,4 +487,46 @@ impl f64 {
487487
// It turns out the safety issues with sNaN were overblown! Hooray!
488488
unsafe { mem::transmute(v) }
489489
}
490+
491+
#[unstable(feature = "float_to_from_bytes", issue = "0")]
492+
#[rustc_const_unstable(feature = "float_to_from_bytes_const")]
493+
#[inline]
494+
pub const fn to_be_bytes(self) -> [u8; 8] {
495+
self.to_bits().to_be_bytes()
496+
}
497+
498+
#[unstable(feature = "float_to_from_bytes", issue = "0")]
499+
#[rustc_const_unstable(feature = "float_to_from_bytes_const")]
500+
#[inline]
501+
pub const fn to_le_bytes(self) -> [u8; 8] {
502+
self.to_bits().to_le_bytes()
503+
}
504+
505+
#[unstable(feature = "float_to_from_bytes", issue = "0")]
506+
#[rustc_const_unstable(feature = "float_to_from_bytes_const")]
507+
#[inline]
508+
pub const fn to_ne_bytes(self) -> [u8; 8] {
509+
self.to_bits().to_ne_bytes()
510+
}
511+
512+
#[unstable(feature = "float_to_from_bytes", issue = "0")]
513+
#[rustc_const_unstable(feature = "float_to_from_bytes_const")]
514+
#[inline]
515+
pub const fn from_be_bytes(bytes: [u8; 8]) -> Self {
516+
Self::from_bits(u64::from_be_bytes(bytes))
517+
}
518+
519+
#[unstable(feature = "float_to_from_bytes", issue = "0")]
520+
#[rustc_const_unstable(feature = "float_to_from_bytes_const")]
521+
#[inline]
522+
pub const fn from_le_bytes(bytes: [u8; 8]) -> Self {
523+
Self::from_bits(u64::from_le_bytes(bytes))
524+
}
525+
526+
#[unstable(feature = "float_to_from_bytes", issue = "0")]
527+
#[rustc_const_unstable(feature = "float_to_from_bytes_const")]
528+
#[inline]
529+
pub const fn from_ne_bytes(bytes: [u8; 8]) -> Self {
530+
Self::from_bits(u64::from_ne_bytes(bytes))
531+
}
490532
}

0 commit comments

Comments
 (0)