Skip to content

Commit

Permalink
Deprecate Num, Unsigned and Primitive
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanzab committed Nov 12, 2014
1 parent 46333d5 commit 2619671
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 68 deletions.
3 changes: 1 addition & 2 deletions src/etc/vim/syntax/rust.vim
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/fmt/float.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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<T: Primitive + Float, U>(
pub fn float_to_str_bytes_common<T: Float, U>(
num: T,
radix: uint,
negative_zero: bool,
Expand Down
11 changes: 6 additions & 5 deletions src/libcore/fmt/num.rs
Expand Up @@ -35,10 +35,11 @@ trait GenericRadix {
fn fmt_int<T: 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.
Expand All @@ -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..])
Expand Down
106 changes: 60 additions & 46 deletions src/libcore/num/mod.rs
Expand Up @@ -19,39 +19,22 @@ 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<Self>
+ Add<Self,Self>
+ Sub<Self,Self>
+ Mul<Self,Self>
+ Div<Self,Self>
+ Rem<Self,Self> {}

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<T: Div<T, T> + Rem<T, T>>(x: T, y: T) -> (T, T) {
(x / y, x % y)
}

/// Useful functions for signed numbers (i.e. numbers that can be negative).
pub trait Signed: Num + Neg<Self> {
pub trait Signed: Neg<Self> {
/// Computes the absolute value.
///
/// For `f32` and `f64`, `NaN` will be returned if the number is `NaN`.
Expand Down Expand Up @@ -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<T: Signed>(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
Expand All @@ -191,27 +169,25 @@ pub fn pow<T: Int>(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<Self>
+ BitAnd<Self,Self>
+ BitOr<Self,Self>
+ BitXor<Self,Self>
+ Shl<uint,Self>
+ Shr<uint,Self> {
pub trait Int
: Copy + Clone
+ NumCast
+ PartialOrd + Ord
+ PartialEq + Eq
+ Add<Self,Self>
+ Sub<Self,Self>
+ Mul<Self,Self>
+ Div<Self,Self>
+ Rem<Self,Self>
+ Not<Self>
+ BitAnd<Self,Self>
+ BitOr<Self,Self>
+ BitXor<Self,Self>
+ Shl<uint,Self>
+ Shr<uint,Self>
{
/// Returns the `0` value of this integer.
// FIXME (#5527): Should be an associated constant
fn zero() -> Self;
Expand Down Expand Up @@ -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<Self,Self>
+ Sub<Self,Self>
+ Mul<Self,Self>
+ Div<Self,Self>
+ Rem<Self,Self>
{
/// Returns the NaN value.
fn nan() -> Self;
/// Returns the infinite value.
Expand Down Expand Up @@ -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<Self>
+ Add<Self,Self>
+ Sub<Self,Self>
+ Mul<Self,Self>
+ Div<Self,Self>
+ Rem<Self,Self> {}
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<Self, Self> {
#[deprecated = "Use `Int::zero()` or `Float::zero()`."]
Expand Down
2 changes: 1 addition & 1 deletion src/libcoretest/num/mod.rs
Expand Up @@ -24,7 +24,7 @@ mod u64;
mod uint;

/// Helper function for testing numeric operations
pub fn test_num<T:Num + NumCast + ::std::fmt::Show>(ten: T, two: T) {
pub fn test_num<T: Int + ::std::fmt::Show>(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());
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/num/mod.rs
Expand Up @@ -135,7 +135,7 @@ pub fn abs_sub<T: FloatMath>(x: T, y: T) -> T {

/// Helper function for testing numeric operations
#[cfg(test)]
pub fn test_num<T:Num + NumCast + Show>(ten: T, two: T) {
pub fn test_num<T: Int + Show>(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());
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/num/strconv.rs
Expand Up @@ -116,7 +116,7 @@ fn int_to_str_bytes_common<T: Int>(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
};
Expand Down
6 changes: 3 additions & 3 deletions src/test/compile-fail/type-params-in-different-spaces-1.rs
Expand Up @@ -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<T>(&self, rhs: T) -> Self {
*self + rhs //~ ERROR expected `Self`, found `T`
}
}

impl<T: Num> BrokenAdd for T {}
impl<T: Int> BrokenAdd for T {}

pub fn main() {
let foo: u8 = 0u8;
Expand Down
6 changes: 3 additions & 3 deletions src/test/run-pass/trait-inheritance-num.rs
Expand Up @@ -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<T:NumExt>(n: &T) -> bool { *n > NumCast::from(1i).unwrap() }
fn greater_than_one_float<T:FloatExt>(n: &T) -> bool { *n > NumCast::from(1i).unwrap() }
fn greater_than_one<T: NumExt>(n: &T) -> bool { *n > NumCast::from(1i).unwrap() }
fn greater_than_one_float<T: FloatExt>(n: &T) -> bool { *n > NumCast::from(1i).unwrap() }

pub fn main() {}
3 changes: 2 additions & 1 deletion src/test/run-pass/trait-inheritance-num0.rs
Expand Up @@ -11,14 +11,15 @@

// Extending Num and using inherited static methods

use std::cmp::PartialOrd;
use std::num::NumCast;

pub trait Num {
fn from_int(i: int) -> Self;
fn gt(&self, other: &Self) -> bool;
}

pub trait NumExt: Num + NumCast { }
pub trait NumExt: NumCast + PartialOrd { }

fn greater_than_one<T:NumExt>(n: &T) -> bool {
n.gt(&NumCast::from(1i).unwrap())
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/trait-inheritance-num1.rs
Expand Up @@ -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<T:NumExt>(n: &T) -> bool {
*n > NumCast::from(1i).unwrap()
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/trait-inheritance-num2.rs
Expand Up @@ -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 {}
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/trait-inheritance-num5.rs
Expand Up @@ -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 {}
Expand Down

0 comments on commit 2619671

Please sign in to comment.