diff options
| author | bors <bors@rust-lang.org> | 2014-01-18 05:36:47 -0800 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-01-18 05:36:47 -0800 |
| commit | 2952685917cb17a3d849cb02d17ce71ccecfb855 (patch) | |
| tree | cbd9d41eab2707b91ad0f24765d4a2a6e6935370 /src/libstd | |
| parent | fb40bdbb6295a989806e7c3dcc23631becdb702f (diff) | |
| parent | f125b71c0081acebbfcdd60bfe517d7e4dd388d4 (diff) | |
| download | rust-2952685917cb17a3d849cb02d17ce71ccecfb855.tar.gz rust-2952685917cb17a3d849cb02d17ce71ccecfb855.zip | |
auto merge of #11622 : bjz/rust/simplify-primitive-trait, r=brson
As part of #10387, this removes the `Primitive::{bits, bytes, is_signed}` methods and removes the trait's operator trait constraints for the reasons outlined below:
- The `Primitive::{bits, bytes}` associated functions were originally added to reflect the existing `BITS` and `BYTES`statics included in the numeric modules. These statics are only exist as a workaround for Rust's lack of CTFE, and should be deprecated in the future in favor of using the `std::mem::size_of` function (see #11621).
- `Primitive::is_signed` seems to be of little utility and does not seem to be used anywhere in the Rust compiler or libraries. It is also rather ugly to call due to the `Option<Self>` workaround for #8888.
- The operator trait constraints are already covered by the `Num` trait.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/num/f32.rs | 25 | ||||
| -rw-r--r-- | src/libstd/num/f64.rs | 25 | ||||
| -rw-r--r-- | src/libstd/num/int_macros.rs | 26 | ||||
| -rw-r--r-- | src/libstd/num/mod.rs | 21 | ||||
| -rw-r--r-- | src/libstd/num/uint_macros.rs | 18 |
5 files changed, 31 insertions, 84 deletions
diff --git a/src/libstd/num/f32.rs b/src/libstd/num/f32.rs index e77348268c7..5b0c75ef174 100644 --- a/src/libstd/num/f32.rs +++ b/src/libstd/num/f32.rs @@ -96,6 +96,9 @@ delegate!( fn tanh(n: c_float) -> c_float = cmath::c_float::tanh ) +// FIXME(#11621): These constants should be deprecated once CTFE is implemented +// in favour of calling their respective functions in `Bounded` and `Float`. + pub static RADIX: uint = 2u; pub static MANTISSA_DIGITS: uint = 53u; @@ -122,6 +125,10 @@ pub static NEG_INFINITY: f32 = -1.0_f32/0.0_f32; pub mod consts { // FIXME (requires Issue #1433 to fix): replace with mathematical // staticants from cmath. + + // FIXME(#11621): These constants should be deprecated once CTFE is + // implemented in favour of calling their respective functions in `Real`. + /// Archimedes' constant pub static PI: f32 = 3.14159265358979323846264338327950288_f32; @@ -554,16 +561,7 @@ impl Bounded for f32 { fn max_value() -> f32 { 3.40282347e+38 } } -impl Primitive for f32 { - #[inline] - fn bits(_: Option<f32>) -> uint { 32 } - - #[inline] - fn bytes(_: Option<f32>) -> uint { Primitive::bits(Some(0f32)) / 8 } - - #[inline] - fn is_signed(_: Option<f32>) -> bool { true } -} +impl Primitive for f32 {} impl Float for f32 { #[inline] @@ -1174,13 +1172,6 @@ mod tests { } #[test] - fn test_primitive() { - let none: Option<f32> = None; - assert_eq!(Primitive::bits(none), mem::size_of::<f32>() * 8); - assert_eq!(Primitive::bytes(none), mem::size_of::<f32>()); - } - - #[test] fn test_is_normal() { let nan: f32 = Float::nan(); let inf: f32 = Float::infinity(); diff --git a/src/libstd/num/f64.rs b/src/libstd/num/f64.rs index 7a06ef2e1af..95e5797ae93 100644 --- a/src/libstd/num/f64.rs +++ b/src/libstd/num/f64.rs @@ -101,6 +101,9 @@ delegate!( // FIXME (#1433): obtain these in a different way +// FIXME(#11621): These constants should be deprecated once CTFE is implemented +// in favour of calling their respective functions in `Bounded` and `Float`. + pub static RADIX: uint = 2u; pub static MANTISSA_DIGITS: uint = 53u; @@ -129,6 +132,10 @@ pub static NEG_INFINITY: f64 = -1.0_f64/0.0_f64; pub mod consts { // FIXME (requires Issue #1433 to fix): replace with mathematical // constants from cmath. + + // FIXME(#11621): These constants should be deprecated once CTFE is + // implemented in favour of calling their respective functions in `Real`. + /// Archimedes' constant pub static PI: f64 = 3.14159265358979323846264338327950288_f64; @@ -556,16 +563,7 @@ impl Bounded for f64 { fn max_value() -> f64 { 1.7976931348623157e+308 } } -impl Primitive for f64 { - #[inline] - fn bits(_: Option<f64>) -> uint { 64 } - - #[inline] - fn bytes(_: Option<f64>) -> uint { Primitive::bits(Some(0f64)) / 8 } - - #[inline] - fn is_signed(_: Option<f64>) -> bool { true } -} +impl Primitive for f64 {} impl Float for f64 { #[inline] @@ -1179,13 +1177,6 @@ mod tests { } #[test] - fn test_primitive() { - let none: Option<f64> = None; - assert_eq!(Primitive::bits(none), mem::size_of::<f64>() * 8); - assert_eq!(Primitive::bytes(none), mem::size_of::<f64>()); - } - - #[test] fn test_is_normal() { let nan: f64 = Float::nan(); let inf: f64 = Float::infinity(); diff --git a/src/libstd/num/int_macros.rs b/src/libstd/num/int_macros.rs index add0991f7af..7102a899758 100644 --- a/src/libstd/num/int_macros.rs +++ b/src/libstd/num/int_macros.rs @@ -13,11 +13,19 @@ 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. pub static bits : uint = $bits; +// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of +// calling the `mem::size_of` function. pub static bytes : uint = ($bits / 8); +// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of +// calling the `Bounded::min_value` function. pub static min_value: $T = (-1 as $T) << (bits - 1); // FIXME(#9837): Compute min_value like this so the high bits that shouldn't exist are 0. +// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of +// calling the `Bounded::max_value` function. pub static max_value: $T = !min_value; impl CheckedDiv for $T { @@ -361,16 +369,7 @@ impl Bounded for $T { impl Int for $T {} -impl Primitive for $T { - #[inline] - fn bits(_: Option<$T>) -> uint { bits } - - #[inline] - fn bytes(_: Option<$T>) -> uint { bits / 8 } - - #[inline] - fn is_signed(_: Option<$T>) -> bool { true } -} +impl Primitive for $T {} // String conversion functions and impl str -> num @@ -640,13 +639,6 @@ mod tests { } #[test] - fn test_primitive() { - let none: Option<$T> = None; - assert_eq!(Primitive::bits(none), mem::size_of::<$T>() * 8); - assert_eq!(Primitive::bytes(none), mem::size_of::<$T>()); - } - - #[test] fn test_from_str() { assert_eq!(from_str::<$T>("0"), Some(0 as $T)); assert_eq!(from_str::<$T>("3"), Some(3 as $T)); diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs index bcc95d6c48d..bdbf0344b47 100644 --- a/src/libstd/num/mod.rs +++ b/src/libstd/num/mod.rs @@ -17,6 +17,7 @@ use clone::{Clone, DeepClone}; use cmp::{Eq, Ord}; +use mem::size_of; use ops::{Add, Sub, Mul, Div, Rem, Neg}; use ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr}; use option::{Option, Some, None}; @@ -425,19 +426,7 @@ pub trait Primitive: Clone + Num + NumCast + Orderable - + Bounded - + Neg<Self> - + Add<Self,Self> - + Sub<Self,Self> - + Mul<Self,Self> - + Div<Self,Self> - + Rem<Self,Self> { - // FIXME (#5527): These should be associated constants - // FIXME (#8888): Removing `unused_self` requires #8888 to be fixed. - fn bits(unused_self: Option<Self>) -> uint; - fn bytes(unused_self: Option<Self>) -> uint; - fn is_signed(unused_self: Option<Self>) -> bool; -} + + Bounded {} /// A collection of traits relevant to primitive signed and unsigned integers pub trait Int: Integer @@ -580,7 +569,7 @@ pub trait ToPrimitive { macro_rules! impl_to_primitive_int_to_int( ($SrcT:ty, $DstT:ty) => ( { - if Primitive::bits(None::<$SrcT>) <= Primitive::bits(None::<$DstT>) { + if size_of::<$SrcT>() <= size_of::<$DstT>() { Some(*self as $DstT) } else { let n = *self as i64; @@ -665,7 +654,7 @@ macro_rules! impl_to_primitive_uint_to_int( macro_rules! impl_to_primitive_uint_to_uint( ($SrcT:ty, $DstT:ty) => ( { - if Primitive::bits(None::<$SrcT>) <= Primitive::bits(None::<$DstT>) { + if size_of::<$SrcT>() <= size_of::<$DstT>() { Some(*self as $DstT) } else { let zero: $SrcT = Zero::zero(); @@ -721,7 +710,7 @@ impl_to_primitive_uint!(u64) macro_rules! impl_to_primitive_float_to_float( ($SrcT:ty, $DstT:ty) => ( - if Primitive::bits(None::<$SrcT>) <= Primitive::bits(None::<$DstT>) { + if size_of::<$SrcT>() <= size_of::<$DstT>() { Some(*self as $DstT) } else { let n = *self as f64; diff --git a/src/libstd/num/uint_macros.rs b/src/libstd/num/uint_macros.rs index 97e547a2d42..1b822a491c6 100644 --- a/src/libstd/num/uint_macros.rs +++ b/src/libstd/num/uint_macros.rs @@ -285,16 +285,7 @@ impl ToStrRadix for $T { } } -impl Primitive for $T { - #[inline] - fn bits(_: Option<$T>) -> uint { bits } - - #[inline] - fn bytes(_: Option<$T>) -> uint { bits / 8 } - - #[inline] - fn is_signed(_: Option<$T>) -> bool { false } -} +impl Primitive for $T {} impl Bitwise for $T { /// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic. @@ -416,13 +407,6 @@ mod tests { } #[test] - fn test_primitive() { - let none: Option<$T> = None; - assert_eq!(Primitive::bits(none), mem::size_of::<$T>() * 8); - assert_eq!(Primitive::bytes(none), mem::size_of::<$T>()); - } - - #[test] pub fn test_to_str() { assert_eq!((0 as $T).to_str_radix(10u), ~"0"); assert_eq!((1 as $T).to_str_radix(10u), ~"1"); |
