diff options
| author | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2014-11-13 00:02:42 +1100 |
|---|---|---|
| committer | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2014-11-13 03:46:03 +1100 |
| commit | de938b6ca1a2b6b6df65d5935c765a7c25fbce84 (patch) | |
| tree | f8b7c283989f7ac9a7694989cfd3a4be71e74dee | |
| parent | e965ba85ca689ad77f63b7f0af9d7e337dcb4825 (diff) | |
| download | rust-de938b6ca1a2b6b6df65d5935c765a7c25fbce84.tar.gz rust-de938b6ca1a2b6b6df65d5935c765a7c25fbce84.zip | |
Remove Signed trait and add SignedInt trait
The methods have been moved into Float and SignedInt
26 files changed, 179 insertions, 130 deletions
diff --git a/src/etc/vim/syntax/rust.vim b/src/etc/vim/syntax/rust.vim index a96e622e760..710092a3e5f 100644 --- a/src/etc/vim/syntax/rust.vim +++ b/src/etc/vim/syntax/rust.vim @@ -97,7 +97,7 @@ syn keyword rustTrait FromIterator IntoIterator Extend ExactSize syn keyword rustTrait Iterator DoubleEndedIterator syn keyword rustTrait RandomAccessIterator CloneableIterator syn keyword rustTrait OrdIterator MutableDoubleEndedIterator -syn keyword rustTrait NumCast Signed Int UnsignedInt Float +syn keyword rustTrait NumCast Int SignedInt UnsignedInt Float syn keyword rustTrait FloatMath ToPrimitive FromPrimitive syn keyword rustTrait Box syn keyword rustTrait GenericPath Path PosixPath WindowsPath diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 6e87fe4ced0..ec339d10e4b 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -19,6 +19,8 @@ //! operators, you could do the following: //! //! ```rust +//! use core::num::SignedInt; +//! //! // Our type. //! struct SketchyNum { //! num : int diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 081f373b831..380ca82783a 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -620,7 +620,7 @@ impl<'a, T> Pointer for &'a mut T { macro_rules! floating(($ty:ident) => { impl Float for $ty { fn fmt(&self, fmt: &mut Formatter) -> Result { - use num::{Float, Signed}; + use num::Float; let digits = match fmt.precision { Some(i) => float::DigExact(i), @@ -641,7 +641,7 @@ macro_rules! floating(($ty:ident) => { impl LowerExp for $ty { fn fmt(&self, fmt: &mut Formatter) -> Result { - use num::{Float, Signed}; + use num::Float; let digits = match fmt.precision { Some(i) => float::DigExact(i), @@ -662,7 +662,7 @@ macro_rules! floating(($ty:ident) => { impl UpperExp for $ty { fn fmt(&self, fmt: &mut Formatter) -> Result { - use num::{Float, Signed}; + use num::Float; let digits = match fmt.precision { Some(i) => float::DigExact(i), diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index a24b6d85007..ae0acc32926 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -573,6 +573,8 @@ pub trait Iterator<A> { /// # Example /// /// ```rust + /// use core::num::SignedInt; + /// /// let xs = [-3i, 0, 1, 5, -10]; /// assert_eq!(*xs.iter().max_by(|x| x.abs()).unwrap(), -10); /// ``` @@ -597,6 +599,8 @@ pub trait Iterator<A> { /// # Example /// /// ```rust + /// use core::num::SignedInt; + /// /// let xs = [-3i, 0, 1, 5, -10]; /// assert_eq!(*xs.iter().min_by(|x| x.abs()).unwrap(), 0); /// ``` diff --git a/src/libcore/num/f32.rs b/src/libcore/num/f32.rs index 5913b8d75a7..ba03bb8f3d5 100644 --- a/src/libcore/num/f32.rs +++ b/src/libcore/num/f32.rs @@ -242,6 +242,41 @@ impl Float for f32 { #[inline] fn fract(self) -> f32 { self - self.trunc() } + /// Computes the absolute value of `self`. Returns `Float::nan()` if the + /// number is `Float::nan()`. + #[inline] + fn abs(self) -> f32 { + unsafe { intrinsics::fabsf32(self) } + } + + /// Returns a number that represents the sign of `self`. + /// + /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()` + /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()` + /// - `Float::nan()` if the number is `Float::nan()` + #[inline] + fn signum(self) -> f32 { + if self.is_nan() { + Float::nan() + } else { + unsafe { intrinsics::copysignf32(1.0, self) } + } + } + + /// Returns `true` if `self` is positive, including `+0.0` and + /// `Float::infinity()`. + #[inline] + fn is_positive(self) -> bool { + self > 0.0 || (1.0 / self) == Float::infinity() + } + + /// Returns `true` if `self` is negative, including `-0.0` and + /// `Float::neg_infinity()`. + #[inline] + fn is_negative(self) -> bool { + self < 0.0 || (1.0 / self) == Float::neg_infinity() + } + /// Fused multiply-add. Computes `(self * a) + b` with only one rounding /// error. This produces a more accurate result with better performance than /// a separate multiplication operation followed by an add. @@ -254,6 +289,7 @@ impl Float for f32 { #[inline] fn recip(self) -> f32 { 1.0 / self } + #[inline] fn powi(self, n: i32) -> f32 { unsafe { intrinsics::powif32(self, n) } } diff --git a/src/libcore/num/f64.rs b/src/libcore/num/f64.rs index 8f68578d768..f1af4f0272c 100644 --- a/src/libcore/num/f64.rs +++ b/src/libcore/num/f64.rs @@ -248,6 +248,41 @@ impl Float for f64 { #[inline] fn fract(self) -> f64 { self - self.trunc() } + /// Computes the absolute value of `self`. Returns `Float::nan()` if the + /// number is `Float::nan()`. + #[inline] + fn abs(self) -> f64 { + unsafe { intrinsics::fabsf64(self) } + } + + /// Returns a number that represents the sign of `self`. + /// + /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()` + /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()` + /// - `Float::nan()` if the number is `Float::nan()` + #[inline] + fn signum(self) -> f64 { + if self.is_nan() { + Float::nan() + } else { + unsafe { intrinsics::copysignf64(1.0, self) } + } + } + + /// Returns `true` if `self` is positive, including `+0.0` and + /// `Float::infinity()`. + #[inline] + fn is_positive(self) -> bool { + self > 0.0 || (1.0 / self) == Float::infinity() + } + + /// Returns `true` if `self` is negative, including `-0.0` and + /// `Float::neg_infinity()`. + #[inline] + fn is_negative(self) -> bool { + self < 0.0 || (1.0 / self) == Float::neg_infinity() + } + /// Fused multiply-add. Computes `(self * a) + b` with only one rounding /// error. This produces a more accurate result with better performance than /// a separate multiplication operation followed by an add. diff --git a/src/libcore/num/float_macros.rs b/src/libcore/num/float_macros.rs index 3e403219a4f..d15cff3a8a9 100644 --- a/src/libcore/num/float_macros.rs +++ b/src/libcore/num/float_macros.rs @@ -13,6 +13,7 @@ macro_rules! assert_approx_eq( ($a:expr, $b:expr) => ({ + use num::Float; let (a, b) = (&$a, &$b); assert!((*a - *b).abs() < 1.0e-6, "{} is not approximately equal to {}", *a, *b); diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index dd2ee6e01c6..216d140ac48 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -33,113 +33,6 @@ pub fn div_rem<T: Div<T, T> + Rem<T, T>>(x: T, y: T) -> (T, T) { (x / y, x % y) } -/// A built-in signed number. -pub trait Signed: Neg<Self> { - /// Computes the absolute value of `self`. - fn abs(self) -> Self; - - /// Returns a number (either `-1`, `0` or `1`) representing sign of `self`. - fn signum(self) -> Self; - - /// Returns `true` if `self` is a positive value. - fn is_positive(self) -> bool; - - /// Returns `true` if `self` is a negative value. - fn is_negative(self) -> bool; -} - -macro_rules! signed_int_impl { - ($T:ty) => { - impl Signed for $T { - /// Computes the absolute value. `Int::min_value()` will be returned - /// if the number is `Int::min_value()`. - #[inline] - fn abs(self) -> $T { - if self.is_negative() { -self } else { self } - } - - /// # Returns - /// - /// - `0` if the number is zero - /// - `1` if the number is positive - /// - `-1` if the number is negative - #[inline] - 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. - #[inline] - fn is_positive(self) -> bool { self > 0 } - - /// Returns `true` if `self` is negative and `false` if the number - /// is zero or positive. - #[inline] - fn is_negative(self) -> bool { self < 0 } - } - } -} - -signed_int_impl!(i8) -signed_int_impl!(i16) -signed_int_impl!(i32) -signed_int_impl!(i64) -signed_int_impl!(int) - -macro_rules! signed_float_impl { - ($T:ty, $fabs:path, $fcopysign:path) => { - impl Signed for $T { - /// Computes the absolute value. Returns `Float::nan()` if the - /// number is `Float::nan()`. - #[inline] - fn abs(self) -> $T { - unsafe { $fabs(self) } - } - - /// # Returns - /// - /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()` - /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()` - /// - `Float::nan()` if the number is `Float::nan()` - #[inline] - fn signum(self) -> $T { - if self.is_nan() { - Float::nan() - } else { - unsafe { $fcopysign(1.0, self) } - } - } - - /// Returns `true` if the number is positive, including `+0.0` and - /// `Float::infinity()`. - #[inline] - fn is_positive(self) -> bool { - self > 0.0 || (1.0 / self) == Float::infinity() - } - - /// Returns `true` if the number is negative, including `-0.0` and - /// `Float::neg_infinity()`. - #[inline] - fn is_negative(self) -> bool { - self < 0.0 || (1.0 / self) == Float::neg_infinity() - } - } - }; -} - -signed_float_impl!(f32, - intrinsics::fabsf32, - intrinsics::copysignf32) - -signed_float_impl!(f64, - intrinsics::fabsf64, - intrinsics::copysignf64) - /// Raises a `base` to the power of `exp`, using exponentiation by squaring. /// /// # Example @@ -702,6 +595,63 @@ int_impl!(int = i64, u64, 64, intrinsics::i64_sub_with_overflow, intrinsics::i64_mul_with_overflow) +/// A built-in two's complement integer. +pub trait SignedInt + : Int + + Neg<Self> +{ + /// Computes the absolute value of `self`. `Int::min_value()` will be + /// returned if the number is `Int::min_value()`. + fn abs(self) -> 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 + fn signum(self) -> Self; + + /// Returns `true` if `self` is positive and `false` if the number + /// is zero or negative. + fn is_positive(self) -> bool; + + /// Returns `true` if `self` is negative and `false` if the number + /// is zero or positive. + fn is_negative(self) -> bool; +} + +macro_rules! signed_int_impl { + ($T:ty) => { + impl SignedInt for $T { + #[inline] + fn abs(self) -> $T { + if self.is_negative() { -self } else { self } + } + + #[inline] + fn signum(self) -> $T { + match self { + n if n > 0 => 1, + 0 => 0, + _ => -1, + } + } + + #[inline] + fn is_positive(self) -> bool { self > 0 } + + #[inline] + fn is_negative(self) -> bool { self < 0 } + } + } +} + +signed_int_impl!(i8) +signed_int_impl!(i16) +signed_int_impl!(i32) +signed_int_impl!(i64) +signed_int_impl!(int) + /// A built-in unsigned integer. pub trait UnsignedInt: Int { /// Returns `true` iff `self == 2^k` for some `k`. @@ -1257,7 +1207,7 @@ pub trait Float + NumCast + PartialOrd + PartialEq - + Signed + + Neg<Self> + Add<Self,Self> + Sub<Self,Self> + Mul<Self,Self> @@ -1327,6 +1277,22 @@ pub trait Float /// Return the fractional part of a number. fn fract(self) -> Self; + /// Computes the absolute value of `self`. Returns `Float::nan()` if the + /// number is `Float::nan()`. + fn abs(self) -> Self; + /// Returns a number that represents the sign of `self`. + /// + /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()` + /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()` + /// - `Float::nan()` if the number is `Float::nan()` + fn signum(self) -> Self; + /// Returns `true` if `self` is positive, including `+0.0` and + /// `Float::infinity()`. + fn is_positive(self) -> bool; + /// Returns `true` if `self` is negative, including `-0.0` and + /// `Float::neg_infinity()`. + fn is_negative(self) -> bool; + /// Fused multiply-add. Computes `(self * a) + b` with only one rounding /// error. This produces a more accurate result with better performance than /// a separate multiplication operation followed by an add. @@ -1494,14 +1460,6 @@ one_impl!(i64, 1i64) one_impl!(f32, 1.0f32) one_impl!(f64, 1.0f64) -#[deprecated = "Use `Signed::abs`"] -pub fn abs<T: Signed>(value: T) -> T { - value.abs() -} -#[deprecated = "Use `Signed::signum`"] -pub fn signum<T: Signed>(value: T) -> T { - value.signum() -} #[deprecated = "Use `UnsignedInt::next_power_of_two`"] pub fn next_power_of_two<T: UnsignedInt>(n: T) -> T { n.next_power_of_two() diff --git a/src/libcore/prelude.rs b/src/libcore/prelude.rs index 7e35f20f078..60012ab149f 100644 --- a/src/libcore/prelude.rs +++ b/src/libcore/prelude.rs @@ -51,7 +51,7 @@ pub use cmp::{Ordering, Less, Equal, Greater, Equiv}; pub use iter::{FromIterator, Extend}; pub use iter::{Iterator, DoubleEndedIterator, RandomAccessIterator, CloneableIterator}; pub use iter::{OrdIterator, MutableDoubleEndedIterator, ExactSize}; -pub use num::{Signed, ToPrimitive, FromPrimitive}; +pub use num::{ToPrimitive, FromPrimitive}; pub use option::{Option, Some, None}; pub use ptr::RawPtr; pub use result::{Result, Ok, Err}; diff --git a/src/libcoretest/cmp.rs b/src/libcoretest/cmp.rs index 59ce73fe40d..716300f652d 100644 --- a/src/libcoretest/cmp.rs +++ b/src/libcoretest/cmp.rs @@ -109,6 +109,8 @@ fn test_partial_max() { #[test] fn test_user_defined_eq() { + use core::num::SignedInt; + // Our type. struct SketchyNum { num : int diff --git a/src/libcoretest/iter.rs b/src/libcoretest/iter.rs index 3bb8d735d9c..2f11307c461 100644 --- a/src/libcoretest/iter.rs +++ b/src/libcoretest/iter.rs @@ -10,6 +10,7 @@ use core::iter::*; use core::iter::order::*; +use core::num::SignedInt; use core::uint; use core::cmp; use core::ops::Slice; diff --git a/src/libcoretest/num/int_macros.rs b/src/libcoretest/num/int_macros.rs index a1b096bf454..5e2530ef2a9 100644 --- a/src/libcoretest/num/int_macros.rs +++ b/src/libcoretest/num/int_macros.rs @@ -15,7 +15,7 @@ macro_rules! int_module (($T:ty, $T_i:ident) => ( mod tests { use core::$T_i::*; use core::int; - use core::num::Int; + use core::num::{Int, SignedInt}; use num; #[test] diff --git a/src/librand/distributions/mod.rs b/src/librand/distributions/mod.rs index 73817a52fa7..7d54422601b 100644 --- a/src/librand/distributions/mod.rs +++ b/src/librand/distributions/mod.rs @@ -23,7 +23,7 @@ that do not need to record state. #![experimental] use core::prelude::*; -use core::num::Int; +use core::num::{Float, Int}; use {Rng, Rand}; diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index 3026e047626..055a4c5dff4 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -37,6 +37,7 @@ use lint::{Context, LintPass, LintArray}; use std::cmp; use std::collections::hash_map::{Occupied, Vacant}; +use std::num::SignedInt; use std::slice; use std::{i8, i16, i32, i64, u8, u16, u32, u64, f32, f64}; use syntax::abi; diff --git a/src/libstd/num/float_macros.rs b/src/libstd/num/float_macros.rs index 519de85edde..4b3727ead61 100644 --- a/src/libstd/num/float_macros.rs +++ b/src/libstd/num/float_macros.rs @@ -14,6 +14,7 @@ macro_rules! assert_approx_eq( ($a:expr, $b:expr) => ({ + use num::Float; let (a, b) = (&$a, &$b); assert!((*a - *b).abs() < 1.0e-6, "{} is not approximately equal to {}", *a, *b); diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs index 62e70e0c9de..0afc8ce0452 100644 --- a/src/libstd/num/mod.rs +++ b/src/libstd/num/mod.rs @@ -23,9 +23,8 @@ use option::Option; #[cfg(test)] use ops::{Add, Sub, Mul, Div, Rem}; pub use core::num::{Num, div_rem, Zero, zero, One, one}; -pub use core::num::{Signed, abs, signum}; pub use core::num::{Unsigned, pow, Bounded}; -pub use core::num::{Primitive, Int, UnsignedInt}; +pub use core::num::{Primitive, Int, SignedInt, UnsignedInt}; pub use core::num::{cast, FromPrimitive, NumCast, ToPrimitive}; pub use core::num::{next_power_of_two, is_power_of_two}; pub use core::num::{checked_next_power_of_two}; diff --git a/src/libstd/prelude.rs b/src/libstd/prelude.rs index 0baae850eaa..158e7a59f6d 100644 --- a/src/libstd/prelude.rs +++ b/src/libstd/prelude.rs @@ -67,7 +67,7 @@ #[doc(no_inline)] pub use iter::{Iterator, DoubleEndedIterator}; #[doc(no_inline)] pub use iter::{RandomAccessIterator, CloneableIterator}; #[doc(no_inline)] pub use iter::{OrdIterator, MutableDoubleEndedIterator}; -#[doc(no_inline)] pub use num::{Signed, ToPrimitive, FromPrimitive}; +#[doc(no_inline)] pub use num::{ToPrimitive, FromPrimitive}; #[doc(no_inline)] pub use boxed::Box; #[doc(no_inline)] pub use option::{Option, Some, None}; #[doc(no_inline)] pub use path::{GenericPath, Path, PosixPath, WindowsPath}; diff --git a/src/libstd/sys/unix/mod.rs b/src/libstd/sys/unix/mod.rs index 969570d385c..4db9e8a9df8 100644 --- a/src/libstd/sys/unix/mod.rs +++ b/src/libstd/sys/unix/mod.rs @@ -18,7 +18,7 @@ extern crate libc; use num; -use num::Int; +use num::{Int, SignedInt}; use prelude::*; use io::{mod, IoResult, IoError}; use sys_common::mkerr_libc; @@ -117,7 +117,7 @@ pub fn decode_error_detailed(errno: i32) -> IoError { } #[inline] -pub fn retry<T: Signed + Int> (f: || -> T) -> T { +pub fn retry<T: SignedInt> (f: || -> T) -> T { let one: T = Int::one(); loop { let n = f(); diff --git a/src/libtest/lib.rs b/src/libtest/lib.rs index a25aff3eaff..11788483397 100644 --- a/src/libtest/lib.rs +++ b/src/libtest/lib.rs @@ -60,7 +60,7 @@ use std::io::fs::PathExtensions; use std::io::stdio::StdWriter; use std::io::{File, ChanReader, ChanWriter}; use std::io; -use std::num::{Int, FloatMath}; +use std::num::{Float, FloatMath, Int}; use std::os; use std::string::String; use std::task::TaskBuilder; diff --git a/src/libtest/stats.rs b/src/libtest/stats.rs index 5cec5076e06..adf58dc875c 100644 --- a/src/libtest/stats.rs +++ b/src/libtest/stats.rs @@ -461,6 +461,7 @@ mod tests { macro_rules! assert_approx_eq( ($a:expr, $b:expr) => ({ + use std::num::Float; let (a, b) = (&$a, &$b); assert!((*a - *b).abs() < 1.0e-6, "{} is not approximately equal to {}", *a, *b); diff --git a/src/libtime/lib.rs b/src/libtime/lib.rs index acd81d4566b..03b5186eef7 100644 --- a/src/libtime/lib.rs +++ b/src/libtime/lib.rs @@ -30,6 +30,7 @@ extern crate libc; use std::fmt::Show; use std::fmt; use std::io::BufReader; +use std::num::SignedInt; use std::string::String; use std::time::Duration; diff --git a/src/test/compile-fail/implicit-method-bind.rs b/src/test/compile-fail/implicit-method-bind.rs index 06a4088617a..34367f06793 100644 --- a/src/test/compile-fail/implicit-method-bind.rs +++ b/src/test/compile-fail/implicit-method-bind.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use std::num::SignedInt; + fn main() { let _f = 10i.abs; //~ ERROR attempted to take value of method } diff --git a/src/test/run-pass/const-binops.rs b/src/test/run-pass/const-binops.rs index c14f430e709..f4dba3f6c7f 100644 --- a/src/test/run-pass/const-binops.rs +++ b/src/test/run-pass/const-binops.rs @@ -12,6 +12,7 @@ macro_rules! assert_approx_eq( ($a:expr, $b:expr) => ({ + use std::num::Float; let (a, b) = (&$a, &$b); assert!((*a - *b).abs() < 1.0e-6, "{} is not approximately equal to {}", *a, *b); diff --git a/src/test/run-pass/intrinsics-math.rs b/src/test/run-pass/intrinsics-math.rs index 164a6845a9f..c3ba7ca12d0 100644 --- a/src/test/run-pass/intrinsics-math.rs +++ b/src/test/run-pass/intrinsics-math.rs @@ -13,6 +13,7 @@ macro_rules! assert_approx_eq( ($a:expr, $b:expr) => ({ + use std::num::Float; let (a, b) = (&$a, &$b); assert!((*a - *b).abs() < 1.0e-6, "{} is not approximately equal to {}", *a, *b); diff --git a/src/test/run-pass/trait-inheritance-self-in-supertype.rs b/src/test/run-pass/trait-inheritance-self-in-supertype.rs index c7e206cb474..96f1c940dcf 100644 --- a/src/test/run-pass/trait-inheritance-self-in-supertype.rs +++ b/src/test/run-pass/trait-inheritance-self-in-supertype.rs @@ -10,6 +10,8 @@ // Test for issue #4183: use of Self in supertraits. +use std::num::Float as StdFloat; + pub static FUZZY_EPSILON: f64 = 0.1; pub trait FuzzyEq<Eps> { diff --git a/src/test/run-pass/utf8_idents.rs b/src/test/run-pass/utf8_idents.rs index 68964fa4957..c99c394969c 100644 --- a/src/test/run-pass/utf8_idents.rs +++ b/src/test/run-pass/utf8_idents.rs @@ -10,9 +10,10 @@ // // ignore-lexer-test FIXME #15679 - #![feature(non_ascii_idents)] +use std::num::Float; + pub fn main() { let ε = 0.00001f64; let Π = 3.14f64; |
