diff --git a/src/libstd/num/f32.rs b/src/libstd/num/f32.rs index d6de4f25f6eef..03a7888e81e1b 100644 --- a/src/libstd/num/f32.rs +++ b/src/libstd/num/f32.rs @@ -16,8 +16,9 @@ use libc::c_int; use num::{Zero, One, strconv}; use num::{FPCategory, FPNaN, FPInfinite , FPZero, FPSubnormal, FPNormal}; use num; -use prelude::*; use to_str; +use util::ForType; +use prelude::*; pub use cmath::c_float_targ_consts::*; @@ -584,13 +585,13 @@ impl Bounded for f32 { impl Primitive for f32 { #[inline] - fn bits(_: Option) -> uint { 32 } + fn bits(_: ForType) -> uint { 32 } #[inline] - fn bytes(_: Option) -> uint { Primitive::bits(Some(0f32)) / 8 } + fn bytes(_: ForType) -> uint { Primitive::bits(ForType::) / 8 } #[inline] - fn is_signed(_: Option) -> bool { true } + fn is_signed(_: ForType) -> bool { true } } impl Float for f32 { @@ -647,25 +648,25 @@ impl Float for f32 { } #[inline] - fn mantissa_digits(_: Option) -> uint { 24 } + fn mantissa_digits(_: ForType) -> uint { 24 } #[inline] - fn digits(_: Option) -> uint { 6 } + fn digits(_: ForType) -> uint { 6 } #[inline] fn epsilon() -> f32 { 1.19209290e-07 } #[inline] - fn min_exp(_: Option) -> int { -125 } + fn min_exp(_: ForType) -> int { -125 } #[inline] - fn max_exp(_: Option) -> int { 128 } + fn max_exp(_: ForType) -> int { 128 } #[inline] - fn min_10_exp(_: Option) -> int { -37 } + fn min_10_exp(_: ForType) -> int { -37 } #[inline] - fn max_10_exp(_: Option) -> int { 38 } + fn max_10_exp(_: ForType) -> int { 38 } /// Constructs a floating point number by multiplying `x` by 2 raised to the power of `exp` #[inline] @@ -927,6 +928,7 @@ mod tests { use num::*; use num; use mem; + use util::ForType; #[test] fn test_num() { @@ -1195,9 +1197,8 @@ mod tests { #[test] fn test_primitive() { - let none: Option = None; - assert_eq!(Primitive::bits(none), mem::size_of::() * 8); - assert_eq!(Primitive::bytes(none), mem::size_of::()); + assert_eq!(Primitive::bits(ForType::), mem::size_of::() * 8); + assert_eq!(Primitive::bytes(ForType::), mem::size_of::()); } #[test] diff --git a/src/libstd/num/f64.rs b/src/libstd/num/f64.rs index 5feab20ba81d3..e8ce02c8a722c 100644 --- a/src/libstd/num/f64.rs +++ b/src/libstd/num/f64.rs @@ -17,8 +17,9 @@ use libc::c_int; use num::{Zero, One, strconv}; use num::{FPCategory, FPNaN, FPInfinite , FPZero, FPSubnormal, FPNormal}; use num; -use prelude::*; use to_str; +use util::ForType; +use prelude::*; pub use cmath::c_double_targ_consts::*; pub use cmp::{min, max}; @@ -632,13 +633,13 @@ impl Bounded for f64 { impl Primitive for f64 { #[inline] - fn bits(_: Option) -> uint { 64 } + fn bits(_: ForType) -> uint { 64 } #[inline] - fn bytes(_: Option) -> uint { Primitive::bits(Some(0f64)) / 8 } + fn bytes(_: ForType) -> uint { Primitive::bits(ForType::) / 8 } #[inline] - fn is_signed(_: Option) -> bool { true } + fn is_signed(_: ForType) -> bool { true } } impl Float for f64 { @@ -695,25 +696,25 @@ impl Float for f64 { } #[inline] - fn mantissa_digits(_: Option) -> uint { 53 } + fn mantissa_digits(_: ForType) -> uint { 53 } #[inline] - fn digits(_: Option) -> uint { 15 } + fn digits(_: ForType) -> uint { 15 } #[inline] fn epsilon() -> f64 { 2.2204460492503131e-16 } #[inline] - fn min_exp(_: Option) -> int { -1021 } + fn min_exp(_: ForType) -> int { -1021 } #[inline] - fn max_exp(_: Option) -> int { 1024 } + fn max_exp(_: ForType) -> int { 1024 } #[inline] - fn min_10_exp(_: Option) -> int { -307 } + fn min_10_exp(_: ForType) -> int { -307 } #[inline] - fn max_10_exp(_: Option) -> int { 308 } + fn max_10_exp(_: ForType) -> int { 308 } /// Constructs a floating point number by multiplying `x` by 2 raised to the power of `exp` #[inline] @@ -975,6 +976,7 @@ mod tests { use num::*; use num; use mem; + use util::ForType; #[test] fn test_num() { @@ -1246,9 +1248,8 @@ mod tests { #[test] fn test_primitive() { - let none: Option = None; - assert_eq!(Primitive::bits(none), mem::size_of::() * 8); - assert_eq!(Primitive::bytes(none), mem::size_of::()); + assert_eq!(Primitive::bits(ForType::), mem::size_of::() * 8); + assert_eq!(Primitive::bytes(ForType::), mem::size_of::()); } #[test] diff --git a/src/libstd/num/int_macros.rs b/src/libstd/num/int_macros.rs index ba1a7a4f912b8..35448232671ea 100644 --- a/src/libstd/num/int_macros.rs +++ b/src/libstd/num/int_macros.rs @@ -20,8 +20,9 @@ macro_rules! int_module (($T:ty, $bits:expr) => (mod generated { use default::Default; use num::{ToStrRadix, FromStrRadix}; use num::{CheckedDiv, Zero, One, strconv}; -use prelude::*; use str; +use util::ForType; +use prelude::*; pub use cmp::{min, max}; @@ -377,13 +378,13 @@ impl Int for $T {} impl Primitive for $T { #[inline] - fn bits(_: Option<$T>) -> uint { bits } + fn bits(_: ForType<$T>) -> uint { bits } #[inline] - fn bytes(_: Option<$T>) -> uint { bits / 8 } + fn bytes(_: ForType<$T>) -> uint { bits / 8 } #[inline] - fn is_signed(_: Option<$T>) -> bool { true } + fn is_signed(_: ForType<$T>) -> bool { true } } // String conversion functions and impl str -> num @@ -458,6 +459,7 @@ mod tests { use i32; use num; use mem; + use util::ForType; #[test] fn test_num() { @@ -653,9 +655,8 @@ 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>()); + assert_eq!(Primitive::bits(ForType::<$T>), mem::size_of::<$T>() * 8); + assert_eq!(Primitive::bytes(ForType::<$T>), mem::size_of::<$T>()); } #[test] diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs index 3097a8e138ef0..21acc4f00f829 100644 --- a/src/libstd/num/mod.rs +++ b/src/libstd/num/mod.rs @@ -20,6 +20,7 @@ use cmp::{Eq, ApproxEq, Ord}; use ops::{Add, Sub, Mul, Div, Rem, Neg}; use ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr}; use option::{Option, Some, None}; +use util::ForType; pub mod strconv; @@ -343,10 +344,10 @@ pub trait Primitive: Clone + Div + Rem { // FIXME (#5527): These should be associated constants - // FIXME (#8888): Removing `unused_self` requires #8888 to be fixed. - fn bits(unused_self: Option) -> uint; - fn bytes(unused_self: Option) -> uint; - fn is_signed(unused_self: Option) -> bool; + // FIXME (#8888): `ForType` should be a language feature. See also: #6894 + fn bits(_: ForType) -> uint; + fn bytes(_: ForType) -> uint; + fn is_signed(_: ForType) -> bool; } /// A collection of traits relevant to primitive signed and unsigned integers @@ -387,14 +388,14 @@ pub trait Float: Real fn is_normal(&self) -> bool; fn classify(&self) -> FPCategory; - // FIXME (#8888): Removing `unused_self` requires #8888 to be fixed. - fn mantissa_digits(unused_self: Option) -> uint; - fn digits(unused_self: Option) -> uint; + // FIXME (#8888): `ForType` should be a language feature. See also: #6894 + fn mantissa_digits(_: ForType) -> uint; + fn digits(_: ForType) -> uint; fn epsilon() -> Self; - fn min_exp(unused_self: Option) -> int; - fn max_exp(unused_self: Option) -> int; - fn min_10_exp(unused_self: Option) -> int; - fn max_10_exp(unused_self: Option) -> int; + fn min_exp(_: ForType) -> int; + fn max_exp(_: ForType) -> int; + fn min_10_exp(_: ForType) -> int; + fn max_10_exp(_: ForType) -> int; fn ldexp(x: Self, exp: int) -> Self; fn frexp(&self) -> (Self, int); @@ -490,7 +491,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 Primitive::bits(ForType::<$SrcT>) <= Primitive::bits(ForType::<$DstT>) { Some(*self as $DstT) } else { let n = *self as i64; @@ -575,7 +576,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 Primitive::bits(ForType::<$SrcT>) <= Primitive::bits(ForType::<$DstT>) { Some(*self as $DstT) } else { let zero: $SrcT = Zero::zero(); @@ -631,7 +632,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 Primitive::bits(ForType::<$SrcT>) <= Primitive::bits(ForType::<$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 2974b402d4a76..18ce80c5c5f16 100644 --- a/src/libstd/num/uint_macros.rs +++ b/src/libstd/num/uint_macros.rs @@ -21,8 +21,9 @@ use default::Default; use num::BitCount; use num::{ToStrRadix, FromStrRadix}; use num::{CheckedDiv, Zero, One, strconv}; -use prelude::*; use str; +use util::ForType; +use prelude::*; pub use cmp::{min, max}; @@ -302,13 +303,13 @@ impl ToStrRadix for $T { impl Primitive for $T { #[inline] - fn bits(_: Option<$T>) -> uint { bits } + fn bits(_: ForType<$T>) -> uint { bits } #[inline] - fn bytes(_: Option<$T>) -> uint { bits / 8 } + fn bytes(_: ForType<$T>) -> uint { bits / 8 } #[inline] - fn is_signed(_: Option<$T>) -> bool { false } + fn is_signed(_: ForType<$T>) -> bool { false } } impl BitCount for $T { @@ -339,6 +340,7 @@ mod tests { use num; use mem; use u16; + use util::ForType; #[test] fn test_num() { @@ -430,9 +432,8 @@ 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>()); + assert_eq!(Primitive::bits(ForType::<$T>), mem::size_of::<$T>() * 8); + assert_eq!(Primitive::bytes(ForType::<$T>), mem::size_of::<$T>()); } #[test] diff --git a/src/libstd/util.rs b/src/libstd/util.rs index 3e44a30e47b33..41b4d33ed6bc1 100644 --- a/src/libstd/util.rs +++ b/src/libstd/util.rs @@ -23,10 +23,8 @@ pub fn id(x: T) -> T { x } #[inline] pub fn ignore(_x: T) { } -/** - * Swap the values at two mutable locations of the same type, without - * deinitialising or copying either one. - */ +/// Swap the values at two mutable locations of the same type, without +/// deinitialising or copying either one. #[inline] pub fn swap(x: &mut T, y: &mut T) { unsafe { @@ -47,10 +45,8 @@ pub fn swap(x: &mut T, y: &mut T) { } } -/** - * Replace the value at a mutable location with a new one, returning the old - * value, without deinitialising or copying either one. - */ +/// Replace the value at a mutable location with a new one, returning the old +/// value, without deinitialising or copying either one. #[inline] pub fn replace(dest: &mut T, mut src: T) -> T { swap(dest, &mut src); @@ -84,6 +80,24 @@ impl Void { } } +/// A zero-sized type hint useful for accessing static methods that don't +/// mention `Self`. +/// +/// # Example +/// +/// ~~~rust +/// trait A { +/// fn a(_: ForType) -> uint; +/// } +/// +/// impl A for int { +/// fn a(_: ForType) -> uint { 6 } +/// } +/// +/// assert_eq!(A::a(ForType::), 6); +/// ~~~ +// FIXME (#8888): This should really be part of the language. See also: #6894 +pub struct ForType; #[cfg(test)] mod tests { @@ -155,6 +169,19 @@ mod tests { unsafe { assert_eq!(did_run, true); } } + + #[test] + fn type_hint() { + trait A { + fn a(_: ForType) -> uint; + } + + impl A for int { + fn a(_: ForType) -> uint { 6 } + } + + assert_eq!(A::a(ForType::), 6); + } } /// Completely miscellaneous language-construct benchmarks.