diff options
| author | bors <bors@rust-lang.org> | 2015-03-19 00:17:00 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-03-19 00:17:00 +0000 |
| commit | 12cb7c6a2847959460ecac75b2c983d071585472 (patch) | |
| tree | 481e99dcf4197b0b25cd765877c1b132f768d772 /src/libcore | |
| parent | 94a95067e017252d4928a4292a6aeef66902e694 (diff) | |
| parent | fccf5a00056b1d72065951a4428070326df1cfb5 (diff) | |
Auto merge of #23482 - alexcrichton:snapshots, r=aturon
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/any.rs | 1 | ||||
| -rw-r--r-- | src/libcore/intrinsics.rs | 26 | ||||
| -rw-r--r-- | src/libcore/num/mod.rs | 1928 | ||||
| -rw-r--r-- | src/libcore/prelude.rs | 2 | ||||
| -rw-r--r-- | src/libcore/ptr.rs | 143 | ||||
| -rw-r--r-- | src/libcore/slice.rs | 2 | ||||
| -rw-r--r-- | src/libcore/str/mod.rs | 2 |
7 files changed, 960 insertions, 1144 deletions
diff --git a/src/libcore/any.rs b/src/libcore/any.rs index 6d3fac4c68d..3938a610668 100644 --- a/src/libcore/any.rs +++ b/src/libcore/any.rs @@ -155,7 +155,6 @@ impl Any { } } -#[cfg(not(stage0))] impl Any+Send { /// Forwards to the method defined on the type `Any`. #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libcore/intrinsics.rs b/src/libcore/intrinsics.rs index ead5da92bd9..1462d07652d 100644 --- a/src/libcore/intrinsics.rs +++ b/src/libcore/intrinsics.rs @@ -44,26 +44,6 @@ use marker::Sized; -#[cfg(stage0)] // SNAP 270a677 -pub type GlueFn = extern "Rust" fn(*const i8); - -#[lang="ty_desc"] -#[derive(Copy)] -#[cfg(stage0)] // SNAP 270a677 -pub struct TyDesc { - // sizeof(T) - pub size: usize, - - // alignof(T) - pub align: usize, - - // Called when a value of type `T` is no longer needed - pub drop_glue: GlueFn, - - // Name corresponding to the type - pub name: &'static str, -} - extern "rust-intrinsic" { // NB: These intrinsics take unsafe pointers because they mutate aliased @@ -198,12 +178,8 @@ extern "rust-intrinsic" { pub fn min_align_of<T>() -> usize; pub fn pref_align_of<T>() -> usize; - /// Get a static pointer to a type descriptor. - #[cfg(stage0)] // SNAP 270a677 - pub fn get_tydesc<T: ?Sized>() -> *const TyDesc; - /// Gets a static string slice containing the name of a type. - #[cfg(not(stage0))] // SNAP 270a677 + #[cfg(not(stage0))] pub fn type_name<T: ?Sized>() -> &'static str; /// Gets an identifier which is globally unique to the specified type. This diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index a77f9709600..156bc2708fb 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -437,13 +437,19 @@ macro_rules! uint_impl { fn max_value() -> $T { -1 } #[inline] - fn count_ones(self) -> u32 { unsafe { $ctpop(self as $ActualT) as u32 } } + fn count_ones(self) -> u32 { + unsafe { $ctpop(self as $ActualT) as u32 } + } #[inline] - fn leading_zeros(self) -> u32 { unsafe { $ctlz(self as $ActualT) as u32 } } + fn leading_zeros(self) -> u32 { + unsafe { $ctlz(self as $ActualT) as u32 } + } #[inline] - fn trailing_zeros(self) -> u32 { unsafe { $cttz(self as $ActualT) as u32 } } + fn trailing_zeros(self) -> u32 { + unsafe { $cttz(self as $ActualT) as u32 } + } #[inline] fn rotate_left(self, n: u32) -> $T { @@ -460,7 +466,9 @@ macro_rules! uint_impl { } #[inline] - fn swap_bytes(self) -> $T { unsafe { $bswap(self as $ActualT) as $T } } + fn swap_bytes(self) -> $T { + unsafe { $bswap(self as $ActualT) as $T } + } #[inline] fn checked_add(self, other: $T) -> Option<$T> { @@ -571,19 +579,29 @@ macro_rules! int_impl { fn count_ones(self) -> u32 { (self as $UnsignedT).count_ones() } #[inline] - fn leading_zeros(self) -> u32 { (self as $UnsignedT).leading_zeros() } + fn leading_zeros(self) -> u32 { + (self as $UnsignedT).leading_zeros() + } #[inline] - fn trailing_zeros(self) -> u32 { (self as $UnsignedT).trailing_zeros() } + fn trailing_zeros(self) -> u32 { + (self as $UnsignedT).trailing_zeros() + } #[inline] - fn rotate_left(self, n: u32) -> $T { (self as $UnsignedT).rotate_left(n) as $T } + fn rotate_left(self, n: u32) -> $T { + (self as $UnsignedT).rotate_left(n) as $T + } #[inline] - fn rotate_right(self, n: u32) -> $T { (self as $UnsignedT).rotate_right(n) as $T } + fn rotate_right(self, n: u32) -> $T { + (self as $UnsignedT).rotate_right(n) as $T + } #[inline] - fn swap_bytes(self) -> $T { (self as $UnsignedT).swap_bytes() as $T } + fn swap_bytes(self) -> $T { + (self as $UnsignedT).swap_bytes() as $T + } #[inline] fn checked_add(self, other: $T) -> Option<$T> { @@ -708,1043 +726,1005 @@ signed_int_impl! { i32 } signed_int_impl! { i64 } signed_int_impl! { int } -#[cfg(stage0)] -/// A built-in unsigned integer. -#[stable(feature = "rust1", since = "1.0.0")] -pub trait UnsignedInt: Int + WrappingOps { - /// Returns `true` iff `self == 2^k` for some `k`. - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - fn is_power_of_two(self) -> bool { - (self.wrapping_sub(Int::one())) & self == Int::zero() && !(self == Int::zero()) - } - - /// Returns the smallest power of two greater than or equal to `self`. - /// Unspecified behavior on overflow. - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - fn next_power_of_two(self) -> Self { - let bits = size_of::<Self>() * 8; - let one: Self = Int::one(); - one << ((bits - self.wrapping_sub(one).leading_zeros() as usize) % bits) - } - - /// Returns the smallest power of two greater than or equal to `n`. If the - /// next power of two is greater than the type's maximum value, `None` is - /// returned, otherwise the power of two is wrapped in `Some`. - #[stable(feature = "rust1", since = "1.0.0")] - fn checked_next_power_of_two(self) -> Option<Self> { - let npot = self.next_power_of_two(); - if npot >= self { - Some(npot) - } else { - None +// `Int` + `SignedInt` implemented for signed integers +macro_rules! int_impl { + ($T:ty = $ActualT:ty, $UnsignedT:ty, $BITS:expr, + $add_with_overflow:path, + $sub_with_overflow:path, + $mul_with_overflow:path) => { + /// Returns the `0` value of this integer type. + // FIXME (#5527): Should be an associated constant + #[unstable(feature = "core", + reason = "unsure about its place in the world")] + #[inline] + pub fn zero() -> $T { 0 } + + /// Returns the `1` value of this integer type. + // FIXME (#5527): Should be an associated constant + #[unstable(feature = "core", + reason = "unsure about its place in the world")] + #[inline] + pub fn one() -> $T { 1 } + + /// Returns the smallest value that can be represented by this integer + /// type. + // FIXME (#5527): Should be and associated constant + #[unstable(feature = "core", + reason = "unsure about its place in the world")] + #[inline] + pub fn min_value() -> $T { (-1 as $T) << ($BITS - 1) } + + /// Returns the largest value that can be represented by this integer + /// type. + // FIXME (#5527): Should be and associated constant + #[unstable(feature = "core", + reason = "unsure about its place in the world")] + #[inline] + pub fn max_value() -> $T { let min: $T = <$T>::min_value(); !min } + + /// Returns the number of ones in the binary representation of `self`. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0b01001100u8; + /// + /// assert_eq!(n.count_ones(), 3); + /// ``` + #[unstable(feature = "core", + reason = "pending integer conventions")] + #[inline] + pub fn count_ones(self) -> u32 { (self as $UnsignedT).count_ones() } + + /// Returns the number of zeros in the binary representation of `self`. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0b01001100u8; + /// + /// assert_eq!(n.count_zeros(), 5); + /// ``` + #[unstable(feature = "core", + reason = "pending integer conventions")] + #[inline] + pub fn count_zeros(self) -> u32 { + (!self).count_ones() } - } -} - -#[cfg(stage0)] -#[stable(feature = "rust1", since = "1.0.0")] -impl UnsignedInt for uint {} - -#[cfg(stage0)] -#[stable(feature = "rust1", since = "1.0.0")] -impl UnsignedInt for u8 {} - -#[cfg(stage0)] -#[stable(feature = "rust1", since = "1.0.0")] -impl UnsignedInt for u16 {} -#[cfg(stage0)] -#[stable(feature = "rust1", since = "1.0.0")] -impl UnsignedInt for u32 {} - -#[cfg(stage0)] -#[stable(feature = "rust1", since = "1.0.0")] -impl UnsignedInt for u64 {} - -// NB(japaric) I added this module to avoid adding several `cfg(not(stage0))`, and avoid name -// clashes between macros. We should move all the items inside this module into the outer scope -// once the `Int` trait is removed -#[cfg(not(stage0))] -mod inherent { - use intrinsics; - use mem::size_of; - use option::Option::{self, Some, None}; - - use super::wrapping::{OverflowingOps, WrappingOps}; - - // `Int` + `SignedInt` implemented for signed integers - macro_rules! int_impl { - ($T:ty = $ActualT:ty, $UnsignedT:ty, $BITS:expr, - $add_with_overflow:path, - $sub_with_overflow:path, - $mul_with_overflow:path) => { - /// Returns the `0` value of this integer type. - // FIXME (#5527): Should be an associated constant - #[unstable(feature = "core", - reason = "unsure about its place in the world")] - #[inline] - pub fn zero() -> $T { 0 } - - /// Returns the `1` value of this integer type. - // FIXME (#5527): Should be an associated constant - #[unstable(feature = "core", - reason = "unsure about its place in the world")] - #[inline] - pub fn one() -> $T { 1 } - - /// Returns the smallest value that can be represented by this integer type. - // FIXME (#5527): Should be and associated constant - #[unstable(feature = "core", - reason = "unsure about its place in the world")] - #[inline] - pub fn min_value() -> $T { (-1 as $T) << ($BITS - 1) } - - /// Returns the largest value that can be represented by this integer type. - // FIXME (#5527): Should be and associated constant - #[unstable(feature = "core", - reason = "unsure about its place in the world")] - #[inline] - pub fn max_value() -> $T { let min: $T = <$T>::min_value(); !min } - - /// Returns the number of ones in the binary representation of `self`. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0b01001100u8; - /// - /// assert_eq!(n.count_ones(), 3); - /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] - #[inline] - pub fn count_ones(self) -> u32 { (self as $UnsignedT).count_ones() } - - /// Returns the number of zeros in the binary representation of `self`. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0b01001100u8; - /// - /// assert_eq!(n.count_zeros(), 5); - /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] - #[inline] - pub fn count_zeros(self) -> u32 { - (!self).count_ones() - } - - /// Returns the number of leading zeros in the binary representation - /// of `self`. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0b0101000u16; - /// - /// assert_eq!(n.leading_zeros(), 10); - /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] - #[inline] - pub fn leading_zeros(self) -> u32 { (self as $UnsignedT).leading_zeros() } + /// Returns the number of leading zeros in the binary representation + /// of `self`. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0b0101000u16; + /// + /// assert_eq!(n.leading_zeros(), 10); + /// ``` + #[unstable(feature = "core", + reason = "pending integer conventions")] + #[inline] + pub fn leading_zeros(self) -> u32 { + (self as $UnsignedT).leading_zeros() + } - /// Returns the number of trailing zeros in the binary representation - /// of `self`. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0b0101000u16; - /// - /// assert_eq!(n.trailing_zeros(), 3); - /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] - #[inline] - pub fn trailing_zeros(self) -> u32 { (self as $UnsignedT).trailing_zeros() } + /// Returns the number of trailing zeros in the binary representation + /// of `self`. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0b0101000u16; + /// + /// assert_eq!(n.trailing_zeros(), 3); + /// ``` + #[unstable(feature = "core", + reason = "pending integer conventions")] + #[inline] + pub fn trailing_zeros(self) -> u32 { + (self as $UnsignedT).trailing_zeros() + } - /// Shifts the bits to the left by a specified amount amount, `n`, wrapping - /// the truncated bits to the end of the resulting integer. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0x0123456789ABCDEFu64; - /// let m = 0x3456789ABCDEF012u64; - /// - /// assert_eq!(n.rotate_left(12), m); - /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] - #[inline] - pub fn rotate_left(self, n: u32) -> $T { (self as $UnsignedT).rotate_left(n) as $T } + /// Shifts the bits to the left by a specified amount amount, `n`, + /// wrapping the truncated bits to the end of the resulting integer. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0x0123456789ABCDEFu64; + /// let m = 0x3456789ABCDEF012u64; + /// + /// assert_eq!(n.rotate_left(12), m); + /// ``` + #[unstable(feature = "core", + reason = "pending integer conventions")] + #[inline] + pub fn rotate_left(self, n: u32) -> $T { + (self as $UnsignedT).rotate_left(n) as $T + } - /// Shifts the bits to the right by a specified amount amount, `n`, wrapping - /// the truncated bits to the beginning of the resulting integer. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0x0123456789ABCDEFu64; - /// let m = 0xDEF0123456789ABCu64; - /// - /// assert_eq!(n.rotate_right(12), m); - /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] - #[inline] - pub fn rotate_right(self, n: u32) -> $T { (self as $UnsignedT).rotate_right(n) as $T } + /// Shifts the bits to the right by a specified amount amount, `n`, + /// wrapping the truncated bits to the beginning of the resulting + /// integer. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0x0123456789ABCDEFu64; + /// let m = 0xDEF0123456789ABCu64; + /// + /// assert_eq!(n.rotate_right(12), m); + /// ``` + #[unstable(feature = "core", + reason = "pending integer conventions")] + #[inline] + pub fn rotate_right(self, n: u32) -> $T { + (self as $UnsignedT).rotate_right(n) as $T + } - /// Reverses the byte order of the integer. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0x0123456789ABCDEFu64; - /// let m = 0xEFCDAB8967452301u64; - /// - /// assert_eq!(n.swap_bytes(), m); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn swap_bytes(self) -> $T { (self as $UnsignedT).swap_bytes() as $T } + /// Reverses the byte order of the integer. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0x0123456789ABCDEFu64; + /// let m = 0xEFCDAB8967452301u64; + /// + /// assert_eq!(n.swap_bytes(), m); + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn swap_bytes(self) -> $T { + (self as $UnsignedT).swap_bytes() as $T + } - /// Convert an integer from big endian to the target's endianness. - /// - /// On big endian this is a no-op. On little endian the bytes are swapped. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0x0123456789ABCDEFu64; - /// - /// if cfg!(target_endian = "big") { - /// assert_eq!(Int::from_be(n), n) - /// } else { - /// assert_eq!(Int::from_be(n), n.swap_bytes()) - /// } - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn from_be(x: $T) -> $T { - if cfg!(target_endian = "big") { x } else { x.swap_bytes() } - } + /// Convert an integer from big endian to the target's endianness. + /// + /// On big endian this is a no-op. On little endian the bytes are + /// swapped. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0x0123456789ABCDEFu64; + /// + /// if cfg!(target_endian = "big") { + /// assert_eq!(Int::from_be(n), n) + /// } else { + /// assert_eq!(Int::from_be(n), n.swap_bytes()) + /// } + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn from_be(x: $T) -> $T { + if cfg!(target_endian = "big") { x } else { x.swap_bytes() } + } - /// Convert an integer from little endian to the target's endianness. - /// - /// On little endian this is a no-op. On big endian the bytes are swapped. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0x0123456789ABCDEFu64; - /// - /// if cfg!(target_endian = "little") { - /// assert_eq!(Int::from_le(n), n) - /// } else { - /// assert_eq!(Int::from_le(n), n.swap_bytes()) - /// } - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn from_le(x: $T) -> $T { - if cfg!(target_endian = "little") { x } else { x.swap_bytes() } - } + /// Convert an integer from little endian to the target's endianness. + /// + /// On little endian this is a no-op. On big endian the bytes are + /// swapped. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0x0123456789ABCDEFu64; + /// + /// if cfg!(target_endian = "little") { + /// assert_eq!(Int::from_le(n), n) + /// } else { + /// assert_eq!(Int::from_le(n), n.swap_bytes()) + /// } + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn from_le(x: $T) -> $T { + if cfg!(target_endian = "little") { x } else { x.swap_bytes() } + } - /// Convert `self` to big endian from the target's endianness. - /// - /// On big endian this is a no-op. On little endian the bytes are swapped. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0x0123456789ABCDEFu64; - /// - /// if cfg!(target_endian = "big") { - /// assert_eq!(n.to_be(), n) - /// } else { - /// assert_eq!(n.to_be(), n.swap_bytes()) - /// } - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn to_be(self) -> $T { // or not to be? - if cfg!(target_endian = "big") { self } else { self.swap_bytes() } - } + /// Convert `self` to big endian from the target's endianness. + /// + /// On big endian this is a no-op. On little endian the bytes are + /// swapped. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0x0123456789ABCDEFu64; + /// + /// if cfg!(target_endian = "big") { + /// assert_eq!(n.to_be(), n) + /// } else { + /// assert_eq!(n.to_be(), n.swap_bytes()) + /// } + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn to_be(self) -> $T { // or not to be? + if cfg!(target_endian = "big") { self } else { self.swap_bytes() } + } - /// Convert `self` to little endian from the target's endianness. - /// - /// On little endian this is a no-op. On big endian the bytes are swapped. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0x0123456789ABCDEFu64; - /// - /// if cfg!(target_endian = "little") { - /// assert_eq!(n.to_le(), n) - /// } else { - /// assert_eq!(n.to_le(), n.swap_bytes()) - /// } - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn to_le(self) -> $T { - if cfg!(target_endian = "little") { self } else { self.swap_bytes() } - } + /// Convert `self` to little endian from the target's endianness. + /// + /// On little endian this is a no-op. On big endian the bytes are + /// swapped. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0x0123456789ABCDEFu64; + /// + /// if cfg!(target_endian = "little") { + /// assert_eq!(n.to_le(), n) + /// } else { + /// assert_eq!(n.to_le(), n.swap_bytes()) + /// } + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn to_le(self) -> $T { + if cfg!(target_endian = "little") { self } else { self.swap_bytes() } + } - /// Checked integer addition. Computes `self + other`, returning `None` if - /// overflow occurred. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// assert_eq!(5u16.checked_add(65530), Some(65535)); - /// assert_eq!(6u16.checked_add(65530), None); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn checked_add(self, other: $T) -> Option<$T> { - checked_op!($T, $ActualT, $add_with_overflow, self, other) - } + /// Checked integer addition. Computes `self + other`, returning `None` + /// if overflow occurred. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// assert_eq!(5u16.checked_add(65530), Some(65535)); + /// assert_eq!(6u16.checked_add(65530), None); + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn checked_add(self, other: $T) -> Option<$T> { + checked_op!($T, $ActualT, $add_with_overflow, self, other) + } - /// Checked integer subtraction. Computes `self - other`, returning `None` - /// if underflow occurred. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// assert_eq!((-127i8).checked_sub(1), Some(-128)); - /// assert_eq!((-128i8).checked_sub(1), None); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn checked_sub(self, other: $T) -> Option<$T> { - checked_op!($T, $ActualT, $sub_with_overflow, self, other) - } + /// Checked integer subtraction. Computes `self - other`, returning + /// `None` if underflow occurred. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// assert_eq!((-127i8).checked_sub(1), Some(-128)); + /// assert_eq!((-128i8).checked_sub(1), None); + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn checked_sub(self, other: $T) -> Option<$T> { + checked_op!($T, $ActualT, $sub_with_overflow, self, other) + } - /// Checked integer multiplication. Computes `self * other`, returning - /// `None` if underflow or overflow occurred. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// assert_eq!(5u8.checked_mul(51), Some(255)); - /// assert_eq!(5u8.checked_mul(52), None); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn checked_mul(self, other: $T) -> Option<$T> { - checked_op!($T, $ActualT, $mul_with_overflow, self, other) - } + /// Checked integer multiplication. Computes `self * other`, returning + /// `None` if underflow or overflow occurred. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// assert_eq!(5u8.checked_mul(51), Some(255)); + /// assert_eq!(5u8.checked_mul(52), None); + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn checked_mul(self, other: $T) -> Option<$T> { + checked_op!($T, $ActualT, $mul_with_overflow, self, other) + } - /// Checked integer division. Computes `self / other`, returning `None` if - /// `other == 0` or the operation results in underflow or overflow. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// assert_eq!((-127i8).checked_div(-1), Some(127)); - /// assert_eq!((-128i8).checked_div(-1), None); - /// assert_eq!((1i8).checked_div(0), None); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn checked_div(self, v: $T) -> Option<$T> { - match v { - 0 => None, - -1 if self == <$T>::min_value() - => None, - v => Some(self / v), - } + /// Checked integer division. Computes `self / other`, returning `None` + /// if `other == 0` or the operation results in underflow or overflow. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// assert_eq!((-127i8).checked_div(-1), Some(127)); + /// assert_eq!((-128i8).checked_div(-1), None); + /// assert_eq!((1i8).checked_div(0), None); + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn checked_div(self, v: $T) -> Option<$T> { + match v { + 0 => None, + -1 if self == <$T>::min_value() + => None, + v => Some(self / v), } + } - /// Saturating integer addition. Computes `self + other`, saturating at - /// the numeric bounds instead of overflowing. - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn saturating_add(self, other: $T) -> $T { - match self.checked_add(other) { - Some(x) => x, - None if other >= <$T>::zero() => <$T>::max_value(), - None => <$T>::min_value(), - } + /// Saturating integer addition. Computes `self + other`, saturating at + /// the numeric bounds instead of overflowing. + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn saturating_add(self, other: $T) -> $T { + match self.checked_add(other) { + Some(x) => x, + None if other >= <$T>::zero() => <$T>::max_value(), + None => <$T>::min_value(), } + } - /// Saturating integer subtraction. Computes `self - other`, saturating at - /// the numeric bounds instead of overflowing. - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn saturating_sub(self, other: $T) -> $T { - match self.checked_sub(other) { - Some(x) => x, - None if other >= <$T>::zero() => <$T>::min_value(), - None => <$T>::max_value(), - } + /// Saturating integer subtraction. Computes `self - other`, saturating + /// at the numeric bounds instead of overflowing. + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn saturating_sub(self, other: $T) -> $T { + match self.checked_sub(other) { + Some(x) => x, + None if other >= <$T>::zero() => <$T>::min_value(), + None => <$T>::max_value(), } + } - /// Raises self to the power of `exp`, using exponentiation by squaring. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// assert_eq!(2.pow(4), 16); - /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] - #[inline] - pub fn pow(self, mut exp: u32) -> $T { - let mut base = self; - let mut acc = <$T>::one(); - - let mut prev_base = self; - let mut base_oflo = false; - while exp > 0 { - if (exp & 1) == 1 { - if base_oflo { - // ensure overflow occurs in the same manner it - // would have otherwise (i.e. signal any exception - // it would have otherwise). - acc = acc * (prev_base * prev_base); - } else { - acc = acc * base; - } + /// Raises self to the power of `exp`, using exponentiation by squaring. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// assert_eq!(2.pow(4), 16); + /// ``` + #[unstable(feature = "core", + reason = "pending integer conventions")] + #[inline] + pub fn pow(self, mut exp: u32) -> $T { + let mut base = self; + let mut acc = <$T>::one(); + + let mut prev_base = self; + let mut base_oflo = false; + while exp > 0 { + if (exp & 1) == 1 { + if base_oflo { + // ensure overflow occurs in the same manner it + // would have otherwise (i.e. signal any exception + // it would have otherwise). + acc = acc * (prev_base * prev_base); + } else { + acc = acc * base; } - prev_base = base; - let (new_base, new_base_oflo) = base.overflowing_mul(base); - base = new_base; - base_oflo = new_base_oflo; - exp /= 2; } - acc + prev_base = base; + let (new_base, new_base_oflo) = base.overflowing_mul(base); + base = new_base; + base_oflo = new_base_oflo; + exp /= 2; } + acc + } - /// Computes the absolute value of `self`. `Int::min_value()` will be - /// returned if the number is `Int::min_value()`. - #[unstable(feature = "core", reason = "overflow in debug builds?")] - #[inline] - pub fn abs(self) -> $T { - if self.is_negative() { -self } else { self } - } + /// Computes the absolute value of `self`. `Int::min_value()` will be + /// returned if the number is `Int::min_value()`. + #[unstable(feature = "core", reason = "overflow in debug builds?")] + #[inline] + pub fn abs(self) -> $T { + if self.is_negative() { -self } else { self } + } - /// Returns a number representing sign of `self`. - /// - /// - `0` if the number is zero - /// - `1` if the number is positive - /// - `-1` if the number is negative - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn signum(self) -> $T { - match self { - n if n > 0 => 1, - 0 => 0, - _ => -1, - } + /// Returns a number representing sign of `self`. + /// + /// - `0` if the number is zero + /// - `1` if the number is positive + /// - `-1` if the number is negative + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn signum(self) -> $T { + match self { + n if n > 0 => 1, + 0 => 0, + _ => -1, } + } - /// Returns `true` if `self` is positive and `false` if the number - /// is zero or negative. - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn is_positive(self) -> bool { self > 0 } + /// Returns `true` if `self` is positive and `false` if the number + /// is zero or negative. + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn is_positive(self) -> bool { self > 0 } - /// Returns `true` if `self` is negative and `false` if the number - /// is zero or positive. - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn is_negative(self) -> bool { self < 0 } - } + /// Returns `true` if `self` is negative and `false` if the number + /// is zero or positive. + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn is_negative(self) -> bool { self < 0 } } +} - #[lang = "i8"] - impl i8 { - int_impl! { i8 = i8, u8, 8, - intrinsics::i8_add_with_overflow, - intrinsics::i8_sub_with_overflow, - intrinsics::i8_mul_with_overflow } - } +#[lang = "i8"] +impl i8 { + int_impl! { i8 = i8, u8, 8, + intrinsics::i8_add_with_overflow, + intrinsics::i8_sub_with_overflow, + intrinsics::i8_mul_with_overflow } +} - #[lang = "i16"] - impl i16 { - int_impl! { i16 = i16, u16, 16, - intrinsics::i16_add_with_overflow, - intrinsics::i16_sub_with_overflow, - intrinsics::i16_mul_with_overflow } - } +#[lang = "i16"] +impl i16 { + int_impl! { i16 = i16, u16, 16, + intrinsics::i16_add_with_overflow, + intrinsics::i16_sub_with_overflow, + intrinsics::i16_mul_with_overflow } +} - #[lang = "i32"] - impl i32 { - int_impl! { i32 = i32, u32, 32, - intrinsics::i32_add_with_overflow, - intrinsics::i32_sub_with_overflow, - intrinsics::i32_mul_with_overflow } - } +#[lang = "i32"] +impl i32 { + int_impl! { i32 = i32, u32, 32, + intrinsics::i32_add_with_overflow, + intrinsics::i32_sub_with_overflow, + intrinsics::i32_mul_with_overflow } +} - #[lang = "i64"] - impl i64 { - int_impl! { i64 = i64, u64, 64, - intrinsics::i64_add_with_overflow, - intrinsics::i64_sub_with_overflow, - intrinsics::i64_mul_with_overflow } - } +#[lang = "i64"] +impl i64 { + int_impl! { i64 = i64, u64, 64, + intrinsics::i64_add_with_overflow, + intrinsics::i64_sub_with_overflow, + intrinsics::i64_mul_with_overflow } +} - #[cfg(target_pointer_width = "32")] - #[lang = "isize"] - impl isize { - int_impl! { int = i32, u32, 32, - intrinsics::i32_add_with_overflow, - intrinsics::i32_sub_with_overflow, - intrinsics::i32_mul_with_overflow } - } +#[cfg(target_pointer_width = "32")] +#[lang = "isize"] +impl isize { + int_impl! { int = i32, u32, 32, + intrinsics::i32_add_with_overflow, + intrinsics::i32_sub_with_overflow, + intrinsics::i32_mul_with_overflow } +} - #[cfg(target_pointer_width = "64")] - #[lang = "isize"] - impl isize { - int_impl! { int = i64, u64, 64, - intrinsics::i64_add_with_overflow, - intrinsics::i64_sub_with_overflow, - intrinsics::i64_mul_with_overflow } - } +#[cfg(target_pointer_width = "64")] +#[lang = "isize"] +impl isize { + int_impl! { int = i64, u64, 64, + intrinsics::i64_add_with_overflow, + intrinsics::i64_sub_with_overflow, + intrinsics::i64_mul_with_overflow } +} - // `Int` + `UnsignedInt` implemented for signed integers - macro_rules! uint_impl { - ($T:ty = $ActualT:ty, $BITS:expr, - $ctpop:path, - $ctlz:path, - $cttz:path, - $bswap:path, - $add_with_overflow:path, - $sub_with_overflow:path, - $mul_with_overflow:path) => { - /// Returns the `0` value of this integer type. - // FIXME (#5527): Should be an associated constant - #[unstable(feature = "core", - reason = "unsure about its place in the world")] - #[inline] - pub fn zero() -> $T { 0 } - - /// Returns the `1` value of this integer type. - // FIXME (#5527): Should be an associated constant - #[unstable(feature = "core", - reason = "unsure about its place in the world")] - #[inline] - pub fn one() -> $T { 1 } - - /// Returns the smallest value that can be represented by this integer type. - // FIXME (#5527): Should be and associated constant - #[unstable(feature = "core", - reason = "unsure about its place in the world")] - #[inline] - pub fn min_value() -> $T { 0 } - - /// Returns the largest value that can be represented by this integer type. - // FIXME (#5527): Should be and associated constant - #[unstable(feature = "core", - reason = "unsure about its place in the world")] - #[inline] - pub fn max_value() -> $T { -1 } - - /// Returns the number of ones in the binary representation of `self`. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0b01001100u8; - /// - /// assert_eq!(n.count_ones(), 3); - /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] - #[inline] - pub fn count_ones(self) -> u32 { unsafe { $ctpop(self as $ActualT) as u32 } } +// `Int` + `UnsignedInt` implemented for signed integers +macro_rules! uint_impl { + ($T:ty = $ActualT:ty, $BITS:expr, + $ctpop:path, + $ctlz:path, + $cttz:path, + $bswap:path, + $add_with_overflow:path, + $sub_with_overflow:path, + $mul_with_overflow:path) => { + /// Returns the `0` value of this integer type. + // FIXME (#5527): Should be an associated constant + #[unstable(feature = "core", + reason = "unsure about its place in the world")] + #[inline] + pub fn zero() -> $T { 0 } + + /// Returns the `1` value of this integer type. + // FIXME (#5527): Should be an associated constant + #[unstable(feature = "core", + reason = "unsure about its place in the world")] + #[inline] + pub fn one() -> $T { 1 } + + /// Returns the smallest value that can be represented by this integer + /// type. + // FIXME (#5527): Should be and associated constant + #[unstable(feature = "core", + reason = "unsure about its place in the world")] + #[inline] + pub fn min_value() -> $T { 0 } + + /// Returns the largest value that can be represented by this integer + /// type. + // FIXME (#5527): Should be and associated constant + #[unstable(feature = "core", + reason = "unsure about its place in the world")] + #[inline] + pub fn max_value() -> $T { -1 } + + /// Returns the number of ones in the binary representation of `self`. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0b01001100u8; + /// + /// assert_eq!(n.count_ones(), 3); + /// ``` + #[unstable(feature = "core", + reason = "pending integer conventions")] + #[inline] + pub fn count_ones(self) -> u32 { + unsafe { $ctpop(self as $ActualT) as u32 } + } - /// Returns the number of zeros in the binary representation of `self`. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0b01001100u8; - /// - /// assert_eq!(n.count_zeros(), 5); - /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] - #[inline] - pub fn count_zeros(self) -> u32 { - (!self).count_ones() - } + /// Returns the number of zeros in the binary representation of `self`. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0b01001100u8; + /// + /// assert_eq!(n.count_zeros(), 5); + /// ``` + #[unstable(feature = "core", + reason = "pending integer conventions")] + #[inline] + pub fn count_zeros(self) -> u32 { + (!self).count_ones() + } - /// Returns the number of leading zeros in the binary representation - /// of `self`. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0b0101000u16; - /// - /// assert_eq!(n.leading_zeros(), 10); - /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] - #[inline] - pub fn leading_zeros(self) -> u32 { unsafe { $ctlz(self as $ActualT) as u32 } } + /// Returns the number of leading zeros in the binary representation + /// of `self`. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0b0101000u16; + /// + /// assert_eq!(n.leading_zeros(), 10); + /// ``` + #[unstable(feature = "core", + reason = "pending integer conventions")] + #[inline] + pub fn leading_zeros(self) -> u32 { + unsafe { $ctlz(self as $ActualT) as u32 } + } - /// Returns the number of trailing zeros in the binary representation - /// of `self`. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0b0101000u16; - /// - /// assert_eq!(n.trailing_zeros(), 3); - /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] - #[inline] - pub fn trailing_zeros(self) -> u32 { unsafe { $cttz(self as $ActualT) as u32 } } + /// Returns the number of trailing zeros in the binary representation + /// of `self`. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0b0101000u16; + /// + /// assert_eq!(n.trailing_zeros(), 3); + /// ``` + #[unstable(feature = "core", + reason = "pending integer conventions")] + #[inline] + pub fn trailing_zeros(self) -> u32 { + unsafe { $cttz(self as $ActualT) as u32 } + } - /// Shifts the bits to the left by a specified amount amount, `n`, wrapping - /// the truncated bits to the end of the resulting integer. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0x0123456789ABCDEFu64; - /// let m = 0x3456789ABCDEF012u64; - /// - /// assert_eq!(n.rotate_left(12), m); - /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] - #[inline] - pub fn rotate_left(self, n: u32) -> $T { - // Protect against undefined behaviour for over-long bit shifts - let n = n % $BITS; - (self << n) | (self >> (($BITS - n) % $BITS)) - } + /// Shifts the bits to the left by a specified amount amount, `n`, + /// wrapping the truncated bits to the end of the resulting integer. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0x0123456789ABCDEFu64; + /// let m = 0x3456789ABCDEF012u64; + /// + /// assert_eq!(n.rotate_left(12), m); + /// ``` + #[unstable(feature = "core", + reason = "pending integer conventions")] + #[inline] + pub fn rotate_left(self, n: u32) -> $T { + // Protect against undefined behaviour for over-long bit shifts + let n = n % $BITS; + (self << n) | (self >> (($BITS - n) % $BITS)) + } - /// Shifts the bits to the right by a specified amount amount, `n`, wrapping - /// the truncated bits to the beginning of the resulting integer. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0x0123456789ABCDEFu64; - /// let m = 0xDEF0123456789ABCu64; - /// - /// assert_eq!(n.rotate_right(12), m); - /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] - #[inline] - pub fn rotate_right(self, n: u32) -> $T { - // Protect against undefined behaviour for over-long bit shifts - let n = n % $BITS; - (self >> n) | (self << (($BITS - n) % $BITS)) - } + /// Shifts the bits to the right by a specified amount amount, `n`, + /// wrapping the truncated bits to the beginning of the resulting + /// integer. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0x0123456789ABCDEFu64; + /// let m = 0xDEF0123456789ABCu64; + /// + /// assert_eq!(n.rotate_right(12), m); + /// ``` + #[unstable(feature = "core", + reason = "pending integer conventions")] + #[inline] + pub fn rotate_right(self, n: u32) -> $T { + // Protect against undefined behaviour for over-long bit shifts + let n = n % $BITS; + (self >> n) | (self << (($BITS - n) % $BITS)) + } - /// Reverses the byte order of the integer. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0x0123456789ABCDEFu64; - /// let m = 0xEFCDAB8967452301u64; - /// - /// assert_eq!(n.swap_bytes(), m); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn swap_bytes(self) -> $T { unsafe { $bswap(self as $ActualT) as $T } } + /// Reverses the byte order of the integer. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0x0123456789ABCDEFu64; + /// let m = 0xEFCDAB8967452301u64; + /// + /// assert_eq!(n.swap_bytes(), m); + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn swap_bytes(self) -> $T { + unsafe { $bswap(self as $ActualT) as $T } + } - /// Convert an integer from big endian to the target's endianness. - /// - /// On big endian this is a no-op. On little endian the bytes are swapped. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0x0123456789ABCDEFu64; - /// - /// if cfg!(target_endian = "big") { - /// assert_eq!(Int::from_be(n), n) - /// } else { - /// assert_eq!(Int::from_be(n), n.swap_bytes()) - /// } - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn from_be(x: $T) -> $T { - if cfg!(target_endian = "big") { x } else { x.swap_bytes() } - } + /// Convert an integer from big endian to the target's endianness. + /// + /// On big endian this is a no-op. On little endian the bytes are + /// swapped. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0x0123456789ABCDEFu64; + /// + /// if cfg!(target_endian = "big") { + /// assert_eq!(Int::from_be(n), n) + /// } else { + /// assert_eq!(Int::from_be(n), n.swap_bytes()) + /// } + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn from_be(x: $T) -> $T { + if cfg!(target_endian = "big") { x } else { x.swap_bytes() } + } - /// Convert an integer from little endian to the target's endianness. - /// - /// On little endian this is a no-op. On big endian the bytes are swapped. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0x0123456789ABCDEFu64; - /// - /// if cfg!(target_endian = "little") { - /// assert_eq!(Int::from_le(n), n) - /// } else { - /// assert_eq!(Int::from_le(n), n.swap_bytes()) - /// } - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn from_le(x: $T) -> $T { - if cfg!(target_endian = "little") { x } else { x.swap_bytes() } - } + /// Convert an integer from little endian to the target's endianness. + /// + /// On little endian this is a no-op. On big endian the bytes are + /// swapped. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0x0123456789ABCDEFu64; + /// + /// if cfg!(target_endian = "little") { + /// assert_eq!(Int::from_le(n), n) + /// } else { + /// assert_eq!(Int::from_le(n), n.swap_bytes()) + /// } + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn from_le(x: $T) -> $T { + if cfg!(target_endian = "little") { x } else { x.swap_bytes() } + } - /// Convert `self` to big endian from the target's endianness. - /// - /// On big endian this is a no-op. On little endian the bytes are swapped. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0x0123456789ABCDEFu64; - /// - /// if cfg!(target_endian = "big") { - /// assert_eq!(n.to_be(), n) - /// } else { - /// assert_eq!(n.to_be(), n.swap_bytes()) - /// } - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn to_be(self) -> $T { // or not to be? - if cfg!(target_endian = "big") { self } else { self.swap_bytes() } - } + /// Convert `self` to big endian from the target's endianness. + /// + /// On big endian this is a no-op. On little endian the bytes are + /// swapped. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0x0123456789ABCDEFu64; + /// + /// if cfg!(target_endian = "big") { + /// assert_eq!(n.to_be(), n) + /// } else { + /// assert_eq!(n.to_be(), n.swap_bytes()) + /// } + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn to_be(self) -> $T { // or not to be? + if cfg!(target_endian = "big") { self } else { self.swap_bytes() } + } - /// Convert `self` to little endian from the target's endianness. - /// - /// On little endian this is a no-op. On big endian the bytes are swapped. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// let n = 0x0123456789ABCDEFu64; - /// - /// if cfg!(target_endian = "little") { - /// assert_eq!(n.to_le(), n) - /// } else { - /// assert_eq!(n.to_le(), n.swap_bytes()) - /// } - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn to_le(self) -> $T { - if cfg!(target_endian = "little") { self } else { self.swap_bytes() } - } + /// Convert `self` to little endian from the target's endianness. + /// + /// On little endian this is a no-op. On big endian the bytes are + /// swapped. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// let n = 0x0123456789ABCDEFu64; + /// + /// if cfg!(target_endian = "little") { + /// assert_eq!(n.to_le(), n) + /// } else { + /// assert_eq!(n.to_le(), n.swap_bytes()) + /// } + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn to_le(self) -> $T { + if cfg!(target_endian = "little") { self } else { self.swap_bytes() } + } - /// Checked integer addition. Computes `self + other`, returning `None` if - /// overflow occurred. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// assert_eq!(5u16.checked_add(65530), Some(65535)); - /// assert_eq!(6u16.checked_add(65530), None); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn checked_add(self, other: $T) -> Option<$T> { - checked_op!($T, $ActualT, $add_with_overflow, self, other) - } + /// Checked integer addition. Computes `self + other`, returning `None` + /// if overflow occurred. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// assert_eq!(5u16.checked_add(65530), Some(65535)); + /// assert_eq!(6u16.checked_add(65530), None); + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn checked_add(self, other: $T) -> Option<$T> { + checked_op!($T, $ActualT, $add_with_overflow, self, other) + } - /// Checked integer subtraction. Computes `self - other`, returning `None` - /// if underflow occurred. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// assert_eq!((-127i8).checked_sub(1), Some(-128)); - /// assert_eq!((-128i8).checked_sub(1), None); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn checked_sub(self, other: $T) -> Option<$T> { - checked_op!($T, $ActualT, $sub_with_overflow, self, other) - } + /// Checked integer subtraction. Computes `self - other`, returning + /// `None` if underflow occurred. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// assert_eq!((-127i8).checked_sub(1), Some(-128)); + /// assert_eq!((-128i8).checked_sub(1), None); + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn checked_sub(self, other: $T) -> Option<$T> { + checked_op!($T, $ActualT, $sub_with_overflow, self, other) + } - /// Checked integer multiplication. Computes `self * other`, returning - /// `None` if underflow or overflow occurred. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// assert_eq!(5u8.checked_mul(51), Some(255)); - /// assert_eq!(5u8.checked_mul(52), None); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn checked_mul(self, other: $T) -> Option<$T> { - checked_op!($T, $ActualT, $mul_with_overflow, self, other) - } + /// Checked integer multiplication. Computes `self * other`, returning + /// `None` if underflow or overflow occurred. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// assert_eq!(5u8.checked_mul(51), Some(255)); + /// assert_eq!(5u8.checked_mul(52), None); + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn checked_mul(self, other: $T) -> Option<$T> { + checked_op!($T, $ActualT, $mul_with_overflow, self, other) + } - /// Checked integer division. Computes `self / other`, returning `None` if - /// `other == 0` or the operation results in underflow or overflow. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// assert_eq!((-127i8).checked_div(-1), Some(127)); - /// assert_eq!((-128i8).checked_div(-1), None); - /// assert_eq!((1i8).checked_div(0), None); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn checked_div(self, v: $T) -> Option<$T> { - match v { - 0 => None, - v => Some(self / v), - } + /// Checked integer division. Computes `self / other`, returning `None` + /// if `other == 0` or the operation results in underflow or overflow. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// assert_eq!((-127i8).checked_div(-1), Some(127)); + /// assert_eq!((-128i8).checked_div(-1), None); + /// assert_eq!((1i8).checked_div(0), None); + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn checked_div(self, v: $T) -> Option<$T> { + match v { + 0 => None, + v => Some(self / v), } + } - /// Saturating integer addition. Computes `self + other`, saturating at - /// the numeric bounds instead of overflowing. - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn saturating_add(self, other: $T) -> $T { - match self.checked_add(other) { - Some(x) => x, - None if other >= <$T>::zero() => <$T>::max_value(), - None => <$T>::min_value(), - } + /// Saturating integer addition. Computes `self + other`, saturating at + /// the numeric bounds instead of overflowing. + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn saturating_add(self, other: $T) -> $T { + match self.checked_add(other) { + Some(x) => x, + None if other >= <$T>::zero() => <$T>::max_value(), + None => <$T>::min_value(), } + } - /// Saturating integer subtraction. Computes `self - other`, saturating at - /// the numeric bounds instead of overflowing. - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn saturating_sub(self, other: $T) -> $T { - match self.checked_sub(other) { - Some(x) => x, - None if other >= <$T>::zero() => <$T>::min_value(), - None => <$T>::max_value(), - } + /// Saturating integer subtraction. Computes `self - other`, saturating + /// at the numeric bounds instead of overflowing. + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn saturating_sub(self, other: $T) -> $T { + match self.checked_sub(other) { + Some(x) => x, + None if other >= <$T>::zero() => <$T>::min_value(), + None => <$T>::max_value(), } + } - /// Raises self to the power of `exp`, using exponentiation by squaring. - /// - /// # Examples - /// - /// ```rust - /// use std::num::Int; - /// - /// assert_eq!(2.pow(4), 16); - /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] - #[inline] - pub fn pow(self, mut exp: u32) -> $T { - let mut base = self; - let mut acc = <$T>::one(); - - let mut prev_base = self; - let mut base_oflo = false; - while exp > 0 { - if (exp & 1) == 1 { - if base_oflo { - // ensure overflow occurs in the same manner it - // would have otherwise (i.e. signal any exception - // it would have otherwise). - acc = acc * (prev_base * prev_base); - } else { - acc = acc * base; - } + /// Raises self to the power of `exp`, using exponentiation by squaring. + /// + /// # Examples + /// + /// ```rust + /// use std::num::Int; + /// + /// assert_eq!(2.pow(4), 16); + /// ``` + #[unstable(feature = "core", + reason = "pending integer conventions")] + #[inline] + pub fn pow(self, mut exp: u32) -> $T { + let mut base = self; + let mut acc = <$T>::one(); + + let mut prev_base = self; + let mut base_oflo = false; + while exp > 0 { + if (exp & 1) == 1 { + if base_oflo { + // ensure overflow occurs in the same manner it + // would have otherwise (i.e. signal any exception + // it would have otherwise). + acc = acc * (prev_base * prev_base); + } else { + acc = acc * base; } - prev_base = base; - let (new_base, new_base_oflo) = base.overflowing_mul(base); - base = new_base; - base_oflo = new_base_oflo; - exp /= 2; } - acc + prev_base = base; + let (new_base, new_base_oflo) = base.overflowing_mul(base); + base = new_base; + base_oflo = new_base_oflo; + exp /= 2; } + acc + } - /// Returns `true` iff `self == 2^k` for some `k`. - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn is_power_of_two(self) -> bool { - (self.wrapping_sub(<$T>::one())) & self == <$T>::zero() && !(self == <$T>::zero()) - } + /// Returns `true` iff `self == 2^k` for some `k`. + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn is_power_of_two(self) -> bool { + (self.wrapping_sub(<$T>::one())) & self == <$T>::zero() && + !(self == <$T>::zero()) + } - /// Returns the smallest power of two greater than or equal to `self`. - /// Unspecified behavior on overflow. - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn next_power_of_two(self) -> $T { - let bits = size_of::<$T>() * 8; - let one: $T = <$T>::one(); - one << ((bits - self.wrapping_sub(one).leading_zeros() as usize) % bits) - } + /// Returns the smallest power of two greater than or equal to `self`. + /// Unspecified behavior on overflow. + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn next_power_of_two(self) -> $T { + let bits = size_of::<$T>() * 8; + let one: $T = <$T>::one(); + one << ((bits - self.wrapping_sub(one).leading_zeros() as usize) % bits) + } - /// Returns the smallest power of two greater than or equal to `n`. If the - /// next power of two is greater than the type's maximum value, `None` is - /// returned, otherwise the power of two is wrapped in `Some`. - #[stable(feature = "rust1", since = "1.0.0")] - pub fn checked_next_power_of_two(self) -> Option<$T> { - let npot = self.next_power_of_two(); - if npot >= self { - Some(npot) - } else { - None - } + /// Returns the smallest power of two greater than or equal to `n`. If + /// the next power of two is greater than the type's maximum value, + /// `None` is returned, otherwise the power of two is wrapped in `Some`. + #[stable(feature = "rust1", since = "1.0.0")] + pub fn checked_next_power_of_two(self) -> Option<$T> { + let npot = self.next_power_of_two(); + if npot >= self { + Some(npot) + } else { + None } } } +} - /// Swapping a single byte is a no-op. This is marked as `unsafe` for - /// consistency with the other `bswap` intrinsics. - unsafe fn bswap8(x: u8) -> u8 { x } - - #[lang = "u8"] - impl u8 { - uint_impl! { u8 = u8, 8, - intrinsics::ctpop8, - intrinsics::ctlz8, - intrinsics::cttz8, - bswap8, - intrinsics::u8_add_with_overflow, - intrinsics::u8_sub_with_overflow, - intrinsics::u8_mul_with_overflow } - } +#[lang = "u8"] +impl u8 { + uint_impl! { u8 = u8, 8, + intrinsics::ctpop8, + intrinsics::ctlz8, + intrinsics::cttz8, + bswap8, + intrinsics::u8_add_with_overflow, + intrinsics::u8_sub_with_overflow, + intrinsics::u8_mul_with_overflow } +} - #[lang = "u16"] - impl u16 { - uint_impl! { u16 = u16, 16, - intrinsics::ctpop16, - intrinsics::ctlz16, - intrinsics::cttz16, - intrinsics::bswap16, - intrinsics::u16_add_with_overflow, - intrinsics::u16_sub_with_overflow, - intrinsics::u16_mul_with_overflow } - } +#[lang = "u16"] +impl u16 { + uint_impl! { u16 = u16, 16, + intrinsics::ctpop16, + intrinsics::ctlz16, + intrinsics::cttz16, + intrinsics::bswap16, + intrinsics::u16_add_with_overflow, + intrinsics::u16_sub_with_overflow, + intrinsics::u16_mul_with_overflow } +} - #[lang = "u32"] - impl u32 { - uint_impl! { u32 = u32, 32, - intrinsics::ctpop32, - intrinsics::ctlz32, - intrinsics::cttz32, - intrinsics::bswap32, - intrinsics::u32_add_with_overflow, - intrinsics::u32_sub_with_overflow, - intrinsics::u32_mul_with_overflow } - } +#[lang = "u32"] +impl u32 { + uint_impl! { u32 = u32, 32, + intrinsics::ctpop32, + intrinsics::ctlz32, + intrinsics::cttz32, + intrinsics::bswap32, + intrinsics::u32_add_with_overflow, + intrinsics::u32_sub_with_overflow, + intrinsics::u32_mul_with_overflow } +} - #[lang = "u64"] - impl u64 { - uint_impl! { u64 = u64, 64, - intrinsics::ctpop64, - intrinsics::ctlz64, - intrinsics::cttz64, - intrinsics::bswap64, - intrinsics::u64_add_with_overflow, - intrinsics::u64_sub_with_overflow, - intrinsics::u64_mul_with_overflow } - } +#[lang = "u64"] +impl u64 { + uint_impl! { u64 = u64, 64, + intrinsics::ctpop64, + intrinsics::ctlz64, + intrinsics::cttz64, + intrinsics::bswap64, + intrinsics::u64_add_with_overflow, + intrinsics::u64_sub_with_overflow, + intrinsics::u64_mul_with_overflow } +} - #[cfg(target_pointer_width = "32")] - #[lang = "usize"] - impl usize { - uint_impl! { uint = u32, 32, - intrinsics::ctpop32, - intrinsics::ctlz32, - intrinsics::cttz32, - intrinsics::bswap32, - intrinsics::u32_add_with_overflow, - intrinsics::u32_sub_with_overflow, - intrinsics::u32_mul_with_overflow } - } +#[cfg(target_pointer_width = "32")] +#[lang = "usize"] +impl usize { + uint_impl! { uint = u32, 32, + intrinsics::ctpop32, + intrinsics::ctlz32, + intrinsics::cttz32, + intrinsics::bswap32, + intrinsics::u32_add_with_overflow, + intrinsics::u32_sub_with_overflow, + intrinsics::u32_mul_with_overflow } +} - #[cfg(target_pointer_width = "64")] - #[lang = "usize"] - impl usize { - uint_impl! { uint = u64, 64, - intrinsics::ctpop64, - intrinsics::ctlz64, - intrinsics::cttz64, - intrinsics::bswap64, - intrinsics::u64_add_with_overflow, - intrinsics::u64_sub_with_overflow, - intrinsics::u64_mul_with_overflow } - } +#[cfg(target_pointer_width = "64")] +#[lang = "usize"] +impl usize { + uint_impl! { uint = u64, 64, + intrinsics::ctpop64, + intrinsics::ctlz64, + intrinsics::cttz64, + intrinsics::bswap64, + intrinsics::u64_add_with_overflow, + intrinsics::u64_sub_with_overflow, + intrinsics::u64_mul_with_overflow } } /// A generic trait for converting a value to a number. diff --git a/src/libcore/prelude.rs b/src/libcore/prelude.rs index fb793a62390..c668fe80d14 100644 --- a/src/libcore/prelude.rs +++ b/src/libcore/prelude.rs @@ -42,8 +42,6 @@ pub use iter::{Extend, IteratorExt}; pub use iter::{Iterator, DoubleEndedIterator}; pub use iter::{ExactSizeIterator}; pub use option::Option::{self, Some, None}; -#[cfg(stage0)] -pub use ptr::{PtrExt, MutPtrExt}; pub use result::Result::{self, Ok, Err}; pub use slice::{AsSlice, SliceExt}; pub use str::{Str, StrExt}; diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index f28c26d1798..1cbea057e88 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -262,145 +262,13 @@ pub unsafe fn write<T>(dst: *mut T, src: T) { intrinsics::move_val_init(&mut *dst, src) } -#[cfg(stage0)] -/// Methods on raw pointers -#[stable(feature = "rust1", since = "1.0.0")] -pub trait PtrExt { - /// The type which is being pointed at - type Target: ?Sized; - - /// Returns true if the pointer is null. - #[stable(feature = "rust1", since = "1.0.0")] - fn is_null(self) -> bool; - - /// Returns `None` if the pointer is null, or else returns a reference to - /// the value wrapped in `Some`. - /// - /// # Safety - /// - /// While this method and its mutable counterpart are useful for - /// null-safety, it is important to note that this is still an unsafe - /// operation because the returned value could be pointing to invalid - /// memory. - #[unstable(feature = "core", - reason = "Option is not clearly the right return type, and we may want \ - to tie the return lifetime to a borrow of the raw pointer")] - unsafe fn as_ref<'a>(&self) -> Option<&'a Self::Target>; - - /// Calculates the offset from a pointer. `count` is in units of T; e.g. a - /// `count` of 3 represents a pointer offset of `3 * sizeof::<T>()` bytes. - /// - /// # Safety - /// - /// The offset must be in-bounds of the object, or one-byte-past-the-end. - /// Otherwise `offset` invokes Undefined Behaviour, regardless of whether - /// the pointer is used. - #[stable(feature = "rust1", since = "1.0.0")] - unsafe fn offset(self, count: isize) -> Self where Self::Target: Sized; -} - -#[cfg(stage0)] -/// Methods on mutable raw pointers -#[stable(feature = "rust1", since = "1.0.0")] -pub trait MutPtrExt { - /// The type which is being pointed at - type Target: ?Sized; - - /// Returns `None` if the pointer is null, or else returns a mutable - /// reference to the value wrapped in `Some`. - /// - /// # Safety - /// - /// As with `as_ref`, this is unsafe because it cannot verify the validity - /// of the returned pointer. - #[unstable(feature = "core", - reason = "Option is not clearly the right return type, and we may want \ - to tie the return lifetime to a borrow of the raw pointer")] - unsafe fn as_mut<'a>(&self) -> Option<&'a mut Self::Target>; -} - -#[cfg(stage0)] -#[stable(feature = "rust1", since = "1.0.0")] -impl<T: ?Sized> PtrExt for *const T { - type Target = T; - - #[inline] - #[stable(feature = "rust1", since = "1.0.0")] - fn is_null(self) -> bool { self == 0 as *const T } - - #[inline] - #[stable(feature = "rust1", since = "1.0.0")] - unsafe fn offset(self, count: isize) -> *const T where T: Sized { - intrinsics::offset(self, count) - } - - #[inline] - #[unstable(feature = "core", - reason = "return value does not necessarily convey all possible \ - information")] - unsafe fn as_ref<'a>(&self) -> Option<&'a T> { - if self.is_null() { - None - } else { - Some(&**self) - } - } -} - -#[cfg(stage0)] -#[stable(feature = "rust1", since = "1.0.0")] -impl<T: ?Sized> PtrExt for *mut T { - type Target = T; - - #[inline] - #[stable(feature = "rust1", since = "1.0.0")] - fn is_null(self) -> bool { self == 0 as *mut T } - - #[inline] - #[stable(feature = "rust1", since = "1.0.0")] - unsafe fn offset(self, count: isize) -> *mut T where T: Sized { - intrinsics::offset(self, count) as *mut T - } - - #[inline] - #[unstable(feature = "core", - reason = "return value does not necessarily convey all possible \ - information")] - unsafe fn as_ref<'a>(&self) -> Option<&'a T> { - if self.is_null() { - None - } else { - Some(&**self) - } - } -} - -#[cfg(stage0)] -#[stable(feature = "rust1", since = "1.0.0")] -impl<T: ?Sized> MutPtrExt for *mut T { - type Target = T; - - #[inline] - #[unstable(feature = "core", - reason = "return value does not necessarily convey all possible \ - information")] - unsafe fn as_mut<'a>(&self) -> Option<&'a mut T> { - if self.is_null() { - None - } else { - Some(&mut **self) - } - } -} - -#[cfg(not(stage0))] #[stable(feature = "rust1", since = "1.0.0")] #[lang = "const_ptr"] impl<T: ?Sized> *const T { /// Returns true if the pointer is null. #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn is_null(self) -> bool { + pub fn is_null(self) -> bool where T: Sized { self == 0 as *const T } @@ -417,7 +285,7 @@ impl<T: ?Sized> *const T { reason = "Option is not clearly the right return type, and we may want \ to tie the return lifetime to a borrow of the raw pointer")] #[inline] - pub unsafe fn as_ref<'a>(&self) -> Option<&'a T> { + pub unsafe fn as_ref<'a>(&self) -> Option<&'a T> where T: Sized { if self.is_null() { None } else { @@ -440,14 +308,13 @@ impl<T: ?Sized> *const T { } } -#[cfg(not(stage0))] #[stable(feature = "rust1", since = "1.0.0")] #[lang = "mut_ptr"] impl<T: ?Sized> *mut T { /// Returns true if the pointer is null. #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn is_null(self) -> bool { + pub fn is_null(self) -> bool where T: Sized { self == 0 as *mut T } @@ -464,7 +331,7 @@ impl<T: ?Sized> *mut T { reason = "Option is not clearly the right return type, and we may want \ to tie the return lifetime to a borrow of the raw pointer")] #[inline] - pub unsafe fn as_ref<'a>(&self) -> Option<&'a T> { + pub unsafe fn as_ref<'a>(&self) -> Option<&'a T> where T: Sized { if self.is_null() { None } else { @@ -497,7 +364,7 @@ impl<T: ?Sized> *mut T { reason = "return value does not necessarily convey all possible \ information")] #[inline] - pub unsafe fn as_mut<'a>(&self) -> Option<&'a mut T> { + pub unsafe fn as_mut<'a>(&self) -> Option<&'a mut T> where T: Sized { if self.is_null() { None } else { diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index b1576c0d377..907b2eba80c 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -49,8 +49,6 @@ use option::Option::{None, Some}; use result::Result; use result::Result::{Ok, Err}; use ptr; -#[cfg(stage0)] -use ptr::PtrExt; use mem; use mem::size_of; use marker::{Send, Sized, Sync, self}; diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 6b83338e1d2..e8181395b5c 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -31,8 +31,6 @@ use mem; use num::Int; use ops::{Fn, FnMut}; use option::Option::{self, None, Some}; -#[cfg(stage0)] -use ptr::PtrExt; use raw::{Repr, Slice}; use result::Result::{self, Ok, Err}; use slice::{self, SliceExt}; |
