@@ -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.
3572
3573
///
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
3575
3575
///
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.
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 = 0x12345678i32.to_le_bytes ();
3597
+ /// assert_eq!(bytes, [0x78, 0x56, 0x34, 0x12 ]);
3586
3598
/// ```
3587
3599
#[ unstable( feature = "int_to_from_bytes" , issue = "52963" ) ]
3588
3600
#[ 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 >( ) ] {
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
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.
3594
3665
///
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.
3597
3669
///
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
3600
3672
///
3601
3673
/// # Examples
3602
3674
///
3603
3675
/// ```
3604
3676
/// #![feature(int_to_from_bytes)]
3605
3677
///
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() );
3608
3680
/// ```
3609
3681
#[ unstable( feature = "int_to_from_bytes" , issue = "52963" ) ]
3610
3682
#[ 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 {
3612
3684
unsafe { mem:: transmute( bytes) }
3613
3685
}
3614
3686
}
0 commit comments