|
15 | 15 | use convert::TryFrom;
|
16 | 16 | use fmt;
|
17 | 17 | use intrinsics;
|
| 18 | +use mem; |
18 | 19 | #[allow(deprecated)] use nonzero::NonZero;
|
19 | 20 | use ops;
|
20 | 21 | use str::FromStr;
|
@@ -1868,6 +1869,50 @@ $EndFeature, "
|
1868 | 1869 | #[inline]
|
1869 | 1870 | pub fn is_negative(self) -> bool { self < 0 }
|
1870 | 1871 | }
|
| 1872 | + |
| 1873 | + /// Return the memory representation of this integer as a byte array. |
| 1874 | + /// |
| 1875 | + /// The target platform’s native endianness is used. |
| 1876 | + /// Portable code likely wants to use this after [`to_be`] or [`to_le`]. |
| 1877 | + /// |
| 1878 | + /// [`to_be`]: #method.to_be |
| 1879 | + /// [`to_le`]: #method.to_le |
| 1880 | + /// |
| 1881 | + /// # Examples |
| 1882 | + /// |
| 1883 | + /// ``` |
| 1884 | + /// #![feature(int_to_from_bytes)] |
| 1885 | + /// |
| 1886 | + /// let bytes = i32::min_value().to_be().to_bytes(); |
| 1887 | + /// assert_eq!(bytes, [0x80, 0, 0, 0]); |
| 1888 | + /// ``` |
| 1889 | + #[unstable(feature = "int_to_from_bytes", issue = "49792")] |
| 1890 | + #[inline] |
| 1891 | + pub fn to_bytes(self) -> [u8; mem::size_of::<Self>()] { |
| 1892 | + unsafe { mem::transmute(self) } |
| 1893 | + } |
| 1894 | + |
| 1895 | + /// Create an integer value from its memory representation as a byte array. |
| 1896 | + /// |
| 1897 | + /// The target platform’s native endianness is used. |
| 1898 | + /// Portable code likely wants to use [`from_be`] or [`from_le`] after this. |
| 1899 | + /// |
| 1900 | + /// [`from_be`]: #method.from_be |
| 1901 | + /// [`from_le`]: #method.from_le |
| 1902 | + /// |
| 1903 | + /// # Examples |
| 1904 | + /// |
| 1905 | + /// ``` |
| 1906 | + /// #![feature(int_to_from_bytes)] |
| 1907 | + /// |
| 1908 | + /// let int = i32::from_be(i32::from_bytes([0x80, 0, 0, 0])); |
| 1909 | + /// assert_eq!(int, i32::min_value()); |
| 1910 | + /// ``` |
| 1911 | + #[unstable(feature = "int_to_from_bytes", issue = "49792")] |
| 1912 | + #[inline] |
| 1913 | + pub fn from_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self { |
| 1914 | + unsafe { mem::transmute(bytes) } |
| 1915 | + } |
1871 | 1916 | }
|
1872 | 1917 | }
|
1873 | 1918 |
|
@@ -3373,6 +3418,50 @@ $EndFeature, "
|
3373 | 3418 | self.one_less_than_next_power_of_two().checked_add(1)
|
3374 | 3419 | }
|
3375 | 3420 | }
|
| 3421 | + |
| 3422 | + /// Return the memory representation of this integer as a byte array. |
| 3423 | + /// |
| 3424 | + /// The target platform’s native endianness is used. |
| 3425 | + /// Portable code likely wants to use this after [`to_be`] or [`to_le`]. |
| 3426 | + /// |
| 3427 | + /// [`to_be`]: #method.to_be |
| 3428 | + /// [`to_le`]: #method.to_le |
| 3429 | + /// |
| 3430 | + /// # Examples |
| 3431 | + /// |
| 3432 | + /// ``` |
| 3433 | + /// #![feature(int_to_from_bytes)] |
| 3434 | + /// |
| 3435 | + /// let bytes = 0x1234_5678_u32.to_be().to_bytes(); |
| 3436 | + /// assert_eq!(bytes, [0x12, 0x34, 0x56, 0x78]); |
| 3437 | + /// ``` |
| 3438 | + #[unstable(feature = "int_to_from_bytes", issue = "49792")] |
| 3439 | + #[inline] |
| 3440 | + pub fn to_bytes(self) -> [u8; mem::size_of::<Self>()] { |
| 3441 | + unsafe { mem::transmute(self) } |
| 3442 | + } |
| 3443 | + |
| 3444 | + /// Create an integer value from its memory representation as a byte array. |
| 3445 | + /// |
| 3446 | + /// The target platform’s native endianness is used. |
| 3447 | + /// Portable code likely wants to use [`to_be`] or [`to_le`] after this. |
| 3448 | + /// |
| 3449 | + /// [`to_be`]: #method.to_be |
| 3450 | + /// [`to_le`]: #method.to_le |
| 3451 | + /// |
| 3452 | + /// # Examples |
| 3453 | + /// |
| 3454 | + /// ``` |
| 3455 | + /// #![feature(int_to_from_bytes)] |
| 3456 | + /// |
| 3457 | + /// let int = u32::from_be(u32::from_bytes([0x12, 0x34, 0x56, 0x78])); |
| 3458 | + /// assert_eq!(int, 0x1234_5678_u32); |
| 3459 | + /// ``` |
| 3460 | + #[unstable(feature = "int_to_from_bytes", issue = "49792")] |
| 3461 | + #[inline] |
| 3462 | + pub fn from_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self { |
| 3463 | + unsafe { mem::transmute(bytes) } |
| 3464 | + } |
3376 | 3465 | }
|
3377 | 3466 | }
|
3378 | 3467 |
|
|
0 commit comments