about summary refs log tree commit diff
path: root/src/libcore/num
diff options
context:
space:
mode:
authorSimon Sapin <simon.sapin@exyr.org>2018-04-11 00:04:32 +0200
committerSimon Sapin <simon.sapin@exyr.org>2018-04-14 12:31:22 +0200
commit447299130a6a088af33152d2cef966c7ab3d7fdb (patch)
treef33b56bc7bf505ff2ac037f5bfc1ecfa662139af /src/libcore/num
parent0b72d48f8e5f3c7cabcf6fcd31474209bff0ab59 (diff)
downloadrust-447299130a6a088af33152d2cef966c7ab3d7fdb.tar.gz
rust-447299130a6a088af33152d2cef966c7ab3d7fdb.zip
Add to_bytes and from_bytes to primitive integers
Diffstat (limited to 'src/libcore/num')
-rw-r--r--src/libcore/num/mod.rs89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs
index dcda404721c..b89fb81c6f7 100644
--- a/src/libcore/num/mod.rs
+++ b/src/libcore/num/mod.rs
@@ -15,6 +15,7 @@
 use convert::TryFrom;
 use fmt;
 use intrinsics;
+use mem;
 #[allow(deprecated)] use nonzero::NonZero;
 use ops;
 use str::FromStr;
@@ -1619,6 +1620,50 @@ $EndFeature, "
             #[inline]
             pub fn is_negative(self) -> bool { self < 0 }
         }
+
+        /// Return the memory representation of this integer as a byte array.
+        ///
+        /// The target platform’s native endianness is used.
+        /// Portable code likely wants to use this after [`to_be`] or [`to_le`].
+        ///
+        /// [`to_be`]: #method.to_be
+        /// [`to_le`]: #method.to_le
+        ///
+        /// # Examples
+        ///
+        /// ```
+        /// #![feature(int_to_from_bytes)]
+        ///
+        /// let bytes = i32::min_value().to_be().to_bytes();
+        /// assert_eq!(bytes, [0x80, 0, 0, 0]);
+        /// ```
+        #[unstable(feature = "int_to_from_bytes", issue = "49792")]
+        #[inline]
+        pub fn to_bytes(self) -> [u8; mem::size_of::<Self>()] {
+            unsafe { mem::transmute(self) }
+        }
+
+        /// Create an integer value from its memory representation as a byte array.
+        ///
+        /// The target platform’s native endianness is used.
+        /// Portable code likely wants to use [`from_be`] or [`from_le`] after this.
+        ///
+        /// [`from_be`]: #method.from_be
+        /// [`from_le`]: #method.from_le
+        ///
+        /// # Examples
+        ///
+        /// ```
+        /// #![feature(int_to_from_bytes)]
+        ///
+        /// let int = i32::from_be(i32::from_bytes([0x80, 0, 0, 0]));
+        /// assert_eq!(int, i32::min_value());
+        /// ```
+        #[unstable(feature = "int_to_from_bytes", issue = "49792")]
+        #[inline]
+        pub fn from_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
+            unsafe { mem::transmute(bytes) }
+        }
     }
 }
 
@@ -2933,6 +2978,50 @@ $EndFeature, "
                 self.one_less_than_next_power_of_two().checked_add(1)
             }
         }
+
+        /// Return the memory representation of this integer as a byte array.
+        ///
+        /// The target platform’s native endianness is used.
+        /// Portable code likely wants to use this after [`to_be`] or [`to_le`].
+        ///
+        /// [`to_be`]: #method.to_be
+        /// [`to_le`]: #method.to_le
+        ///
+        /// # Examples
+        ///
+        /// ```
+        /// #![feature(int_to_from_bytes)]
+        ///
+        /// let bytes = 0x1234_5678_u32.to_be().to_bytes();
+        /// assert_eq!(bytes, [0x12, 0x34, 0x56, 0x78]);
+        /// ```
+        #[unstable(feature = "int_to_from_bytes", issue = "49792")]
+        #[inline]
+        pub fn to_bytes(self) -> [u8; mem::size_of::<Self>()] {
+            unsafe { mem::transmute(self) }
+        }
+
+        /// Create an integer value from its memory representation as a byte array.
+        ///
+        /// The target platform’s native endianness is used.
+        /// Portable code likely wants to use [`to_be`] or [`to_le`] after this.
+        ///
+        /// [`to_be`]: #method.to_be
+        /// [`to_le`]: #method.to_le
+        ///
+        /// # Examples
+        ///
+        /// ```
+        /// #![feature(int_to_from_bytes)]
+        ///
+        /// let int = u32::from_be(u32::from_bytes([0x12, 0x34, 0x56, 0x78]));
+        /// assert_eq!(int, 0x1234_5678_u32);
+        /// ```
+        #[unstable(feature = "int_to_from_bytes", issue = "49792")]
+        #[inline]
+        pub fn from_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
+            unsafe { mem::transmute(bytes) }
+        }
     }
 }