Deprecate Num, Unsigned and Primitive

This commit is contained in:
Brendan Zabarauskas 2014-11-10 11:30:52 +11:00
parent 46333d527b
commit 26196715e8
13 changed files with 83 additions and 68 deletions

View File

@ -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

View File

@ -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<T: Primitive + Float, U>(
pub fn float_to_str_bytes_common<T: Float, U>(
num: T,
radix: uint,
negative_zero: bool,

View File

@ -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.
@ -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..])

View File

@ -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<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) {
@ -51,7 +34,7 @@ pub fn div_rem<T: Div<T, T> + Rem<T, T>>(x: T, y: T) -> (T, T) {
}
/// 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`.
@ -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
@ -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;
@ -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.
@ -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()`."]

View File

@ -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());

View File

@ -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());

View File

@ -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
};

View File

@ -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;

View File

@ -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() {}

View File

@ -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<T:NumExt>(n: &T) -> bool {
n.gt(&NumCast::from(1i).unwrap())

View File

@ -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()

View File

@ -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 {}

View File

@ -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 {}