Skip to content

Commit 0644cc1

Browse files
committed
Auto merge of rust-lang#76610 - hch12907:master, r=LukasKalbertodt
Implement as_ne_bytes() for integers and floats This is related to issue rust-lang#64464. I am pretty sure that these functions are actually const-ify-able, and technically as_bits() can also be implemented for floats, but I might need some comments on both.
2 parents 0d37dca + 3c582db commit 0644cc1

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

Diff for: library/core/src/num/f32.rs

+29
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,35 @@ impl f32 {
773773
self.to_bits().to_ne_bytes()
774774
}
775775

776+
/// Return the memory representation of this floating point number as a byte array in
777+
/// native byte order.
778+
///
779+
/// [`to_ne_bytes`] should be preferred over this whenever possible.
780+
///
781+
/// [`to_ne_bytes`]: #method.to_ne_bytes
782+
///
783+
/// # Examples
784+
///
785+
/// ```
786+
/// #![feature(num_as_ne_bytes)]
787+
/// let num = 12.5f32;
788+
/// let bytes = num.as_ne_bytes();
789+
/// assert_eq!(
790+
/// bytes,
791+
/// if cfg!(target_endian = "big") {
792+
/// &[0x41, 0x48, 0x00, 0x00]
793+
/// } else {
794+
/// &[0x00, 0x00, 0x48, 0x41]
795+
/// }
796+
/// );
797+
/// ```
798+
#[unstable(feature = "num_as_ne_bytes", issue = "76976")]
799+
#[inline]
800+
pub fn as_ne_bytes(&self) -> &[u8; 4] {
801+
// SAFETY: `f32` is a plain old datatype so we can always transmute to it
802+
unsafe { &*(self as *const Self as *const _) }
803+
}
804+
776805
/// Create a floating point value from its representation as a byte array in big endian.
777806
///
778807
/// # Examples

Diff for: library/core/src/num/f64.rs

+29
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,35 @@ impl f64 {
787787
self.to_bits().to_ne_bytes()
788788
}
789789

790+
/// Return the memory representation of this floating point number as a byte array in
791+
/// native byte order.
792+
///
793+
/// [`to_ne_bytes`] should be preferred over this whenever possible.
794+
///
795+
/// [`to_ne_bytes`]: #method.to_ne_bytes
796+
///
797+
/// # Examples
798+
///
799+
/// ```
800+
/// #![feature(num_as_ne_bytes)]
801+
/// let num = 12.5f64;
802+
/// let bytes = num.as_ne_bytes();
803+
/// assert_eq!(
804+
/// bytes,
805+
/// if cfg!(target_endian = "big") {
806+
/// &[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
807+
/// } else {
808+
/// &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]
809+
/// }
810+
/// );
811+
/// ```
812+
#[unstable(feature = "num_as_ne_bytes", issue = "76976")]
813+
#[inline]
814+
pub fn as_ne_bytes(&self) -> &[u8; 8] {
815+
// SAFETY: `f64` is a plain old datatype so we can always transmute to it
816+
unsafe { &*(self as *const Self as *const _) }
817+
}
818+
790819
/// Create a floating point value from its representation as a byte array in big endian.
791820
///
792821
/// # Examples

Diff for: library/core/src/num/int_macros.rs

+35
Original file line numberDiff line numberDiff line change
@@ -2054,6 +2054,41 @@ assert_eq!(
20542054
}
20552055
}
20562056

2057+
doc_comment! {
2058+
concat!("
2059+
Return the memory representation of this integer as a byte array in
2060+
native byte order.
2061+
2062+
[`to_ne_bytes`] should be preferred over this whenever possible.
2063+
2064+
[`to_ne_bytes`]: #method.to_ne_bytes
2065+
",
2066+
2067+
"
2068+
# Examples
2069+
2070+
```
2071+
#![feature(num_as_ne_bytes)]
2072+
let num = ", $swap_op, stringify!($SelfT), ";
2073+
let bytes = num.as_ne_bytes();
2074+
assert_eq!(
2075+
bytes,
2076+
if cfg!(target_endian = \"big\") {
2077+
&", $be_bytes, "
2078+
} else {
2079+
&", $le_bytes, "
2080+
}
2081+
);
2082+
```"),
2083+
#[unstable(feature = "num_as_ne_bytes", issue = "76976")]
2084+
#[inline]
2085+
pub fn as_ne_bytes(&self) -> &[u8; mem::size_of::<Self>()] {
2086+
// SAFETY: integers are plain old datatypes so we can always transmute them to
2087+
// arrays of bytes
2088+
unsafe { &*(self as *const Self as *const _) }
2089+
}
2090+
}
2091+
20572092
doc_comment! {
20582093
concat!("Create an integer value from its representation as a byte array in
20592094
big endian.

Diff for: library/core/src/num/uint_macros.rs

+35
Original file line numberDiff line numberDiff line change
@@ -1812,6 +1812,41 @@ assert_eq!(
18121812
}
18131813
}
18141814

1815+
doc_comment! {
1816+
concat!("
1817+
Return the memory representation of this integer as a byte array in
1818+
native byte order.
1819+
1820+
[`to_ne_bytes`] should be preferred over this whenever possible.
1821+
1822+
[`to_ne_bytes`]: #method.to_ne_bytes
1823+
",
1824+
1825+
"
1826+
# Examples
1827+
1828+
```
1829+
#![feature(num_as_ne_bytes)]
1830+
let num = ", $swap_op, stringify!($SelfT), ";
1831+
let bytes = num.as_ne_bytes();
1832+
assert_eq!(
1833+
bytes,
1834+
if cfg!(target_endian = \"big\") {
1835+
&", $be_bytes, "
1836+
} else {
1837+
&", $le_bytes, "
1838+
}
1839+
);
1840+
```"),
1841+
#[unstable(feature = "num_as_ne_bytes", issue = "76976")]
1842+
#[inline]
1843+
pub fn as_ne_bytes(&self) -> &[u8; mem::size_of::<Self>()] {
1844+
// SAFETY: integers are plain old datatypes so we can always transmute them to
1845+
// arrays of bytes
1846+
unsafe { &*(self as *const Self as *const _) }
1847+
}
1848+
}
1849+
18151850
doc_comment! {
18161851
concat!("Create a native endian integer value from its representation
18171852
as a byte array in big endian.

0 commit comments

Comments
 (0)