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