@@ -1892,47 +1892,119 @@ $EndFeature, "
1892
1892
pub fn is_negative( self ) -> bool { self < 0 }
1893
1893
}
1894
1894
1895
- /// Return the memory representation of this integer as a byte array.
1895
+ /// Return the memory representation of this integer as a byte array in
1896
+ /// big-endian (network) byte order.
1896
1897
///
1897
- /// The target platform’s native endianness is used.
1898
- /// Portable code likely wants to use this after [`to_be`] or [`to_le`].
1898
+ /// # Examples
1899
1899
///
1900
- /// [`to_be`]: #method.to_be
1901
- /// [`to_le`]: #method.to_le
1900
+ /// ```
1901
+ /// #![feature(int_to_from_bytes)]
1902
+ ///
1903
+ /// let bytes = 0x12345678i32.to_be_bytes();
1904
+ /// assert_eq!(bytes, [0x12, 0x34, 0x56, 0x78]);
1905
+ /// ```
1906
+ #[ unstable( feature = "int_to_from_bytes" , issue = "49792" ) ]
1907
+ #[ inline]
1908
+ pub fn to_be_bytes( self ) -> [ u8 ; mem:: size_of:: <Self >( ) ] {
1909
+ self . to_be( ) . to_ne_bytes( )
1910
+ }
1911
+
1912
+ /// Return the memory representation of this integer as a byte array in
1913
+ /// little-endian byte order.
1914
+ ///
1915
+ /// # Examples
1916
+ ///
1917
+ /// ```
1918
+ /// #![feature(int_to_from_bytes)]
1919
+ ///
1920
+ /// let bytes = 0x12345678i32.to_le_bytes();
1921
+ /// assert_eq!(bytes, [0x78, 0x56, 0x34, 0x12]);
1922
+ /// ```
1923
+ #[ unstable( feature = "int_to_from_bytes" , issue = "49792" ) ]
1924
+ #[ inline]
1925
+ pub fn to_le_bytes( self ) -> [ u8 ; mem:: size_of:: <Self >( ) ] {
1926
+ self . to_le( ) . to_ne_bytes( )
1927
+ }
1928
+
1929
+ /// Return the memory representation of this integer as a byte array in
1930
+ /// native byte order.
1931
+ ///
1932
+ /// As the target platform's native endianness is used, portable code
1933
+ /// should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate,
1934
+ /// instead.
1935
+ ///
1936
+ /// [`to_be_bytes`]: #method.to_be_bytes
1937
+ /// [`to_le_bytes`]: #method.to_le_bytes
1902
1938
///
1903
1939
/// # Examples
1904
1940
///
1905
1941
/// ```
1906
1942
/// #![feature(int_to_from_bytes)]
1907
1943
///
1908
- /// let bytes = i32::min_value().to_be().to_bytes ();
1944
+ /// let bytes = i32::min_value().to_be().to_ne_bytes ();
1909
1945
/// assert_eq!(bytes, [0x80, 0, 0, 0]);
1910
1946
/// ```
1911
1947
#[ unstable( feature = "int_to_from_bytes" , issue = "49792" ) ]
1912
1948
#[ inline]
1913
- pub fn to_bytes ( self ) -> [ u8 ; mem:: size_of:: <Self >( ) ] {
1949
+ pub fn to_ne_bytes ( self ) -> [ u8 ; mem:: size_of:: <Self >( ) ] {
1914
1950
unsafe { mem:: transmute( self ) }
1915
1951
}
1916
1952
1917
- /// Create an integer value from its memory representation as a byte array.
1953
+ /// Create an integer value from its representation as a byte array in
1954
+ /// big endian.
1918
1955
///
1919
- /// The target platform’s native endianness is used.
1920
- /// Portable code likely wants to use [`from_be`] or [`from_le`] after this.
1956
+ /// # Examples
1957
+ ///
1958
+ /// ```
1959
+ /// #![feature(int_to_from_bytes)]
1921
1960
///
1922
- /// [`from_be`]: #method.from_be
1923
- /// [`from_le`]: #method.from_le
1961
+ /// let int = i32::from_be_bytes([0x12, 0x34, 0x56, 0x78]);
1962
+ /// assert_eq!(int, 0x12_34_56_78);
1963
+ /// ```
1964
+ #[ unstable( feature = "int_to_from_bytes" , issue = "49792" ) ]
1965
+ #[ inline]
1966
+ pub fn from_be_bytes( bytes: [ u8 ; mem:: size_of:: <Self >( ) ] ) -> Self {
1967
+ Self :: from_be( Self :: from_ne_bytes( bytes) )
1968
+ }
1969
+
1970
+ /// Create an integer value from its representation as a byte array in
1971
+ /// little endian.
1924
1972
///
1925
1973
/// # Examples
1926
1974
///
1927
1975
/// ```
1928
1976
/// #![feature(int_to_from_bytes)]
1929
1977
///
1930
- /// let int = i32::from_be(i32::from_bytes([0x80, 0, 0, 0]));
1978
+ /// let int = i32::from_le_bytes([0x12, 0x34, 0x56, 0x78]);
1979
+ /// assert_eq!(int, 0x78_56_34_12);
1980
+ /// ```
1981
+ #[ unstable( feature = "int_to_from_bytes" , issue = "49792" ) ]
1982
+ #[ inline]
1983
+ pub fn from_le_bytes( bytes: [ u8 ; mem:: size_of:: <Self >( ) ] ) -> Self {
1984
+ Self :: from_le( Self :: from_ne_bytes( bytes) )
1985
+ }
1986
+
1987
+ /// Create an integer value from its memory representation as a byte
1988
+ /// array in native endianness.
1989
+ ///
1990
+ /// As the target platform's native endianness is used, portable code
1991
+ /// likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as
1992
+ /// appropriate instead.
1993
+ ///
1994
+ /// [`from_be_bytes`]: #method.from_be_bytes
1995
+ /// [`from_le_bytes`]: #method.from_le_bytes
1996
+ ///
1997
+ /// # Examples
1998
+ ///
1999
+ /// ```
2000
+ /// #![feature(int_to_from_bytes)]
2001
+ ///
2002
+ /// let int = i32::from_be(i32::from_ne_bytes([0x80, 0, 0, 0]));
1931
2003
/// assert_eq!(int, i32::min_value());
1932
2004
/// ```
1933
2005
#[ unstable( feature = "int_to_from_bytes" , issue = "49792" ) ]
1934
2006
#[ inline]
1935
- pub fn from_bytes ( bytes: [ u8 ; mem:: size_of:: <Self >( ) ] ) -> Self {
2007
+ pub fn from_ne_bytes ( bytes: [ u8 ; mem:: size_of:: <Self >( ) ] ) -> Self {
1936
2008
unsafe { mem:: transmute( bytes) }
1937
2009
}
1938
2010
}
0 commit comments