Skip to content

Commit 709ec40

Browse files
committed
Rollup merge of rust-lang#49871 - SimonSapin:int-bytes, r=sfackler
Add to_bytes and from_bytes to primitive integers Discussion issue turned tracking issue: rust-lang#49792
2 parents 9659f05 + 4472991 commit 709ec40

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

src/libcore/num/mod.rs

+89
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use convert::TryFrom;
1616
use fmt;
1717
use intrinsics;
18+
use mem;
1819
#[allow(deprecated)] use nonzero::NonZero;
1920
use ops;
2021
use str::FromStr;
@@ -1868,6 +1869,50 @@ $EndFeature, "
18681869
#[inline]
18691870
pub fn is_negative(self) -> bool { self < 0 }
18701871
}
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+
}
18711916
}
18721917
}
18731918

@@ -3373,6 +3418,50 @@ $EndFeature, "
33733418
self.one_less_than_next_power_of_two().checked_add(1)
33743419
}
33753420
}
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+
}
33763465
}
33773466
}
33783467

0 commit comments

Comments
 (0)