Individual docs for {from,to}_*_bytes
This commit is contained in:
parent
d8af8b66d9
commit
90b7c5acbc
1 changed files with 314 additions and 251 deletions
|
@ -192,7 +192,7 @@ mod wrapping;
|
|||
macro_rules! int_impl {
|
||||
($SelfT:ty, $ActualT:ident, $UnsignedT:ty, $BITS:expr, $Min:expr, $Max:expr, $Feature:expr,
|
||||
$EndFeature:expr, $rot:expr, $rot_op:expr, $rot_result:expr, $swap_op:expr, $swapped:expr,
|
||||
$reversed:expr) => {
|
||||
$reversed:expr, $le_bytes:expr, $be_bytes:expr) => {
|
||||
doc_comment! {
|
||||
concat!("Returns the smallest value that can be represented by this integer type.
|
||||
|
||||
|
@ -2063,23 +2063,25 @@ $EndFeature, "
|
|||
self.to_be().to_ne_bytes()
|
||||
}
|
||||
|
||||
/// Return the memory representation of this integer as a byte array in
|
||||
/// big-endian (network) byte order.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(int_to_from_bytes)]
|
||||
///
|
||||
/// let bytes = 0x12_34_56_78_i32.to_be_bytes();
|
||||
/// assert_eq!(bytes, [0x12, 0x34, 0x56, 0x78]);
|
||||
/// ```
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn to_be_bytes(self) -> [u8; mem::size_of::<Self>()] {
|
||||
self.to_be().to_ne_bytes()
|
||||
doc_comment! {
|
||||
concat!("Return the memory representation of this integer as a byte array in
|
||||
big-endian (network) byte order.
|
||||
|
||||
# Examples
|
||||
|
||||
```
|
||||
#![feature(int_to_from_bytes)]
|
||||
|
||||
let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes();
|
||||
assert_eq!(bytes, ", $be_bytes, ");
|
||||
```"),
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn to_be_bytes(self) -> [u8; mem::size_of::<Self>()] {
|
||||
self.to_be().to_ne_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
/// no docs here
|
||||
|
@ -2090,23 +2092,25 @@ $EndFeature, "
|
|||
self.to_le().to_ne_bytes()
|
||||
}
|
||||
|
||||
/// Return the memory representation of this integer as a byte array in
|
||||
/// little-endian byte order.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(int_to_from_bytes)]
|
||||
///
|
||||
/// let bytes = 0x12_34_56_78_i32.to_le_bytes();
|
||||
/// assert_eq!(bytes, [0x78, 0x56, 0x34, 0x12]);
|
||||
/// ```
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn to_le_bytes(self) -> [u8; mem::size_of::<Self>()] {
|
||||
self.to_le().to_ne_bytes()
|
||||
doc_comment! {
|
||||
concat!("Return the memory representation of this integer as a byte array in
|
||||
little-endian byte order.
|
||||
|
||||
# Examples
|
||||
|
||||
```
|
||||
#![feature(int_to_from_bytes)]
|
||||
|
||||
let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes();
|
||||
assert_eq!(bytes, ", $le_bytes, ");
|
||||
```"),
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn to_le_bytes(self) -> [u8; mem::size_of::<Self>()] {
|
||||
self.to_le().to_ne_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
/// no docs here
|
||||
|
@ -2117,30 +2121,37 @@ $EndFeature, "
|
|||
unsafe { mem::transmute(self) }
|
||||
}
|
||||
|
||||
/// Return the memory representation of this integer as a byte array in
|
||||
/// native byte order.
|
||||
///
|
||||
/// As the target platform's native endianness is used, portable code
|
||||
/// should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate,
|
||||
/// instead.
|
||||
///
|
||||
/// [`to_be_bytes`]: #method.to_be_bytes
|
||||
/// [`to_le_bytes`]: #method.to_le_bytes
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(int_to_from_bytes)]
|
||||
///
|
||||
/// let bytes = i32::min_value().to_be().to_ne_bytes();
|
||||
/// assert_eq!(bytes, [0x80, 0, 0, 0]);
|
||||
/// ```
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] {
|
||||
unsafe { mem::transmute(self) }
|
||||
doc_comment! {
|
||||
concat!("
|
||||
Return the memory representation of this integer as a byte array in
|
||||
native byte order.
|
||||
|
||||
As the target platform's native endianness is used, portable code
|
||||
should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate,
|
||||
instead.
|
||||
|
||||
[`to_be_bytes`]: #method.to_be_bytes
|
||||
[`to_le_bytes`]: #method.to_le_bytes
|
||||
|
||||
# Examples
|
||||
|
||||
```
|
||||
#![feature(int_to_from_bytes)]
|
||||
|
||||
let bytes = ", $swap_op, stringify!($SelfT), ".to_ne_bytes();
|
||||
assert_eq!(bytes, if cfg!(target_endian = \"big\") {
|
||||
", $be_bytes, "
|
||||
} else {
|
||||
", $le_bytes, "
|
||||
});
|
||||
```"),
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] {
|
||||
unsafe { mem::transmute(self) }
|
||||
}
|
||||
}
|
||||
|
||||
/// no docs here
|
||||
|
@ -2151,23 +2162,25 @@ $EndFeature, "
|
|||
Self::from_be(Self::from_ne_bytes(bytes))
|
||||
}
|
||||
|
||||
/// Create an integer value from its representation as a byte array in
|
||||
/// big endian.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(int_to_from_bytes)]
|
||||
///
|
||||
/// let int = i32::from_be_bytes([0x12, 0x34, 0x56, 0x78]);
|
||||
/// assert_eq!(int, 0x12_34_56_78);
|
||||
/// ```
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
|
||||
Self::from_be(Self::from_ne_bytes(bytes))
|
||||
doc_comment! {
|
||||
concat!("Create an integer value from its representation as a byte array in
|
||||
big endian.
|
||||
|
||||
# Examples
|
||||
|
||||
```
|
||||
#![feature(int_to_from_bytes)]
|
||||
|
||||
let value = ", stringify!($SelfT), "::from_be_bytes(", $be_bytes, ");
|
||||
assert_eq!(value, ", $swap_op, ");
|
||||
```"),
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
|
||||
Self::from_be(Self::from_ne_bytes(bytes))
|
||||
}
|
||||
}
|
||||
|
||||
/// no docs here
|
||||
|
@ -2178,23 +2191,26 @@ $EndFeature, "
|
|||
Self::from_le(Self::from_ne_bytes(bytes))
|
||||
}
|
||||
|
||||
/// Create an integer value from its representation as a byte array in
|
||||
/// little endian.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(int_to_from_bytes)]
|
||||
///
|
||||
/// let int = i32::from_le_bytes([0x12, 0x34, 0x56, 0x78]);
|
||||
/// assert_eq!(int, 0x78_56_34_12);
|
||||
/// ```
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
|
||||
Self::from_le(Self::from_ne_bytes(bytes))
|
||||
doc_comment! {
|
||||
concat!("
|
||||
Create an integer value from its representation as a byte array in
|
||||
little endian.
|
||||
|
||||
# Examples
|
||||
|
||||
```
|
||||
#![feature(int_to_from_bytes)]
|
||||
|
||||
let value = ", stringify!($SelfT), "::from_le_bytes(", $le_bytes, ");
|
||||
assert_eq!(value, ", $swap_op, ");
|
||||
```"),
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
|
||||
Self::from_le(Self::from_ne_bytes(bytes))
|
||||
}
|
||||
}
|
||||
|
||||
/// no docs here
|
||||
|
@ -2205,56 +2221,65 @@ $EndFeature, "
|
|||
unsafe { mem::transmute(bytes) }
|
||||
}
|
||||
|
||||
/// Create an integer value from its memory representation as a byte
|
||||
/// array in native endianness.
|
||||
///
|
||||
/// As the target platform's native endianness is used, portable code
|
||||
/// likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as
|
||||
/// appropriate instead.
|
||||
///
|
||||
/// [`from_be_bytes`]: #method.from_be_bytes
|
||||
/// [`from_le_bytes`]: #method.from_le_bytes
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(int_to_from_bytes)]
|
||||
///
|
||||
/// let int = i32::from_be(i32::from_ne_bytes([0x80, 0, 0, 0]));
|
||||
/// assert_eq!(int, i32::min_value());
|
||||
/// ```
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
|
||||
unsafe { mem::transmute(bytes) }
|
||||
doc_comment! {
|
||||
concat!("Create an integer value from its memory representation as a byte
|
||||
array in native endianness.
|
||||
|
||||
As the target platform's native endianness is used, portable code
|
||||
likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as
|
||||
appropriate instead.
|
||||
|
||||
[`from_be_bytes`]: #method.from_be_bytes
|
||||
[`from_le_bytes`]: #method.from_le_bytes
|
||||
|
||||
# Examples
|
||||
|
||||
```
|
||||
#![feature(int_to_from_bytes)]
|
||||
|
||||
let value = ", stringify!($SelfT), "::from_ne_bytes(if cfg!(target_endian = \"big\") {
|
||||
", $be_bytes, "
|
||||
} else {
|
||||
", $le_bytes, "
|
||||
});
|
||||
assert_eq!(value, ", $swap_op, ");
|
||||
```"),
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
|
||||
unsafe { mem::transmute(bytes) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[lang = "i8"]
|
||||
impl i8 {
|
||||
int_impl! { i8, i8, u8, 8, -128, 127, "", "", 2, "-0x7e", "0xa", "0x12", "0x12", "0x48" }
|
||||
int_impl! { i8, i8, u8, 8, -128, 127, "", "", 2, "-0x7e", "0xa", "0x12", "0x12", "0x48",
|
||||
"[0x12]", "[0x12]" }
|
||||
}
|
||||
|
||||
#[lang = "i16"]
|
||||
impl i16 {
|
||||
int_impl! { i16, i16, u16, 16, -32768, 32767, "", "", 4, "-0x5ffd", "0x3a", "0x1234", "0x3412",
|
||||
"0x2c48" }
|
||||
"0x2c48", "[0x34, 0x12]", "[0x12, 0x34]" }
|
||||
}
|
||||
|
||||
#[lang = "i32"]
|
||||
impl i32 {
|
||||
int_impl! { i32, i32, u32, 32, -2147483648, 2147483647, "", "", 8, "0x10000b3", "0xb301",
|
||||
"0x12345678", "0x78563412", "0x1e6a2c48" }
|
||||
"0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]",
|
||||
"[0x12, 0x34, 0x56, 0x78]" }
|
||||
}
|
||||
|
||||
#[lang = "i64"]
|
||||
impl i64 {
|
||||
int_impl! { i64, i64, u64, 64, -9223372036854775808, 9223372036854775807, "", "", 12,
|
||||
"0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
|
||||
"0x6a2c48091e6a2c48" }
|
||||
"0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
|
||||
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]" }
|
||||
}
|
||||
|
||||
#[lang = "i128"]
|
||||
|
@ -2262,22 +2287,26 @@ impl i128 {
|
|||
int_impl! { i128, i128, u128, 128, -170141183460469231731687303715884105728,
|
||||
170141183460469231731687303715884105727, "", "", 16,
|
||||
"0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
|
||||
"0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48"
|
||||
}
|
||||
"0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
|
||||
"[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
|
||||
0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
|
||||
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
|
||||
0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]" }
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "16")]
|
||||
#[lang = "isize"]
|
||||
impl isize {
|
||||
int_impl! { isize, i16, u16, 16, -32768, 32767, "", "", 4, "-0x5ffd", "0x3a", "0x1234",
|
||||
"0x3412", "0x2c48" }
|
||||
"0x3412", "0x2c48", "[0x34, 0x12]", "[0x12, 0x34]" }
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
#[lang = "isize"]
|
||||
impl isize {
|
||||
int_impl! { isize, i32, u32, 32, -2147483648, 2147483647, "", "", 8, "0x10000b3", "0xb301",
|
||||
"0x12345678", "0x78563412", "0x1e6a2c48" }
|
||||
"0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]",
|
||||
"[0x12, 0x34, 0x56, 0x78]" }
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
|
@ -2285,7 +2314,8 @@ impl isize {
|
|||
impl isize {
|
||||
int_impl! { isize, i64, u64, 64, -9223372036854775808, 9223372036854775807, "", "",
|
||||
12, "0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
|
||||
"0x6a2c48091e6a2c48" }
|
||||
"0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
|
||||
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]" }
|
||||
}
|
||||
|
||||
// Emits the correct `cttz` call, depending on the size of the type.
|
||||
|
@ -2305,7 +2335,7 @@ macro_rules! uint_cttz_call {
|
|||
macro_rules! uint_impl {
|
||||
($SelfT:ty, $ActualT:ty, $BITS:expr, $MaxV:expr, $Feature:expr, $EndFeature:expr,
|
||||
$rot:expr, $rot_op:expr, $rot_result:expr, $swap_op:expr, $swapped:expr,
|
||||
$reversed:expr ) => {
|
||||
$reversed:expr, $le_bytes:expr, $be_bytes:expr) => {
|
||||
doc_comment! {
|
||||
concat!("Returns the smallest value that can be represented by this integer type.
|
||||
|
||||
|
@ -3960,23 +3990,25 @@ $EndFeature, "
|
|||
self.to_be().to_ne_bytes()
|
||||
}
|
||||
|
||||
/// Return the memory representation of this integer as a byte array in
|
||||
/// big-endian (network) byte order.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(int_to_from_bytes)]
|
||||
///
|
||||
/// let bytes = 0x12_34_56_78_i32.to_be_bytes();
|
||||
/// assert_eq!(bytes, [0x12, 0x34, 0x56, 0x78]);
|
||||
/// ```
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn to_be_bytes(self) -> [u8; mem::size_of::<Self>()] {
|
||||
self.to_be().to_ne_bytes()
|
||||
doc_comment! {
|
||||
concat!("Return the memory representation of this integer as a byte array in
|
||||
big-endian (network) byte order.
|
||||
|
||||
# Examples
|
||||
|
||||
```
|
||||
#![feature(int_to_from_bytes)]
|
||||
|
||||
let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes();
|
||||
assert_eq!(bytes, ", $be_bytes, ");
|
||||
```"),
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn to_be_bytes(self) -> [u8; mem::size_of::<Self>()] {
|
||||
self.to_be().to_ne_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
/// no docs here
|
||||
|
@ -3987,23 +4019,25 @@ $EndFeature, "
|
|||
self.to_le().to_ne_bytes()
|
||||
}
|
||||
|
||||
/// Return the memory representation of this integer as a byte array in
|
||||
/// little-endian byte order.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(int_to_from_bytes)]
|
||||
///
|
||||
/// let bytes = 0x12_34_56_78_i32.to_le_bytes();
|
||||
/// assert_eq!(bytes, [0x78, 0x56, 0x34, 0x12]);
|
||||
/// ```
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn to_le_bytes(self) -> [u8; mem::size_of::<Self>()] {
|
||||
self.to_le().to_ne_bytes()
|
||||
doc_comment! {
|
||||
concat!("Return the memory representation of this integer as a byte array in
|
||||
little-endian byte order.
|
||||
|
||||
# Examples
|
||||
|
||||
```
|
||||
#![feature(int_to_from_bytes)]
|
||||
|
||||
let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes();
|
||||
assert_eq!(bytes, ", $le_bytes, ");
|
||||
```"),
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn to_le_bytes(self) -> [u8; mem::size_of::<Self>()] {
|
||||
self.to_le().to_ne_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
/// no docs here
|
||||
|
@ -4014,30 +4048,37 @@ $EndFeature, "
|
|||
unsafe { mem::transmute(self) }
|
||||
}
|
||||
|
||||
/// Return the memory representation of this integer as a byte array in
|
||||
/// native byte order.
|
||||
///
|
||||
/// As the target platform's native endianness is used, portable code
|
||||
/// should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate,
|
||||
/// instead.
|
||||
///
|
||||
/// [`to_be_bytes`]: #method.to_be_bytes
|
||||
/// [`to_le_bytes`]: #method.to_le_bytes
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(int_to_from_bytes)]
|
||||
///
|
||||
/// let bytes = i32::min_value().to_be().to_ne_bytes();
|
||||
/// assert_eq!(bytes, [0x80, 0, 0, 0]);
|
||||
/// ```
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] {
|
||||
unsafe { mem::transmute(self) }
|
||||
doc_comment! {
|
||||
concat!("
|
||||
Return the memory representation of this integer as a byte array in
|
||||
native byte order.
|
||||
|
||||
As the target platform's native endianness is used, portable code
|
||||
should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate,
|
||||
instead.
|
||||
|
||||
[`to_be_bytes`]: #method.to_be_bytes
|
||||
[`to_le_bytes`]: #method.to_le_bytes
|
||||
|
||||
# Examples
|
||||
|
||||
```
|
||||
#![feature(int_to_from_bytes)]
|
||||
|
||||
let bytes = ", $swap_op, stringify!($SelfT), ".to_ne_bytes();
|
||||
assert_eq!(bytes, if cfg!(target_endian = \"big\") {
|
||||
", $be_bytes, "
|
||||
} else {
|
||||
", $le_bytes, "
|
||||
});
|
||||
```"),
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] {
|
||||
unsafe { mem::transmute(self) }
|
||||
}
|
||||
}
|
||||
|
||||
/// no docs here
|
||||
|
@ -4048,23 +4089,25 @@ $EndFeature, "
|
|||
Self::from_be(Self::from_ne_bytes(bytes))
|
||||
}
|
||||
|
||||
/// Create an integer value from its representation as a byte array in
|
||||
/// big endian.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(int_to_from_bytes)]
|
||||
///
|
||||
/// let int = i32::from_be_bytes([0x12, 0x34, 0x56, 0x78]);
|
||||
/// assert_eq!(int, 0x12_34_56_78);
|
||||
/// ```
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
|
||||
Self::from_be(Self::from_ne_bytes(bytes))
|
||||
doc_comment! {
|
||||
concat!("Create an integer value from its representation as a byte array in
|
||||
big endian.
|
||||
|
||||
# Examples
|
||||
|
||||
```
|
||||
#![feature(int_to_from_bytes)]
|
||||
|
||||
let value = ", stringify!($SelfT), "::from_be_bytes(", $be_bytes, ");
|
||||
assert_eq!(value, ", $swap_op, ");
|
||||
```"),
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
|
||||
Self::from_be(Self::from_ne_bytes(bytes))
|
||||
}
|
||||
}
|
||||
|
||||
/// no docs here
|
||||
|
@ -4075,23 +4118,26 @@ $EndFeature, "
|
|||
Self::from_le(Self::from_ne_bytes(bytes))
|
||||
}
|
||||
|
||||
/// Create an integer value from its representation as a byte array in
|
||||
/// little endian.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(int_to_from_bytes)]
|
||||
///
|
||||
/// let int = i32::from_le_bytes([0x12, 0x34, 0x56, 0x78]);
|
||||
/// assert_eq!(int, 0x78_56_34_12);
|
||||
/// ```
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
|
||||
Self::from_le(Self::from_ne_bytes(bytes))
|
||||
doc_comment! {
|
||||
concat!("
|
||||
Create an integer value from its representation as a byte array in
|
||||
little endian.
|
||||
|
||||
# Examples
|
||||
|
||||
```
|
||||
#![feature(int_to_from_bytes)]
|
||||
|
||||
let value = ", stringify!($SelfT), "::from_le_bytes(", $le_bytes, ");
|
||||
assert_eq!(value, ", $swap_op, ");
|
||||
```"),
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
|
||||
Self::from_le(Self::from_ne_bytes(bytes))
|
||||
}
|
||||
}
|
||||
|
||||
/// no docs here
|
||||
|
@ -4102,37 +4148,44 @@ $EndFeature, "
|
|||
unsafe { mem::transmute(bytes) }
|
||||
}
|
||||
|
||||
/// Create an integer value from its memory representation as a byte
|
||||
/// array in native endianness.
|
||||
///
|
||||
/// As the target platform's native endianness is used, portable code
|
||||
/// likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as
|
||||
/// appropriate instead.
|
||||
///
|
||||
/// [`from_be_bytes`]: #method.from_be_bytes
|
||||
/// [`from_le_bytes`]: #method.from_le_bytes
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(int_to_from_bytes)]
|
||||
///
|
||||
/// let int = i32::from_be(i32::from_ne_bytes([0x80, 0, 0, 0]));
|
||||
/// assert_eq!(int, i32::min_value());
|
||||
/// ```
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
|
||||
unsafe { mem::transmute(bytes) }
|
||||
doc_comment! {
|
||||
concat!("Create an integer value from its memory representation as a byte
|
||||
array in native endianness.
|
||||
|
||||
As the target platform's native endianness is used, portable code
|
||||
likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as
|
||||
appropriate instead.
|
||||
|
||||
[`from_be_bytes`]: #method.from_be_bytes
|
||||
[`from_le_bytes`]: #method.from_le_bytes
|
||||
|
||||
# Examples
|
||||
|
||||
```
|
||||
#![feature(int_to_from_bytes)]
|
||||
|
||||
let value = ", stringify!($SelfT), "::from_ne_bytes(if cfg!(target_endian = \"big\") {
|
||||
", $be_bytes, "
|
||||
} else {
|
||||
", $le_bytes, "
|
||||
});
|
||||
assert_eq!(value, ", $swap_op, ");
|
||||
```"),
|
||||
#[unstable(feature = "int_to_from_bytes", issue = "52963")]
|
||||
#[rustc_const_unstable(feature = "const_int_conversion")]
|
||||
#[inline]
|
||||
#[cfg(not(stage0))]
|
||||
pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
|
||||
unsafe { mem::transmute(bytes) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[lang = "u8"]
|
||||
impl u8 {
|
||||
uint_impl! { u8, u8, 8, 255, "", "", 2, "0x82", "0xa", "0x12", "0x12", "0x48" }
|
||||
uint_impl! { u8, u8, 8, 255, "", "", 2, "0x82", "0xa", "0x12", "0x12", "0x48", "[0x12]",
|
||||
"[0x12]" }
|
||||
|
||||
|
||||
/// Checks if the value is within the ASCII range.
|
||||
|
@ -4658,45 +4711,55 @@ impl u8 {
|
|||
|
||||
#[lang = "u16"]
|
||||
impl u16 {
|
||||
uint_impl! { u16, u16, 16, 65535, "", "", 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48" }
|
||||
uint_impl! { u16, u16, 16, 65535, "", "", 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
|
||||
"[0x34, 0x12]", "[0x12, 0x34]" }
|
||||
}
|
||||
|
||||
#[lang = "u32"]
|
||||
impl u32 {
|
||||
uint_impl! { u32, u32, 32, 4294967295, "", "", 8, "0x10000b3", "0xb301", "0x12345678",
|
||||
"0x78563412", "0x1e6a2c48" }
|
||||
"0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]" }
|
||||
}
|
||||
|
||||
#[lang = "u64"]
|
||||
impl u64 {
|
||||
uint_impl! { u64, u64, 64, 18446744073709551615, "", "", 12, "0xaa00000000006e1", "0x6e10aa",
|
||||
"0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48" }
|
||||
"0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
|
||||
"[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
|
||||
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]" }
|
||||
}
|
||||
|
||||
#[lang = "u128"]
|
||||
impl u128 {
|
||||
uint_impl! { u128, u128, 128, 340282366920938463463374607431768211455, "", "", 16,
|
||||
"0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
|
||||
"0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48" }
|
||||
"0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
|
||||
"[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
|
||||
0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
|
||||
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
|
||||
0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]" }
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "16")]
|
||||
#[lang = "usize"]
|
||||
impl usize {
|
||||
uint_impl! { usize, u16, 16, 65536, "", "", 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48" }
|
||||
uint_impl! { usize, u16, 16, 65536, "", "", 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
|
||||
"[0x34, 0x12]", "[0x12, 0x34]" }
|
||||
}
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
#[lang = "usize"]
|
||||
impl usize {
|
||||
uint_impl! { usize, u32, 32, 4294967295, "", "", 8, "0x10000b3", "0xb301", "0x12345678",
|
||||
"0x78563412", "0x1e6a2c48" }
|
||||
"0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]" }
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
#[lang = "usize"]
|
||||
impl usize {
|
||||
uint_impl! { usize, u64, 64, 18446744073709551615, "", "", 12, "0xaa00000000006e1", "0x6e10aa",
|
||||
"0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48" }
|
||||
"0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
|
||||
"[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
|
||||
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]" }
|
||||
}
|
||||
|
||||
/// A classification of floating point numbers.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue