about summary refs log tree commit diff
path: root/src/libcore/intrinsics.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-04-19 13:24:05 +0000
committerbors <bors@rust-lang.org>2020-04-19 13:24:05 +0000
commit1b7dec9e449bb00c7901577abbded39738652b51 (patch)
treef908f092b07ee3843882e6a3687ed6636cfbccd1 /src/libcore/intrinsics.rs
parent36b1a9296cde2b773771710e9bbd608fd2eca35f (diff)
parent1d2532bdf623772c0596166c31df98374d7c6af6 (diff)
downloadrust-1b7dec9e449bb00c7901577abbded39738652b51.tar.gz
rust-1b7dec9e449bb00c7901577abbded39738652b51.zip
Auto merge of #71326 - Dylan-DPC:rollup-hdlkdj5, r=Dylan-DPC
Rollup of 5 pull requests

Successful merges:

 - #71107 (Address concerns of weak-into-raw)
 - #71188 (Fixed missing trait method suggests incorrect code (self parameter not named "self"). )
 - #71300 (Clarify when to use the tracking issue template)
 - #71315 (Add example in the alternative in std::mem::transmute docs)
 - #71319 (Clean up E0522 explanation)

Failed merges:

r? @ghost
Diffstat (limited to 'src/libcore/intrinsics.rs')
-rw-r--r--src/libcore/intrinsics.rs18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/libcore/intrinsics.rs b/src/libcore/intrinsics.rs
index 4a11fb39389..75c73130891 100644
--- a/src/libcore/intrinsics.rs
+++ b/src/libcore/intrinsics.rs
@@ -1100,6 +1100,24 @@ extern "rust-intrinsic" {
     /// Below are common applications of `transmute` which can be replaced with safer
     /// constructs.
     ///
+    /// Turning raw bytes(`&[u8]`) to `u32`, `f64`, etc.:
+    ///
+    /// ```
+    /// let raw_bytes = [0x78, 0x56, 0x34, 0x12];
+    ///
+    /// let num = unsafe {
+    ///     std::mem::transmute::<[u8; 4], u32>(raw_bytes);
+    /// };
+    ///
+    /// // use `u32::from_ne_bytes` instead
+    /// let num = u32::from_ne_bytes(raw_bytes);
+    /// // or use `u32::from_le_bytes` or `u32::from_ge_bytes` to specify the endianness
+    /// let num = u32::from_le_bytes(raw_bytes);
+    /// assert_eq!(num, 0x12345678);
+    /// let num = u32::from_be_bytes(raw_bytes);
+    /// assert_eq!(num, 0x78563412);
+    /// ```
+    ///
     /// Turning a pointer into a `usize`:
     ///
     /// ```