Skip to content

Commit ad47f08

Browse files
tbu-tesuji
authored andcommitted
Add float conversions to and from bytes
Use the same API as for integers. Fixes #57492.
1 parent db592f4 commit ad47f08

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

src/libcore/num/f32.rs

+70
Original file line numberDiff line numberDiff line change
@@ -461,4 +461,74 @@ impl f32 {
461461
// It turns out the safety issues with sNaN were overblown! Hooray!
462462
unsafe { mem::transmute(v) }
463463
}
464+
465+
/// Return the floating point value as a byte array in big-endian byte order.
466+
///
467+
/// # Examples
468+
///
469+
/// ```
470+
/// assert_eq!(0.0f32.to_be_bytes(), [0b0000_0000, 0b0000_0000, 0b0000_0000, 0b0000_0000]);
471+
/// assert_eq!(1.0f32.to_be_bytes(), [0b0111_1111, 0b1000_0000, 0b0000_0000, 0b0000_0000]);
472+
/// ```
473+
#[unstable(feature = "float_to_from_bytes", issue = "60446")]
474+
#[inline]
475+
pub fn to_be_bytes(self) -> [u8; 4] {
476+
self.to_bits().to_be_bytes()
477+
}
478+
479+
/// Return the floating point value as a byte array in little-endian byte order.
480+
///
481+
/// # Examples
482+
///
483+
/// ```
484+
/// assert_eq!(0.0f32.to_le_bytes(), [0b0000_0000, 0b0000_0000, 0b0000_0000, 0b0000_0000]);
485+
/// assert_eq!(1.0f32.to_le_bytes(), [0b0000_0000, 0b0000_0000, 0b1000_0000, 0b0111_1111]);
486+
/// ```
487+
#[unstable(feature = "float_to_from_bytes", issue = "60446")]
488+
#[inline]
489+
pub fn to_le_bytes(self) -> [u8; 4] {
490+
self.to_bits().to_le_bytes()
491+
}
492+
493+
/// Return the floating point value as a byte array in native byte order.
494+
///
495+
///
496+
/// As the target platform's native endianness is used, portable code
497+
/// should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate, instead.
498+
///
499+
/// # Examples
500+
///
501+
/// ```
502+
/// assert_eq!(
503+
/// u32::from_ne_bytes(0.0f32.to_ne_bytes()),
504+
/// 0b0000_0000_0000_0000_0000_0000_0000_0000,
505+
/// );
506+
/// assert_eq!(
507+
/// u32::from_ne_bytes(1.0f32.to_ne_bytes()),
508+
/// 0b0111_1111_1000_0000_0000_0000_0000_0000,
509+
/// );
510+
/// ```
511+
#[unstable(feature = "float_to_from_bytes", issue = "60446")]
512+
#[inline]
513+
pub fn to_ne_bytes(self) -> [u8; 4] {
514+
self.to_bits().to_ne_bytes()
515+
}
516+
517+
#[unstable(feature = "float_to_from_bytes", issue = "60446")]
518+
#[inline]
519+
pub fn from_be_bytes(bytes: [u8; 4]) -> Self {
520+
Self::from_bits(u32::from_be_bytes(bytes))
521+
}
522+
523+
#[unstable(feature = "float_to_from_bytes", issue = "60446")]
524+
#[inline]
525+
pub fn from_le_bytes(bytes: [u8; 4]) -> Self {
526+
Self::from_bits(u32::from_le_bytes(bytes))
527+
}
528+
529+
#[unstable(feature = "float_to_from_bytes", issue = "60446")]
530+
#[inline]
531+
pub fn from_ne_bytes(bytes: [u8; 4]) -> Self {
532+
Self::from_bits(u32::from_ne_bytes(bytes))
533+
}
464534
}

src/libcore/num/f64.rs

+36
Original file line numberDiff line numberDiff line change
@@ -474,4 +474,40 @@ impl f64 {
474474
// It turns out the safety issues with sNaN were overblown! Hooray!
475475
unsafe { mem::transmute(v) }
476476
}
477+
478+
#[unstable(feature = "float_to_from_bytes", issue = "60446")]
479+
#[inline]
480+
pub fn to_be_bytes(self) -> [u8; 8] {
481+
self.to_bits().to_be_bytes()
482+
}
483+
484+
#[unstable(feature = "float_to_from_bytes", issue = "60446")]
485+
#[inline]
486+
pub fn to_le_bytes(self) -> [u8; 8] {
487+
self.to_bits().to_le_bytes()
488+
}
489+
490+
#[unstable(feature = "float_to_from_bytes", issue = "60446")]
491+
#[inline]
492+
pub fn to_ne_bytes(self) -> [u8; 8] {
493+
self.to_bits().to_ne_bytes()
494+
}
495+
496+
#[unstable(feature = "float_to_from_bytes", issue = "60446")]
497+
#[inline]
498+
pub fn from_be_bytes(bytes: [u8; 8]) -> Self {
499+
Self::from_bits(u64::from_be_bytes(bytes))
500+
}
501+
502+
#[unstable(feature = "float_to_from_bytes", issue = "60446")]
503+
#[inline]
504+
pub fn from_le_bytes(bytes: [u8; 8]) -> Self {
505+
Self::from_bits(u64::from_le_bytes(bytes))
506+
}
507+
508+
#[unstable(feature = "float_to_from_bytes", issue = "60446")]
509+
#[inline]
510+
pub fn from_ne_bytes(bytes: [u8; 8]) -> Self {
511+
Self::from_bits(u64::from_ne_bytes(bytes))
512+
}
477513
}

0 commit comments

Comments
 (0)