@@ -3568,47 +3568,119 @@ $EndFeature, "
35683568 }
35693569 }
35703570
3571- /// Return the memory representation of this integer as a byte array.
3571+ /// Return the memory representation of this integer as a byte array in
3572+ /// big-endian (network) byte order.
35723573 ///
3573- /// The target platform’s native endianness is used.
3574- /// Portable code likely wants to use this after [`to_be`] or [`to_le`].
3574+ /// # Examples
35753575 ///
3576- /// [`to_be`]: #method.to_be
3577- /// [`to_le`]: #method.to_le
3576+ /// ```
3577+ /// #![feature(int_to_from_bytes)]
3578+ ///
3579+ /// let bytes = 0x12345678i32.to_be_bytes();
3580+ /// assert_eq!(bytes, [0x12, 0x34, 0x56, 0x78]);
3581+ /// ```
3582+ #[ unstable( feature = "int_to_from_bytes" , issue = "52963" ) ]
3583+ #[ inline]
3584+ pub fn to_be_bytes( self ) -> [ u8 ; mem:: size_of:: <Self >( ) ] {
3585+ self . to_be( ) . to_ne_bytes( )
3586+ }
3587+
3588+ /// Return the memory representation of this integer as a byte array in
3589+ /// little-endian byte order.
35783590 ///
35793591 /// # Examples
35803592 ///
35813593 /// ```
35823594 /// #![feature(int_to_from_bytes)]
35833595 ///
3584- /// let bytes = 0x1234_5678_u32.to_be().to_bytes ();
3585- /// assert_eq!(bytes, [0x12, 0x34, 0x56, 0x78 ]);
3596+ /// let bytes = 0x12345678i32.to_le_bytes ();
3597+ /// assert_eq!(bytes, [0x78, 0x56, 0x34, 0x12 ]);
35863598 /// ```
35873599 #[ unstable( feature = "int_to_from_bytes" , issue = "52963" ) ]
35883600 #[ inline]
3589- pub fn to_bytes( self ) -> [ u8 ; mem:: size_of:: <Self >( ) ] {
3601+ pub fn to_le_bytes( self ) -> [ u8 ; mem:: size_of:: <Self >( ) ] {
3602+ self . to_le( ) . to_ne_bytes( )
3603+ }
3604+
3605+ /// Return the memory representation of this integer as a byte array in
3606+ /// native byte order.
3607+ ///
3608+ /// As the target platform's native endianness is used, portable code
3609+ /// should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate,
3610+ /// instead.
3611+ ///
3612+ /// [`to_be_bytes`]: #method.to_be_bytes
3613+ /// [`to_le_bytes`]: #method.to_le_bytes
3614+ ///
3615+ /// # Examples
3616+ ///
3617+ /// ```
3618+ /// #![feature(int_to_from_bytes)]
3619+ ///
3620+ /// let bytes = i32::min_value().to_be().to_ne_bytes();
3621+ /// assert_eq!(bytes, [0x80, 0, 0, 0]);
3622+ /// ```
3623+ #[ unstable( feature = "int_to_from_bytes" , issue = "52963" ) ]
3624+ #[ inline]
3625+ pub fn to_ne_bytes( self ) -> [ u8 ; mem:: size_of:: <Self >( ) ] {
35903626 unsafe { mem:: transmute( self ) }
35913627 }
35923628
3593- /// Create an integer value from its memory representation as a byte array.
3629+ /// Create an integer value from its representation as a byte array in
3630+ /// big endian.
3631+ ///
3632+ /// # Examples
3633+ ///
3634+ /// ```
3635+ /// #![feature(int_to_from_bytes)]
3636+ ///
3637+ /// let int = i32::from_be_bytes([0x12, 0x34, 0x56, 0x78]);
3638+ /// assert_eq!(int, 0x12_34_56_78);
3639+ /// ```
3640+ #[ unstable( feature = "int_to_from_bytes" , issue = "52963" ) ]
3641+ #[ inline]
3642+ pub fn from_be_bytes( bytes: [ u8 ; mem:: size_of:: <Self >( ) ] ) -> Self {
3643+ Self :: from_be( Self :: from_ne_bytes( bytes) )
3644+ }
3645+
3646+ /// Create an integer value from its representation as a byte array in
3647+ /// little endian.
3648+ ///
3649+ /// # Examples
3650+ ///
3651+ /// ```
3652+ /// #![feature(int_to_from_bytes)]
3653+ ///
3654+ /// let int = i32::from_le_bytes([0x12, 0x34, 0x56, 0x78]);
3655+ /// assert_eq!(int, 0x78_56_34_12);
3656+ /// ```
3657+ #[ unstable( feature = "int_to_from_bytes" , issue = "52963" ) ]
3658+ #[ inline]
3659+ pub fn from_le_bytes( bytes: [ u8 ; mem:: size_of:: <Self >( ) ] ) -> Self {
3660+ Self :: from_le( Self :: from_ne_bytes( bytes) )
3661+ }
3662+
3663+ /// Create an integer value from its memory representation as a byte
3664+ /// array in native endianness.
35943665 ///
3595- /// The target platform’s native endianness is used.
3596- /// Portable code likely wants to use [`to_be`] or [`to_le`] after this.
3666+ /// As the target platform's native endianness is used, portable code
3667+ /// likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as
3668+ /// appropriate instead.
35973669 ///
3598- /// [`to_be `]: #method.to_be
3599- /// [`to_le `]: #method.to_le
3670+ /// [`from_be_bytes `]: #method.from_be_bytes
3671+ /// [`from_le_bytes `]: #method.from_le_bytes
36003672 ///
36013673 /// # Examples
36023674 ///
36033675 /// ```
36043676 /// #![feature(int_to_from_bytes)]
36053677 ///
3606- /// let int = u32 ::from_be(u32::from_bytes([0x12, 0x34, 0x56, 0x78 ]));
3607- /// assert_eq!(int, 0x1234_5678_u32 );
3678+ /// let int = i32 ::from_be(i32::from_ne_bytes([0x80, 0, 0, 0 ]));
3679+ /// assert_eq!(int, i32::min_value() );
36083680 /// ```
36093681 #[ unstable( feature = "int_to_from_bytes" , issue = "52963" ) ]
36103682 #[ inline]
3611- pub fn from_bytes ( bytes: [ u8 ; mem:: size_of:: <Self >( ) ] ) -> Self {
3683+ pub fn from_ne_bytes ( bytes: [ u8 ; mem:: size_of:: <Self >( ) ] ) -> Self {
36123684 unsafe { mem:: transmute( bytes) }
36133685 }
36143686 }
0 commit comments