diff options
| author | bors <bors@rust-lang.org> | 2018-04-14 13:11:24 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-04-14 13:11:24 +0000 |
| commit | 21dae950bebab7376d2bcb92299861e6cd05299d (patch) | |
| tree | 321aff4a41151ac452434bec0483b2a15b5f58c3 /src/libcore | |
| parent | cfc3465b9d3eac25c0511fd5e99c9f342a494411 (diff) | |
| parent | 0e9d6f9bb0ed675c82c30ca6a3227731f2facf6a (diff) | |
| download | rust-21dae950bebab7376d2bcb92299861e6cd05299d.tar.gz rust-21dae950bebab7376d2bcb92299861e6cd05299d.zip | |
Auto merge of #49939 - kennytm:rollup, r=kennytm
Rollup of 14 pull requests Successful merges: #49908, #49876, #49916, #49951, #49465, #49922, #49866, #49915, #49886, #49913, #49852, #49958, #49871, #49864 Failed merges:
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/num/mod.rs | 89 | ||||
| -rw-r--r-- | src/libcore/ops/bit.rs | 14 | ||||
| -rw-r--r-- | src/libcore/sync/atomic.rs | 4 |
3 files changed, 103 insertions, 4 deletions
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 35d70609c19..f2e8caaad14 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; @@ -1868,6 +1869,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) } + } } } @@ -3373,6 +3418,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) } + } } } diff --git a/src/libcore/ops/bit.rs b/src/libcore/ops/bit.rs index a0ecd6cf75c..ec1e65be774 100644 --- a/src/libcore/ops/bit.rs +++ b/src/libcore/ops/bit.rs @@ -315,7 +315,12 @@ macro_rules! bitxor_impl { bitxor_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 } -/// The left shift operator `<<`. +/// The left shift operator `<<`. Note that because this trait is implemented +/// for all integer types with multiple right-hand-side types, Rust's type +/// checker has special handling for `_ << _`, setting the result type for +/// integer operations to the type of the left-hand-side operand. This means +/// that though `a << b` and `a.shl(b)` are one and the same from an evaluation +/// standpoint, they are different when it comes to type inference. /// /// # Examples /// @@ -417,7 +422,12 @@ macro_rules! shl_impl_all { shl_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 isize i128 } -/// The right shift operator `>>`. +/// The right shift operator `>>`. Note that because this trait is implemented +/// for all integer types with multiple right-hand-side types, Rust's type +/// checker has special handling for `_ >> _`, setting the result type for +/// integer operations to the type of the left-hand-side operand. This means +/// that though `a >> b` and `a.shr(b)` are one and the same from an evaluation +/// standpoint, they are different when it comes to type inference. /// /// # Examples /// diff --git a/src/libcore/sync/atomic.rs b/src/libcore/sync/atomic.rs index d336934ec72..62e0979c5fe 100644 --- a/src/libcore/sync/atomic.rs +++ b/src/libcore/sync/atomic.rs @@ -1425,8 +1425,8 @@ assert_eq!(foo.load(Ordering::SeqCst), 0b011110); doc_comment! { concat!("Fetches the value, and applies a function to it that returns an optional -new value. Returns a `Result` (`Ok(_)` if the function returned `Some(_)`, else `Err(_)`) of the -previous value. +new value. Returns a `Result` of `Ok(previous_value)` if the function returned `Some(_)`, else +`Err(previous_value)`. Note: This may call the function multiple times if the value has been changed from other threads in the meantime, as long as the function returns `Some(_)`, but the function will have been applied |
