diff options
| author | Brian Anderson <banderson@mozilla.com> | 2015-02-19 21:05:35 -0800 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2015-03-02 16:12:46 -0800 |
| commit | 76e9fa63ba0b6d892aa880db9c8373ede3e67c03 (patch) | |
| tree | 5c932ad4ef0079ca1f4a8b3d0767f7e5984df2a5 /src/libcore/num | |
| parent | 2ca6eaedae9ec4bff2a63f81f473aba653e46ac5 (diff) | |
| download | rust-76e9fa63ba0b6d892aa880db9c8373ede3e67c03.tar.gz rust-76e9fa63ba0b6d892aa880db9c8373ede3e67c03.zip | |
core: Audit num module for int/uint
* count_ones/zeros, trailing_ones/zeros return u32, not usize * rotate_left/right take u32, not usize * RADIX, MANTISSA_DIGITS, DIGITS, BITS, BYTES are u32, not usize Doesn't touch pow because there's another PR for it. [breaking-change]
Diffstat (limited to 'src/libcore/num')
| -rw-r--r-- | src/libcore/num/f32.rs | 26 | ||||
| -rw-r--r-- | src/libcore/num/f64.rs | 26 | ||||
| -rw-r--r-- | src/libcore/num/int_macros.rs | 4 | ||||
| -rw-r--r-- | src/libcore/num/mod.rs | 110 | ||||
| -rw-r--r-- | src/libcore/num/uint_macros.rs | 4 |
5 files changed, 115 insertions, 55 deletions
diff --git a/src/libcore/num/f32.rs b/src/libcore/num/f32.rs index b542c9d47f7..0d8e3044ecc 100644 --- a/src/libcore/num/f32.rs +++ b/src/libcore/num/f32.rs @@ -23,12 +23,12 @@ use num::FpCategory as Fp; use option::Option; #[unstable(feature = "core", reason = "pending integer conventions")] -pub const RADIX: uint = 2; +pub const RADIX: u32 = 2; #[unstable(feature = "core", reason = "pending integer conventions")] -pub const MANTISSA_DIGITS: uint = 24; +pub const MANTISSA_DIGITS: u32 = 24; #[unstable(feature = "core", reason = "pending integer conventions")] -pub const DIGITS: uint = 6; +pub const DIGITS: u32 = 6; #[stable(feature = "rust1", since = "1.0.0")] pub const EPSILON: f32 = 1.19209290e-07_f32; @@ -57,14 +57,14 @@ pub const MIN_POSITIVE: f32 = 1.17549435e-38_f32; pub const MAX: f32 = 3.40282347e+38_f32; #[unstable(feature = "core", reason = "pending integer conventions")] -pub const MIN_EXP: int = -125; +pub const MIN_EXP: i32 = -125; #[unstable(feature = "core", reason = "pending integer conventions")] -pub const MAX_EXP: int = 128; +pub const MAX_EXP: i32 = 128; #[unstable(feature = "core", reason = "pending integer conventions")] -pub const MIN_10_EXP: int = -37; +pub const MIN_10_EXP: i32 = -37; #[unstable(feature = "core", reason = "pending integer conventions")] -pub const MAX_10_EXP: int = 38; +pub const MAX_10_EXP: i32 = 38; #[stable(feature = "rust1", since = "1.0.0")] pub const NAN: f32 = 0.0_f32/0.0_f32; @@ -193,12 +193,12 @@ impl Float for f32 { #[inline] #[unstable(feature = "core")] #[deprecated(since = "1.0.0")] - fn mantissa_digits(_: Option<f32>) -> uint { MANTISSA_DIGITS } + fn mantissa_digits(_: Option<f32>) -> uint { MANTISSA_DIGITS as uint } #[inline] #[unstable(feature = "core")] #[deprecated(since = "1.0.0")] - fn digits(_: Option<f32>) -> uint { DIGITS } + fn digits(_: Option<f32>) -> uint { DIGITS as uint } #[inline] #[unstable(feature = "core")] @@ -208,22 +208,22 @@ impl Float for f32 { #[inline] #[unstable(feature = "core")] #[deprecated(since = "1.0.0")] - fn min_exp(_: Option<f32>) -> int { MIN_EXP } + fn min_exp(_: Option<f32>) -> int { MIN_EXP as int } #[inline] #[unstable(feature = "core")] #[deprecated(since = "1.0.0")] - fn max_exp(_: Option<f32>) -> int { MAX_EXP } + fn max_exp(_: Option<f32>) -> int { MAX_EXP as int } #[inline] #[unstable(feature = "core")] #[deprecated(since = "1.0.0")] - fn min_10_exp(_: Option<f32>) -> int { MIN_10_EXP } + fn min_10_exp(_: Option<f32>) -> int { MIN_10_EXP as int } #[inline] #[unstable(feature = "core")] #[deprecated(since = "1.0.0")] - fn max_10_exp(_: Option<f32>) -> int { MAX_10_EXP } + fn max_10_exp(_: Option<f32>) -> int { MAX_10_EXP as int } #[inline] #[unstable(feature = "core")] diff --git a/src/libcore/num/f64.rs b/src/libcore/num/f64.rs index 2aae7107548..d7e91058a46 100644 --- a/src/libcore/num/f64.rs +++ b/src/libcore/num/f64.rs @@ -27,11 +27,11 @@ use option::Option; // members of `Bounded` and `Float`. #[unstable(feature = "core", reason = "pending integer conventions")] -pub const RADIX: uint = 2; +pub const RADIX: u32 = 2; -pub const MANTISSA_DIGITS: uint = 53; +pub const MANTISSA_DIGITS: u32 = 53; #[unstable(feature = "core", reason = "pending integer conventions")] -pub const DIGITS: uint = 15; +pub const DIGITS: u32 = 15; #[stable(feature = "rust1", since = "1.0.0")] pub const EPSILON: f64 = 2.2204460492503131e-16_f64; @@ -60,14 +60,14 @@ pub const MIN_POSITIVE: f64 = 2.2250738585072014e-308_f64; pub const MAX: f64 = 1.7976931348623157e+308_f64; #[unstable(feature = "core", reason = "pending integer conventions")] -pub const MIN_EXP: int = -1021; +pub const MIN_EXP: i32 = -1021; #[unstable(feature = "core", reason = "pending integer conventions")] -pub const MAX_EXP: int = 1024; +pub const MAX_EXP: i32 = 1024; #[unstable(feature = "core", reason = "pending integer conventions")] -pub const MIN_10_EXP: int = -307; +pub const MIN_10_EXP: i32 = -307; #[unstable(feature = "core", reason = "pending integer conventions")] -pub const MAX_10_EXP: int = 308; +pub const MAX_10_EXP: i32 = 308; #[stable(feature = "rust1", since = "1.0.0")] pub const NAN: f64 = 0.0_f64/0.0_f64; @@ -200,12 +200,12 @@ impl Float for f64 { #[inline] #[unstable(feature = "core")] #[deprecated(since = "1.0.0")] - fn mantissa_digits(_: Option<f64>) -> uint { MANTISSA_DIGITS } + fn mantissa_digits(_: Option<f64>) -> uint { MANTISSA_DIGITS as uint } #[inline] #[unstable(feature = "core")] #[deprecated(since = "1.0.0")] - fn digits(_: Option<f64>) -> uint { DIGITS } + fn digits(_: Option<f64>) -> uint { DIGITS as uint } #[inline] #[unstable(feature = "core")] @@ -215,22 +215,22 @@ impl Float for f64 { #[inline] #[unstable(feature = "core")] #[deprecated(since = "1.0.0")] - fn min_exp(_: Option<f64>) -> int { MIN_EXP } + fn min_exp(_: Option<f64>) -> int { MIN_EXP as int } #[inline] #[unstable(feature = "core")] #[deprecated(since = "1.0.0")] - fn max_exp(_: Option<f64>) -> int { MAX_EXP } + fn max_exp(_: Option<f64>) -> int { MAX_EXP as int } #[inline] #[unstable(feature = "core")] #[deprecated(since = "1.0.0")] - fn min_10_exp(_: Option<f64>) -> int { MIN_10_EXP } + fn min_10_exp(_: Option<f64>) -> int { MIN_10_EXP as int } #[inline] #[unstable(feature = "core")] #[deprecated(since = "1.0.0")] - fn max_10_exp(_: Option<f64>) -> int { MAX_10_EXP } + fn max_10_exp(_: Option<f64>) -> int { MAX_10_EXP as int } #[inline] #[unstable(feature = "core")] diff --git a/src/libcore/num/int_macros.rs b/src/libcore/num/int_macros.rs index 954c8a08e64..fe0d6d13c4c 100644 --- a/src/libcore/num/int_macros.rs +++ b/src/libcore/num/int_macros.rs @@ -15,11 +15,11 @@ macro_rules! int_module { ($T:ty, $bits:expr) => ( // FIXME(#11621): Should be deprecated once CTFE is implemented in favour of // calling the `mem::size_of` function. #[unstable(feature = "core")] -pub const BITS : uint = $bits; +pub const BITS : u32 = $bits; // FIXME(#11621): Should be deprecated once CTFE is implemented in favour of // calling the `mem::size_of` function. #[unstable(feature = "core")] -pub const BYTES : uint = ($bits / 8); +pub const BYTES : u32 = ($bits / 8); // FIXME(#11621): Should be deprecated once CTFE is implemented in favour of // calling the `Bounded::min_value` function. diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 318799f59a8..d77a1eb4203 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -86,7 +86,7 @@ pub trait Int /// ``` #[unstable(feature = "core", reason = "pending integer conventions")] - fn count_ones(self) -> uint; + fn count_ones(self) -> u32; /// Returns the number of zeros in the binary representation of `self`. /// @@ -102,7 +102,7 @@ pub trait Int #[unstable(feature = "core", reason = "pending integer conventions")] #[inline] - fn count_zeros(self) -> uint { + fn count_zeros(self) -> u32 { (!self).count_ones() } @@ -120,7 +120,7 @@ pub trait Int /// ``` #[unstable(feature = "core", reason = "pending integer conventions")] - fn leading_zeros(self) -> uint; + fn leading_zeros(self) -> u32; /// Returns the number of trailing zeros in the binary representation /// of `self`. @@ -136,7 +136,7 @@ pub trait Int /// ``` #[unstable(feature = "core", reason = "pending integer conventions")] - fn trailing_zeros(self) -> uint; + fn trailing_zeros(self) -> u32; /// Shifts the bits to the left by a specified amount amount, `n`, wrapping /// the truncated bits to the end of the resulting integer. @@ -153,7 +153,7 @@ pub trait Int /// ``` #[unstable(feature = "core", reason = "pending integer conventions")] - fn rotate_left(self, n: uint) -> Self; + fn rotate_left(self, n: u32) -> Self; /// Shifts the bits to the right by a specified amount amount, `n`, wrapping /// the truncated bits to the beginning of the resulting integer. @@ -170,7 +170,7 @@ pub trait Int /// ``` #[unstable(feature = "core", reason = "pending integer conventions")] - fn rotate_right(self, n: uint) -> Self; + fn rotate_right(self, n: u32) -> Self; /// Reverses the byte order of the integer. /// @@ -418,23 +418,23 @@ macro_rules! uint_impl { fn max_value() -> $T { -1 } #[inline] - fn count_ones(self) -> uint { unsafe { $ctpop(self as $ActualT) as uint } } + fn count_ones(self) -> u32 { unsafe { $ctpop(self as $ActualT) as u32 } } #[inline] - fn leading_zeros(self) -> uint { unsafe { $ctlz(self as $ActualT) as uint } } + fn leading_zeros(self) -> u32 { unsafe { $ctlz(self as $ActualT) as u32 } } #[inline] - fn trailing_zeros(self) -> uint { unsafe { $cttz(self as $ActualT) as uint } } + fn trailing_zeros(self) -> u32 { unsafe { $cttz(self as $ActualT) as u32 } } #[inline] - fn rotate_left(self, n: uint) -> $T { + 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)) } #[inline] - fn rotate_right(self, n: uint) -> $T { + 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)) @@ -549,19 +549,19 @@ macro_rules! int_impl { fn max_value() -> $T { let min: $T = Int::min_value(); !min } #[inline] - fn count_ones(self) -> uint { (self as $UnsignedT).count_ones() } + fn count_ones(self) -> u32 { (self as $UnsignedT).count_ones() } #[inline] - fn leading_zeros(self) -> uint { (self as $UnsignedT).leading_zeros() } + fn leading_zeros(self) -> u32 { (self as $UnsignedT).leading_zeros() } #[inline] - fn trailing_zeros(self) -> uint { (self as $UnsignedT).trailing_zeros() } + fn trailing_zeros(self) -> u32 { (self as $UnsignedT).trailing_zeros() } #[inline] - fn rotate_left(self, n: uint) -> $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: uint) -> $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 } @@ -706,7 +706,7 @@ pub trait UnsignedInt: Int { fn next_power_of_two(self) -> Self { let bits = size_of::<Self>() * 8; let one: Self = Int::one(); - one << ((bits - (self - one).leading_zeros()) % bits) + one << ((bits - (self - one).leading_zeros() as usize) % bits) } /// Returns the smallest power of two greater than or equal to `n`. If the @@ -743,8 +743,16 @@ impl UnsignedInt for u64 {} pub trait ToPrimitive { /// Converts the value of `self` to an `int`. #[inline] + #[unstable(feature = "core")] + #[deprecated(since = "1.0.0", reason = "use to_isize")] fn to_int(&self) -> Option<int> { - self.to_i64().and_then(|x| x.to_int()) + self.to_i64().and_then(|x| x.to_isize()) + } + + /// Converts the value of `self` to an `isize`. + #[inline] + fn to_isize(&self) -> Option<isize> { + self.to_i64().and_then(|x| x.to_isize()) } /// Converts the value of `self` to an `i8`. @@ -770,8 +778,16 @@ pub trait ToPrimitive { /// Converts the value of `self` to an `uint`. #[inline] + #[unstable(feature = "core")] + #[deprecated(since = "1.0.0", reason = "use to_usize")] fn to_uint(&self) -> Option<uint> { - self.to_u64().and_then(|x| x.to_uint()) + self.to_u64().and_then(|x| x.to_usize()) + } + + /// Converts the value of `self` to a `usize`. + #[inline] + fn to_usize(&self) -> Option<usize> { + self.to_u64().and_then(|x| x.to_usize()) } /// Converts the value of `self` to an `u8`. @@ -848,6 +864,8 @@ macro_rules! impl_to_primitive_int { #[inline] fn to_int(&self) -> Option<int> { impl_to_primitive_int_to_int!($T, int, *self) } #[inline] + fn to_isize(&self) -> Option<isize> { impl_to_primitive_int_to_int!($T, isize, *self) } + #[inline] fn to_i8(&self) -> Option<i8> { impl_to_primitive_int_to_int!($T, i8, *self) } #[inline] fn to_i16(&self) -> Option<i16> { impl_to_primitive_int_to_int!($T, i16, *self) } @@ -859,6 +877,8 @@ macro_rules! impl_to_primitive_int { #[inline] fn to_uint(&self) -> Option<uint> { impl_to_primitive_int_to_uint!($T, uint, *self) } #[inline] + fn to_usize(&self) -> Option<usize> { impl_to_primitive_int_to_uint!($T, usize, *self) } + #[inline] fn to_u8(&self) -> Option<u8> { impl_to_primitive_int_to_uint!($T, u8, *self) } #[inline] fn to_u16(&self) -> Option<u16> { impl_to_primitive_int_to_uint!($T, u16, *self) } @@ -875,7 +895,7 @@ macro_rules! impl_to_primitive_int { ) } -impl_to_primitive_int! { int } +impl_to_primitive_int! { isize } impl_to_primitive_int! { i8 } impl_to_primitive_int! { i16 } impl_to_primitive_int! { i32 } @@ -918,6 +938,8 @@ macro_rules! impl_to_primitive_uint { #[inline] fn to_int(&self) -> Option<int> { impl_to_primitive_uint_to_int!(int, *self) } #[inline] + fn to_isize(&self) -> Option<int> { impl_to_primitive_uint_to_int!(isize, *self) } + #[inline] fn to_i8(&self) -> Option<i8> { impl_to_primitive_uint_to_int!(i8, *self) } #[inline] fn to_i16(&self) -> Option<i16> { impl_to_primitive_uint_to_int!(i16, *self) } @@ -929,6 +951,8 @@ macro_rules! impl_to_primitive_uint { #[inline] fn to_uint(&self) -> Option<uint> { impl_to_primitive_uint_to_uint!($T, uint, *self) } #[inline] + fn to_usize(&self) -> Option<uint> { impl_to_primitive_uint_to_uint!($T, usize, *self) } + #[inline] fn to_u8(&self) -> Option<u8> { impl_to_primitive_uint_to_uint!($T, u8, *self) } #[inline] fn to_u16(&self) -> Option<u16> { impl_to_primitive_uint_to_uint!($T, u16, *self) } @@ -945,7 +969,7 @@ macro_rules! impl_to_primitive_uint { ) } -impl_to_primitive_uint! { uint } +impl_to_primitive_uint! { usize } impl_to_primitive_uint! { u8 } impl_to_primitive_uint! { u16 } impl_to_primitive_uint! { u32 } @@ -973,6 +997,8 @@ macro_rules! impl_to_primitive_float { #[inline] fn to_int(&self) -> Option<int> { Some(*self as int) } #[inline] + fn to_isize(&self) -> Option<int> { Some(*self as isize) } + #[inline] fn to_i8(&self) -> Option<i8> { Some(*self as i8) } #[inline] fn to_i16(&self) -> Option<i16> { Some(*self as i16) } @@ -984,6 +1010,8 @@ macro_rules! impl_to_primitive_float { #[inline] fn to_uint(&self) -> Option<uint> { Some(*self as uint) } #[inline] + fn to_usize(&self) -> Option<uint> { Some(*self as usize) } + #[inline] fn to_u8(&self) -> Option<u8> { Some(*self as u8) } #[inline] fn to_u16(&self) -> Option<u16> { Some(*self as u16) } @@ -1009,10 +1037,19 @@ pub trait FromPrimitive : ::marker::Sized { /// Convert an `int` to return an optional value of this type. If the /// value cannot be represented by this value, the `None` is returned. #[inline] + #[unstable(feature = "core")] + #[deprecated(since = "1.0.0", reason = "use from_isize")] fn from_int(n: int) -> Option<Self> { FromPrimitive::from_i64(n as i64) } + /// Convert an `isize` to return an optional value of this type. If the + /// value cannot be represented by this value, the `None` is returned. + #[inline] + fn from_isize(n: isize) -> Option<Self> { + FromPrimitive::from_i64(n as i64) + } + /// Convert an `i8` to return an optional value of this type. If the /// type cannot be represented by this value, the `None` is returned. #[inline] @@ -1041,10 +1078,19 @@ pub trait FromPrimitive : ::marker::Sized { /// Convert an `uint` to return an optional value of this type. If the /// type cannot be represented by this value, the `None` is returned. #[inline] + #[unstable(feature = "core")] + #[deprecated(since = "1.0.0", reason = "use from_usize")] fn from_uint(n: uint) -> Option<Self> { FromPrimitive::from_u64(n as u64) } + /// Convert a `usize` to return an optional value of this type. If the + /// type cannot be represented by this value, the `None` is returned. + #[inline] + fn from_usize(n: usize) -> Option<Self> { + FromPrimitive::from_u64(n as u64) + } + /// Convert an `u8` to return an optional value of this type. If the /// type cannot be represented by this value, the `None` is returned. #[inline] @@ -1087,8 +1133,15 @@ pub trait FromPrimitive : ::marker::Sized { /// A utility function that just calls `FromPrimitive::from_int`. #[unstable(feature = "core", reason = "likely to be removed")] +#[deprecated(since = "1.0.0", reason = "use from_isize")] pub fn from_int<A: FromPrimitive>(n: int) -> Option<A> { - FromPrimitive::from_int(n) + FromPrimitive::from_isize(n) +} + +/// A utility function that just calls `FromPrimitive::from_isize`. +#[unstable(feature = "core", reason = "likely to be removed")] +pub fn from_isize<A: FromPrimitive>(n: isize) -> Option<A> { + FromPrimitive::from_isize(n) } /// A utility function that just calls `FromPrimitive::from_i8`. @@ -1117,8 +1170,15 @@ pub fn from_i64<A: FromPrimitive>(n: i64) -> Option<A> { /// A utility function that just calls `FromPrimitive::from_uint`. #[unstable(feature = "core", reason = "likely to be removed")] +#[deprecated(since = "1.0.0", reason = "use from_uint")] pub fn from_uint<A: FromPrimitive>(n: uint) -> Option<A> { - FromPrimitive::from_uint(n) + FromPrimitive::from_usize(n) +} + +/// A utility function that just calls `FromPrimitive::from_usize`. +#[unstable(feature = "core", reason = "likely to be removed")] +pub fn from_usize<A: FromPrimitive>(n: usize) -> Option<A> { + FromPrimitive::from_usize(n) } /// A utility function that just calls `FromPrimitive::from_u8`. @@ -1718,12 +1778,12 @@ macro_rules! from_str_radix_int_impl { } } } -from_str_radix_int_impl! { int } +from_str_radix_int_impl! { isize } from_str_radix_int_impl! { i8 } from_str_radix_int_impl! { i16 } from_str_radix_int_impl! { i32 } from_str_radix_int_impl! { i64 } -from_str_radix_int_impl! { uint } +from_str_radix_int_impl! { usize } from_str_radix_int_impl! { u8 } from_str_radix_int_impl! { u16 } from_str_radix_int_impl! { u32 } diff --git a/src/libcore/num/uint_macros.rs b/src/libcore/num/uint_macros.rs index 06502be54aa..330f0b91bf1 100644 --- a/src/libcore/num/uint_macros.rs +++ b/src/libcore/num/uint_macros.rs @@ -13,9 +13,9 @@ macro_rules! uint_module { ($T:ty, $T_SIGNED:ty, $bits:expr) => ( #[unstable(feature = "core")] -pub const BITS : uint = $bits; +pub const BITS : u32 = $bits; #[unstable(feature = "core")] -pub const BYTES : uint = ($bits / 8); +pub const BYTES : u32 = ($bits / 8); #[stable(feature = "rust1", since = "1.0.0")] pub const MIN: $T = 0 as $T; |
