@@ -1891,7 +1891,7 @@ $EndFeature, "
18911891 /// ```
18921892 /// #![feature(int_to_from_bytes)]
18931893 ///
1894- /// let bytes = 0x12345678i32 .to_be_bytes();
1894+ /// let bytes = 0x12_34_56_78_i32 .to_be_bytes();
18951895 /// assert_eq!(bytes, [0x12, 0x34, 0x56, 0x78]);
18961896 /// ```
18971897 #[ unstable( feature = "int_to_from_bytes" , issue = "52963" ) ]
@@ -1908,7 +1908,7 @@ $EndFeature, "
19081908 /// ```
19091909 /// #![feature(int_to_from_bytes)]
19101910 ///
1911- /// let bytes = 0x12345678i32 .to_le_bytes();
1911+ /// let bytes = 0x12_34_56_78_i32 .to_le_bytes();
19121912 /// assert_eq!(bytes, [0x78, 0x56, 0x34, 0x12]);
19131913 /// ```
19141914 #[ unstable( feature = "int_to_from_bytes" , issue = "52963" ) ]
@@ -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.
3573+ ///
3574+ /// # Examples
35723575 ///
3573- /// The target platform’s native endianness is used.
3574- /// Portable code likely wants to use this after [`to_be`] or [`to_le`].
3576+ /// ```
3577+ /// #![feature(int_to_from_bytes)]
35753578 ///
3576- /// [`to_be`]: #method.to_be
3577- /// [`to_le`]: #method.to_le
3579+ /// let bytes = 0x12_34_56_78_i32.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 = 0x12_34_56_78_i32.to_le_bytes();
3597+ /// assert_eq!(bytes, [0x78, 0x56, 0x34, 0x12]);
3598+ /// ```
3599+ #[ unstable( feature = "int_to_from_bytes" , issue = "52963" ) ]
3600+ #[ inline]
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]);
35863622 /// ```
35873623 #[ unstable( feature = "int_to_from_bytes" , issue = "52963" ) ]
35883624 #[ inline]
3589- pub fn to_bytes ( self ) -> [ u8 ; mem:: size_of:: <Self >( ) ] {
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
35943633 ///
3595- /// The target platform’s native endianness is used.
3596- /// Portable code likely wants to use [`to_be`] or [`to_le`] after this.
3634+ /// ```
3635+ /// #![feature(int_to_from_bytes)]
35973636 ///
3598- /// [`to_be`]: #method.to_be
3599- /// [`to_le`]: #method.to_le
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.
36003648 ///
36013649 /// # Examples
36023650 ///
36033651 /// ```
36043652 /// #![feature(int_to_from_bytes)]
36053653 ///
3606- /// let int = u32::from_be(u32::from_bytes( [0x12, 0x34, 0x56, 0x78]) );
3607- /// assert_eq!(int, 0x1234_5678_u32 );
3654+ /// let int = i32::from_le_bytes( [0x12, 0x34, 0x56, 0x78]);
3655+ /// assert_eq!(int, 0x78_56_34_12 );
36083656 /// ```
36093657 #[ unstable( feature = "int_to_from_bytes" , issue = "52963" ) ]
36103658 #[ inline]
3611- pub fn from_bytes( bytes: [ u8 ; mem:: size_of:: <Self >( ) ] ) -> Self {
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.
3665+ ///
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.
3669+ ///
3670+ /// [`from_be_bytes`]: #method.from_be_bytes
3671+ /// [`from_le_bytes`]: #method.from_le_bytes
3672+ ///
3673+ /// # Examples
3674+ ///
3675+ /// ```
3676+ /// #![feature(int_to_from_bytes)]
3677+ ///
3678+ /// let int = i32::from_be(i32::from_ne_bytes([0x80, 0, 0, 0]));
3679+ /// assert_eq!(int, i32::min_value());
3680+ /// ```
3681+ #[ unstable( feature = "int_to_from_bytes" , issue = "52963" ) ]
3682+ #[ inline]
3683+ pub fn from_ne_bytes( bytes: [ u8 ; mem:: size_of:: <Self >( ) ] ) -> Self {
36123684 unsafe { mem:: transmute( bytes) }
36133685 }
36143686 }
0 commit comments