@@ -1891,7 +1891,7 @@ $EndFeature, "
1891
1891
/// ```
1892
1892
/// #![feature(int_to_from_bytes)]
1893
1893
///
1894
- /// let bytes = 0x12345678i32 .to_be_bytes();
1894
+ /// let bytes = 0x12_34_56_78_i32 .to_be_bytes();
1895
1895
/// assert_eq!(bytes, [0x12, 0x34, 0x56, 0x78]);
1896
1896
/// ```
1897
1897
#[ unstable( feature = "int_to_from_bytes" , issue = "52963" ) ]
@@ -1908,7 +1908,7 @@ $EndFeature, "
1908
1908
/// ```
1909
1909
/// #![feature(int_to_from_bytes)]
1910
1910
///
1911
- /// let bytes = 0x12345678i32 .to_le_bytes();
1911
+ /// let bytes = 0x12_34_56_78_i32 .to_le_bytes();
1912
1912
/// assert_eq!(bytes, [0x78, 0x56, 0x34, 0x12]);
1913
1913
/// ```
1914
1914
#[ unstable( feature = "int_to_from_bytes" , issue = "52963" ) ]
@@ -3568,47 +3568,119 @@ $EndFeature, "
3568
3568
}
3569
3569
}
3570
3570
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
3572
3575
///
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)]
3575
3578
///
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.
3578
3590
///
3579
3591
/// # Examples
3580
3592
///
3581
3593
/// ```
3582
3594
/// #![feature(int_to_from_bytes)]
3583
3595
///
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]);
3586
3622
/// ```
3587
3623
#[ unstable( feature = "int_to_from_bytes" , issue = "52963" ) ]
3588
3624
#[ inline]
3589
- pub fn to_bytes ( self ) -> [ u8 ; mem:: size_of:: <Self >( ) ] {
3625
+ pub fn to_ne_bytes ( self ) -> [ u8 ; mem:: size_of:: <Self >( ) ] {
3590
3626
unsafe { mem:: transmute( self ) }
3591
3627
}
3592
3628
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
3594
3633
///
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)]
3597
3636
///
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.
3600
3648
///
3601
3649
/// # Examples
3602
3650
///
3603
3651
/// ```
3604
3652
/// #![feature(int_to_from_bytes)]
3605
3653
///
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 );
3608
3656
/// ```
3609
3657
#[ unstable( feature = "int_to_from_bytes" , issue = "52963" ) ]
3610
3658
#[ 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 {
3612
3684
unsafe { mem:: transmute( bytes) }
3613
3685
}
3614
3686
}
0 commit comments