diff --git a/src/etc/vim/syntax/rust.vim b/src/etc/vim/syntax/rust.vim index 7033f493f0f..a96e622e760 100644 --- a/src/etc/vim/syntax/rust.vim +++ b/src/etc/vim/syntax/rust.vim @@ -97,8 +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 Num NumCast -syn keyword rustTrait Signed Unsigned Primitive Int Float +syn keyword rustTrait NumCast Signed Int UnsignedInt Float syn keyword rustTrait FloatMath ToPrimitive FromPrimitive syn keyword rustTrait Box syn keyword rustTrait GenericPath Path PosixPath WindowsPath diff --git a/src/libcore/fmt/float.rs b/src/libcore/fmt/float.rs index 365d5a7212b..ec920ddc6db 100644 --- a/src/libcore/fmt/float.rs +++ b/src/libcore/fmt/float.rs @@ -13,7 +13,7 @@ use char; use fmt; use iter::{range, DoubleEndedIterator}; -use num::{Float, FPNaN, FPInfinite, ToPrimitive, Primitive}; +use num::{Float, FPNaN, FPInfinite, ToPrimitive}; use num::cast; use result::Ok; use slice::{mod, SlicePrelude}; @@ -79,7 +79,7 @@ static DIGIT_E_RADIX: uint = ('e' as uint) - ('a' as uint) + 11u; * - Fails if `radix` > 25 and `exp_format` is `ExpBin` due to conflict * between digit and exponent sign `'p'`. */ -pub fn float_to_str_bytes_common( +pub fn float_to_str_bytes_common( num: T, radix: uint, negative_zero: bool, diff --git a/src/libcore/fmt/num.rs b/src/libcore/fmt/num.rs index 787d2d2c678..0a5af56217c 100644 --- a/src/libcore/fmt/num.rs +++ b/src/libcore/fmt/num.rs @@ -35,10 +35,11 @@ trait GenericRadix { fn fmt_int(&self, mut x: T, f: &mut fmt::Formatter) -> fmt::Result { // The radix can be as low as 2, so we need a buffer of at least 64 // characters for a base 2 number. + let zero = Int::zero(); + let is_positive = x >= zero; let mut buf = [0u8, ..64]; - let base = cast(self.base()).unwrap(); let mut curr = buf.len(); - let is_positive = x >= Int::zero(); + let base = cast(self.base()).unwrap(); if is_positive { // Accumulate each digit of the number from the least significant // to the most significant figure. @@ -47,16 +48,16 @@ trait GenericRadix { x = x / base; // Deaccumulate the number. *byte = self.digit(cast(n).unwrap()); // Store the digit in the buffer. curr -= 1; - if x == Int::zero() { break; } // No more digits left to accumulate. + if x == zero { break }; // No more digits left to accumulate. } } else { // Do the same as above, but accounting for two's complement. for byte in buf.iter_mut().rev() { - let n = -(x % base); // Get the current place value. + let n = zero - (x % base); // Get the current place value. x = x / base; // Deaccumulate the number. *byte = self.digit(cast(n).unwrap()); // Store the digit in the buffer. curr -= 1; - if x == Int::zero() { break; } // No more digits left to accumulate. + if x == zero { break }; // No more digits left to accumulate. } } f.pad_integral(is_positive, self.prefix(), buf[curr..]) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index adce510610b..ef218c1ce5a 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -19,31 +19,14 @@ use {int, i8, i16, i32, i64}; use {uint, u8, u16, u32, u64}; use {f32, f64}; use clone::Clone; -use cmp::{Ord, PartialEq, PartialOrd}; +use cmp::{PartialEq, Eq}; +use cmp::{PartialOrd, Ord}; use kinds::Copy; 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}; -/// The base trait for numeric types -#[allow(deprecated)] -pub trait Num: PartialEq + Zero + One - + Neg - + Add - + Sub - + Mul - + Div - + Rem {} - -macro_rules! trait_impl( - ($name:ident for $($t:ty)*) => ($( - impl $name for $t {} - )*) -) - -trait_impl!(Num for uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64) - /// Simultaneous division and remainder #[inline] pub fn div_rem + Rem>(x: T, y: T) -> (T, T) { @@ -51,7 +34,7 @@ pub fn div_rem + Rem>(x: T, y: T) -> (T, T) { } /// Useful functions for signed numbers (i.e. numbers that can be negative). -pub trait Signed: Num + Neg { +pub trait Signed: Neg { /// Computes the absolute value. /// /// For `f32` and `f64`, `NaN` will be returned if the number is `NaN`. @@ -161,11 +144,6 @@ signed_float_impl!(f64, f64::NAN, f64::INFINITY, f64::NEG_INFINITY, /// * `-1` if the number is negative #[inline(always)] pub fn signum(value: T) -> T { value.signum() } -/// A trait for values which cannot be negative -pub trait Unsigned: Num {} - -trait_impl!(Unsigned for uint u8 u16 u32 u64) - /// Raises a value to the power of exp, using exponentiation by squaring. /// /// # Example @@ -191,27 +169,25 @@ pub fn pow(mut base: T, mut exp: uint) -> T { } } -/// Specifies the available operations common to all of Rust's core numeric primitives. -/// These may not always make sense from a purely mathematical point of view, but -/// may be useful for systems programming. -pub trait Primitive: Copy - + Clone - + Num - + NumCast - + PartialOrd {} - -trait_impl!(Primitive for uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64) - /// A primitive signed or unsigned integer equipped with various bitwise /// operators, bit counting methods, and endian conversion functions. -pub trait Int: Primitive - + Ord - + Not - + BitAnd - + BitOr - + BitXor - + Shl - + Shr { +pub trait Int + : Copy + Clone + + NumCast + + PartialOrd + Ord + + PartialEq + Eq + + Add + + Sub + + Mul + + Div + + Rem + + Not + + BitAnd + + BitOr + + BitXor + + Shl + + Shr +{ /// Returns the `0` value of this integer. // FIXME (#5527): Should be an associated constant fn zero() -> Self; @@ -1253,13 +1229,24 @@ pub enum FPCategory { FPNormal, } -/// Operations on primitive floating point numbers. +/// Operations on the built-in floating point numbers. // FIXME(#5527): In a future version of Rust, many of these functions will // become constants. // // FIXME(#8888): Several of these functions have a parameter named // `unused_self`. Removing it requires #8888 to be fixed. -pub trait Float: Signed + Primitive { +pub trait Float + : Copy + Clone + + NumCast + + PartialOrd + + PartialEq + + Signed + + Add + + Sub + + Mul + + Div + + Rem +{ /// Returns the NaN value. fn nan() -> Self; /// Returns the infinite value. @@ -1404,6 +1391,33 @@ pub trait Float: Signed + Primitive { // DEPRECATED +macro_rules! trait_impl { + ($name:ident for $($t:ty)*) => { + $(#[allow(deprecated)] impl $name for $t {})* + }; +} + +#[deprecated = "Generalised numbers are no longer supported"] +#[allow(deprecated)] +pub trait Num: PartialEq + Zero + One + + Neg + + Add + + Sub + + Mul + + Div + + Rem {} +trait_impl!(Num for uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64) + +#[deprecated = "Generalised unsigned numbers are no longer supported"] +#[allow(deprecated)] +pub trait Unsigned: Num {} +trait_impl!(Unsigned for uint u8 u16 u32 u64) + +#[deprecated = "Use `Float` or `Int`"] +#[allow(deprecated)] +pub trait Primitive: Copy + Clone + Num + NumCast + PartialOrd {} +trait_impl!(Primitive for uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64) + #[deprecated = "The generic `Zero` trait will be removed soon."] pub trait Zero: Add { #[deprecated = "Use `Int::zero()` or `Float::zero()`."] diff --git a/src/libcoretest/num/mod.rs b/src/libcoretest/num/mod.rs index 8bb238c0b66..51a44ef4d59 100644 --- a/src/libcoretest/num/mod.rs +++ b/src/libcoretest/num/mod.rs @@ -24,7 +24,7 @@ mod u64; mod uint; /// Helper function for testing numeric operations -pub fn test_num(ten: T, two: T) { +pub fn test_num(ten: T, two: T) { assert_eq!(ten.add(&two), cast(12i).unwrap()); assert_eq!(ten.sub(&two), cast(8i).unwrap()); assert_eq!(ten.mul(&two), cast(20i).unwrap()); diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs index f831de88858..28fe7538ca5 100644 --- a/src/libstd/num/mod.rs +++ b/src/libstd/num/mod.rs @@ -135,7 +135,7 @@ pub fn abs_sub(x: T, y: T) -> T { /// Helper function for testing numeric operations #[cfg(test)] -pub fn test_num(ten: T, two: T) { +pub fn test_num(ten: T, two: T) { assert_eq!(ten.add(&two), cast(12i).unwrap()); assert_eq!(ten.sub(&two), cast(8i).unwrap()); assert_eq!(ten.mul(&two), cast(20i).unwrap()); diff --git a/src/libstd/num/strconv.rs b/src/libstd/num/strconv.rs index 7c21b020647..06d2f0ef028 100644 --- a/src/libstd/num/strconv.rs +++ b/src/libstd/num/strconv.rs @@ -116,7 +116,7 @@ fn int_to_str_bytes_common(num: T, radix: uint, sign: SignFormat, f: |u8 // numbers [-35 .. 0] we always have [0 .. 35]. let current_digit_signed = deccum % radix_gen; let current_digit = if current_digit_signed < _0 { - -current_digit_signed + _0 - current_digit_signed } else { current_digit_signed }; diff --git a/src/test/compile-fail/type-params-in-different-spaces-1.rs b/src/test/compile-fail/type-params-in-different-spaces-1.rs index c87e8541758..66479202e12 100644 --- a/src/test/compile-fail/type-params-in-different-spaces-1.rs +++ b/src/test/compile-fail/type-params-in-different-spaces-1.rs @@ -8,15 +8,15 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::num::Num; +use std::num::Int; -trait BrokenAdd: Num { +trait BrokenAdd: Int { fn broken_add(&self, rhs: T) -> Self { *self + rhs //~ ERROR expected `Self`, found `T` } } -impl BrokenAdd for T {} +impl BrokenAdd for T {} pub fn main() { let foo: u8 = 0u8; diff --git a/src/test/run-pass/trait-inheritance-num.rs b/src/test/run-pass/trait-inheritance-num.rs index 3b61a85995f..e606018feb9 100644 --- a/src/test/run-pass/trait-inheritance-num.rs +++ b/src/test/run-pass/trait-inheritance-num.rs @@ -12,11 +12,11 @@ use std::cmp::{PartialEq, PartialOrd}; use std::num::NumCast; -pub trait NumExt: Num + NumCast + PartialEq + PartialOrd {} +pub trait NumExt: NumCast + PartialEq + PartialOrd {} pub trait FloatExt: NumExt {} -fn greater_than_one(n: &T) -> bool { *n > NumCast::from(1i).unwrap() } -fn greater_than_one_float(n: &T) -> bool { *n > NumCast::from(1i).unwrap() } +fn greater_than_one(n: &T) -> bool { *n > NumCast::from(1i).unwrap() } +fn greater_than_one_float(n: &T) -> bool { *n > NumCast::from(1i).unwrap() } pub fn main() {} diff --git a/src/test/run-pass/trait-inheritance-num0.rs b/src/test/run-pass/trait-inheritance-num0.rs index 16e702bb79a..42eaaa09fcd 100644 --- a/src/test/run-pass/trait-inheritance-num0.rs +++ b/src/test/run-pass/trait-inheritance-num0.rs @@ -11,6 +11,7 @@ // Extending Num and using inherited static methods +use std::cmp::PartialOrd; use std::num::NumCast; pub trait Num { @@ -18,7 +19,7 @@ pub trait Num { fn gt(&self, other: &Self) -> bool; } -pub trait NumExt: Num + NumCast { } +pub trait NumExt: NumCast + PartialOrd { } fn greater_than_one(n: &T) -> bool { n.gt(&NumCast::from(1i).unwrap()) diff --git a/src/test/run-pass/trait-inheritance-num1.rs b/src/test/run-pass/trait-inheritance-num1.rs index 5a2e88631a6..9407afbdd6b 100644 --- a/src/test/run-pass/trait-inheritance-num1.rs +++ b/src/test/run-pass/trait-inheritance-num1.rs @@ -11,7 +11,7 @@ use std::cmp::PartialOrd; use std::num::NumCast; -pub trait NumExt: Num + NumCast + PartialOrd { } +pub trait NumExt: NumCast + PartialOrd { } fn greater_than_one(n: &T) -> bool { *n > NumCast::from(1i).unwrap() diff --git a/src/test/run-pass/trait-inheritance-num2.rs b/src/test/run-pass/trait-inheritance-num2.rs index b3f769798b3..aa35f2cd70f 100644 --- a/src/test/run-pass/trait-inheritance-num2.rs +++ b/src/test/run-pass/trait-inheritance-num2.rs @@ -32,7 +32,7 @@ impl TypeExt for f32 {} impl TypeExt for f64 {} -pub trait NumExt: TypeExt + PartialEq + PartialOrd + Num + NumCast {} +pub trait NumExt: TypeExt + PartialEq + PartialOrd + NumCast {} impl NumExt for u8 {} impl NumExt for u16 {} diff --git a/src/test/run-pass/trait-inheritance-num5.rs b/src/test/run-pass/trait-inheritance-num5.rs index e3d631013c0..4c79600e2e9 100644 --- a/src/test/run-pass/trait-inheritance-num5.rs +++ b/src/test/run-pass/trait-inheritance-num5.rs @@ -11,7 +11,7 @@ use std::cmp::PartialEq; use std::num::NumCast; -pub trait NumExt: PartialEq + Num + NumCast {} +pub trait NumExt: PartialEq + NumCast {} impl NumExt for f32 {} impl NumExt for int {}