@@ -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}
0 commit comments