|
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;
|
@@ -1619,6 +1620,50 @@ $EndFeature, "
|
1619 | 1620 | #[inline]
|
1620 | 1621 | pub fn is_negative(self) -> bool { self < 0 }
|
1621 | 1622 | }
|
| 1623 | + |
| 1624 | + /// Return the memory representation of this integer as a byte array. |
| 1625 | + /// |
| 1626 | + /// The target platform’s native endianness is used. |
| 1627 | + /// Portable code likely wants to use this after [`to_be`] or [`to_le`]. |
| 1628 | + /// |
| 1629 | + /// [`to_be`]: #method.to_be |
| 1630 | + /// [`to_le`]: #method.to_le |
| 1631 | + /// |
| 1632 | + /// # Examples |
| 1633 | + /// |
| 1634 | + /// ``` |
| 1635 | + /// #![feature(int_to_from_bytes)] |
| 1636 | + /// |
| 1637 | + /// let bytes = i32::min_value().to_be().to_bytes(); |
| 1638 | + /// assert_eq!(bytes, [0x80, 0, 0, 0]); |
| 1639 | + /// ``` |
| 1640 | + #[unstable(feature = "int_to_from_bytes", issue = "49792")] |
| 1641 | + #[inline] |
| 1642 | + pub fn to_bytes(self) -> [u8; mem::size_of::<Self>()] { |
| 1643 | + unsafe { mem::transmute(self) } |
| 1644 | + } |
| 1645 | + |
| 1646 | + /// Create an integer value from its memory representation as a byte array. |
| 1647 | + /// |
| 1648 | + /// The target platform’s native endianness is used. |
| 1649 | + /// Portable code likely wants to use [`from_be`] or [`from_le`] after this. |
| 1650 | + /// |
| 1651 | + /// [`from_be`]: #method.from_be |
| 1652 | + /// [`from_le`]: #method.from_le |
| 1653 | + /// |
| 1654 | + /// # Examples |
| 1655 | + /// |
| 1656 | + /// ``` |
| 1657 | + /// #![feature(int_to_from_bytes)] |
| 1658 | + /// |
| 1659 | + /// let int = i32::from_be(i32::from_bytes([0x80, 0, 0, 0])); |
| 1660 | + /// assert_eq!(int, i32::min_value()); |
| 1661 | + /// ``` |
| 1662 | + #[unstable(feature = "int_to_from_bytes", issue = "49792")] |
| 1663 | + #[inline] |
| 1664 | + pub fn from_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self { |
| 1665 | + unsafe { mem::transmute(bytes) } |
| 1666 | + } |
1622 | 1667 | }
|
1623 | 1668 | }
|
1624 | 1669 |
|
@@ -2933,6 +2978,50 @@ $EndFeature, "
|
2933 | 2978 | self.one_less_than_next_power_of_two().checked_add(1)
|
2934 | 2979 | }
|
2935 | 2980 | }
|
| 2981 | + |
| 2982 | + /// Return the memory representation of this integer as a byte array. |
| 2983 | + /// |
| 2984 | + /// The target platform’s native endianness is used. |
| 2985 | + /// Portable code likely wants to use this after [`to_be`] or [`to_le`]. |
| 2986 | + /// |
| 2987 | + /// [`to_be`]: #method.to_be |
| 2988 | + /// [`to_le`]: #method.to_le |
| 2989 | + /// |
| 2990 | + /// # Examples |
| 2991 | + /// |
| 2992 | + /// ``` |
| 2993 | + /// #![feature(int_to_from_bytes)] |
| 2994 | + /// |
| 2995 | + /// let bytes = 0x1234_5678_u32.to_be().to_bytes(); |
| 2996 | + /// assert_eq!(bytes, [0x12, 0x34, 0x56, 0x78]); |
| 2997 | + /// ``` |
| 2998 | + #[unstable(feature = "int_to_from_bytes", issue = "49792")] |
| 2999 | + #[inline] |
| 3000 | + pub fn to_bytes(self) -> [u8; mem::size_of::<Self>()] { |
| 3001 | + unsafe { mem::transmute(self) } |
| 3002 | + } |
| 3003 | + |
| 3004 | + /// Create an integer value from its memory representation as a byte array. |
| 3005 | + /// |
| 3006 | + /// The target platform’s native endianness is used. |
| 3007 | + /// Portable code likely wants to use [`to_be`] or [`to_le`] after this. |
| 3008 | + /// |
| 3009 | + /// [`to_be`]: #method.to_be |
| 3010 | + /// [`to_le`]: #method.to_le |
| 3011 | + /// |
| 3012 | + /// # Examples |
| 3013 | + /// |
| 3014 | + /// ``` |
| 3015 | + /// #![feature(int_to_from_bytes)] |
| 3016 | + /// |
| 3017 | + /// let int = u32::from_be(u32::from_bytes([0x12, 0x34, 0x56, 0x78])); |
| 3018 | + /// assert_eq!(int, 0x1234_5678_u32); |
| 3019 | + /// ``` |
| 3020 | + #[unstable(feature = "int_to_from_bytes", issue = "49792")] |
| 3021 | + #[inline] |
| 3022 | + pub fn from_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self { |
| 3023 | + unsafe { mem::transmute(bytes) } |
| 3024 | + } |
2936 | 3025 | }
|
2937 | 3026 | }
|
2938 | 3027 |
|
|
0 commit comments