libcore: Move the numeric operations out of Num. r=brson

Sadly I could not use trait inheritance due to a type parameter substitution
bug.
This commit is contained in:
Patrick Walton 2013-02-12 17:07:26 -08:00
parent 6efa3543a8
commit 216e85fadf
15 changed files with 165 additions and 118 deletions

View file

@ -199,7 +199,7 @@ pub use vec::{OwnedVector, OwnedCopyableVector};
pub use iter::{BaseIter, ExtendedIter, EqIter, CopyableIter}; pub use iter::{BaseIter, ExtendedIter, EqIter, CopyableIter};
pub use iter::{CopyableOrderedIter, CopyableNonstrictIter, Times}; pub use iter::{CopyableOrderedIter, CopyableNonstrictIter, Times};
pub use num::{Num, NumCast}; pub use num::NumCast;
pub use ptr::Ptr; pub use ptr::Ptr;
pub use to_str::ToStr; pub use to_str::ToStr;
pub use clone::Clone; pub use clone::Clone;

View file

@ -13,8 +13,9 @@
use cmath; use cmath;
use cmp; use cmp;
use libc::{c_float, c_int}; use libc::{c_float, c_int};
use num;
use num::NumCast; use num::NumCast;
use num;
use ops;
use option::Option; use option::Option;
use from_str; use from_str;
use to_str; use to_str;
@ -271,21 +272,6 @@ impl f32 : cmp::Ord {
pure fn gt(&self, other: &f32) -> bool { (*self) > (*other) } pure fn gt(&self, other: &f32) -> bool { (*self) > (*other) }
} }
impl f32: num::Num {
#[inline(always)]
pure fn add(&self, other: &f32) -> f32 { return *self + *other; }
#[inline(always)]
pure fn sub(&self, other: &f32) -> f32 { return *self - *other; }
#[inline(always)]
pure fn mul(&self, other: &f32) -> f32 { return *self * *other; }
#[inline(always)]
pure fn div(&self, other: &f32) -> f32 { return *self / *other; }
#[inline(always)]
pure fn modulo(&self, other: &f32) -> f32 { return *self % *other; }
#[inline(always)]
pure fn neg(&self) -> f32 { return -*self; }
}
impl f32: num::Zero { impl f32: num::Zero {
#[inline(always)] #[inline(always)]
static pure fn zero() -> f32 { 0.0 } static pure fn zero() -> f32 { 0.0 }
@ -320,6 +306,31 @@ pub impl f32: NumCast {
#[inline(always)] pure fn to_float(&self) -> float { *self as float } #[inline(always)] pure fn to_float(&self) -> float { *self as float }
} }
#[cfg(notest)]
impl ops::Add<f32,f32> for f32 {
pure fn add(&self, other: &f32) -> f32 { *self + *other }
}
#[cfg(notest)]
impl ops::Sub<f32,f32> for f32 {
pure fn sub(&self, other: &f32) -> f32 { *self - *other }
}
#[cfg(notest)]
impl ops::Mul<f32,f32> for f32 {
pure fn mul(&self, other: &f32) -> f32 { *self * *other }
}
#[cfg(notest)]
impl ops::Div<f32,f32> for f32 {
pure fn div(&self, other: &f32) -> f32 { *self / *other }
}
#[cfg(notest)]
impl ops::Modulo<f32,f32> for f32 {
pure fn modulo(&self, other: &f32) -> f32 { *self % *other }
}
#[cfg(notest)]
impl ops::Neg<f32> for f32 {
pure fn neg(&self) -> f32 { -*self }
}
#[abi="rust-intrinsic"] #[abi="rust-intrinsic"]
pub extern { pub extern {
fn floorf32(val: f32) -> f32; fn floorf32(val: f32) -> f32;

View file

@ -14,8 +14,9 @@ use cmath;
use cmp; use cmp;
use libc::{c_double, c_int}; use libc::{c_double, c_int};
use libc; use libc;
use num;
use num::NumCast; use num::NumCast;
use num;
use ops;
use option::Option; use option::Option;
use to_str; use to_str;
use from_str; use from_str;
@ -296,21 +297,6 @@ impl f64 : cmp::Ord {
pure fn gt(&self, other: &f64) -> bool { (*self) > (*other) } pure fn gt(&self, other: &f64) -> bool { (*self) > (*other) }
} }
impl f64: num::Num {
#[inline(always)]
pure fn add(&self, other: &f64) -> f64 { return *self + *other; }
#[inline(always)]
pure fn sub(&self, other: &f64) -> f64 { return *self - *other; }
#[inline(always)]
pure fn mul(&self, other: &f64) -> f64 { return *self * *other; }
#[inline(always)]
pure fn div(&self, other: &f64) -> f64 { return *self / *other; }
#[inline(always)]
pure fn modulo(&self, other: &f64) -> f64 { return *self % *other; }
#[inline(always)]
pure fn neg(&self) -> f64 { return -*self; }
}
pub impl f64: NumCast { pub impl f64: NumCast {
/** /**
* Cast `n` to an `f64` * Cast `n` to an `f64`
@ -345,6 +331,31 @@ impl f64: num::One {
static pure fn one() -> f64 { 1.0 } static pure fn one() -> f64 { 1.0 }
} }
#[cfg(notest)]
impl ops::Add<f64,f64> for f64 {
pure fn add(&self, other: &f64) -> f64 { *self + *other }
}
#[cfg(notest)]
impl ops::Sub<f64,f64> for f64 {
pure fn sub(&self, other: &f64) -> f64 { *self - *other }
}
#[cfg(notest)]
impl ops::Mul<f64,f64> for f64 {
pure fn mul(&self, other: &f64) -> f64 { *self * *other }
}
#[cfg(notest)]
impl ops::Div<f64,f64> for f64 {
pure fn div(&self, other: &f64) -> f64 { *self / *other }
}
#[cfg(notest)]
impl ops::Modulo<f64,f64> for f64 {
pure fn modulo(&self, other: &f64) -> f64 { *self % *other }
}
#[cfg(notest)]
impl ops::Neg<f64> for f64 {
pure fn neg(&self) -> f64 { -*self }
}
#[abi="rust-intrinsic"] #[abi="rust-intrinsic"]
pub extern { pub extern {
fn floorf64(val: f64) -> f64; fn floorf64(val: f64) -> f64;

View file

@ -25,8 +25,9 @@ use m_float = f64;
use cmp::{Eq, Ord}; use cmp::{Eq, Ord};
use cmp; use cmp;
use f64; use f64;
use num;
use num::NumCast; use num::NumCast;
use num;
use ops;
use option::{None, Option, Some}; use option::{None, Option, Some};
use str; use str;
use uint; use uint;
@ -404,21 +405,6 @@ impl float : Ord {
pure fn gt(&self, other: &float) -> bool { (*self) > (*other) } pure fn gt(&self, other: &float) -> bool { (*self) > (*other) }
} }
impl float: num::Num {
#[inline(always)]
pub pure fn add(&self, other: &float) -> float { return *self + *other; }
#[inline(always)]
pub pure fn sub(&self, other: &float) -> float { return *self - *other; }
#[inline(always)]
pub pure fn mul(&self, other: &float) -> float { return *self * *other; }
#[inline(always)]
pub pure fn div(&self, other: &float) -> float { return *self / *other; }
#[inline(always)]
pure fn modulo(&self, other: &float) -> float { return *self % *other; }
#[inline(always)]
pure fn neg(&self) -> float { return -*self; }
}
impl float: num::Zero { impl float: num::Zero {
#[inline(always)] #[inline(always)]
static pure fn zero() -> float { 0.0 } static pure fn zero() -> float { 0.0 }
@ -486,6 +472,31 @@ impl float: num::Round {
} }
} }
#[cfg(notest)]
impl ops::Add<float,float> for float {
pure fn add(&self, other: &float) -> float { *self + *other }
}
#[cfg(notest)]
impl ops::Sub<float,float> for float {
pure fn sub(&self, other: &float) -> float { *self - *other }
}
#[cfg(notest)]
impl ops::Mul<float,float> for float {
pure fn mul(&self, other: &float) -> float { *self * *other }
}
#[cfg(notest)]
impl ops::Div<float,float> for float {
pure fn div(&self, other: &float) -> float { *self / *other }
}
#[cfg(notest)]
impl ops::Modulo<float,float> for float {
pure fn modulo(&self, other: &float) -> float { *self % *other }
}
#[cfg(notest)]
impl ops::Neg<float> for float {
pure fn neg(&self) -> float { -*self }
}
#[test] #[test]
pub fn test_from_str() { pub fn test_from_str() {
assert from_str(~"3") == Some(3.); assert from_str(~"3") == Some(3.);

View file

@ -166,21 +166,6 @@ impl T : Eq {
pure fn ne(&self, other: &T) -> bool { return (*self) != (*other); } pure fn ne(&self, other: &T) -> bool { return (*self) != (*other); }
} }
impl T: num::Num {
#[inline(always)]
pure fn add(&self, other: &T) -> T { return *self + *other; }
#[inline(always)]
pure fn sub(&self, other: &T) -> T { return *self - *other; }
#[inline(always)]
pure fn mul(&self, other: &T) -> T { return *self * *other; }
#[inline(always)]
pure fn div(&self, other: &T) -> T { return *self / *other; }
#[inline(always)]
pure fn modulo(&self, other: &T) -> T { return *self % *other; }
#[inline(always)]
pure fn neg(&self) -> T { return -*self; }
}
impl T: num::Zero { impl T: num::Zero {
#[inline(always)] #[inline(always)]
static pure fn zero() -> T { 0 } static pure fn zero() -> T { 0 }
@ -203,6 +188,31 @@ impl T: num::Round {
pure fn fract(&self) -> T { 0 } pure fn fract(&self) -> T { 0 }
} }
#[cfg(notest)]
impl ops::Add<T,T> for T {
pure fn add(&self, other: &T) -> T { *self + *other }
}
#[cfg(notest)]
impl ops::Sub<T,T> for T {
pure fn sub(&self, other: &T) -> T { *self - *other }
}
#[cfg(notest)]
impl ops::Mul<T,T> for T {
pure fn mul(&self, other: &T) -> T { *self * *other }
}
#[cfg(notest)]
impl ops::Div<T,T> for T {
pure fn div(&self, other: &T) -> T { *self / *other }
}
#[cfg(notest)]
impl ops::Modulo<T,T> for T {
pure fn modulo(&self, other: &T) -> T { *self % *other }
}
#[cfg(notest)]
impl ops::Neg<T> for T {
pure fn neg(&self) -> T { -*self }
}
// String conversion functions and impl str -> num // String conversion functions and impl str -> num
/// Parse a string as a number in base 10. /// Parse a string as a number in base 10.

View file

@ -10,22 +10,13 @@
//! An interface for numeric types //! An interface for numeric types
use core::cmp::{Ord, Eq}; use core::cmp::{Ord, Eq};
use ops::{Add, Div, Modulo, Mul, Neg, Sub};
use option::{None, Option, Some}; use option::{None, Option, Some};
use char; use char;
use str; use str;
use kinds::Copy; use kinds::Copy;
use vec; use vec;
pub trait Num {
// FIXME: Trait composition. (#2616)
pure fn add(&self, other: &Self) -> Self;
pure fn sub(&self, other: &Self) -> Self;
pure fn mul(&self, other: &Self) -> Self;
pure fn div(&self, other: &Self) -> Self;
pure fn modulo(&self, other: &Self) -> Self;
pure fn neg(&self) -> Self;
}
pub trait IntConvertible { pub trait IntConvertible {
pure fn to_int(&self) -> int; pure fn to_int(&self) -> int;
static pure fn from_int(n: int) -> Self; static pure fn from_int(n: int) -> Self;
@ -39,7 +30,7 @@ pub trait One {
static pure fn one() -> Self; static pure fn one() -> Self;
} }
pub pure fn abs<T: Ord Num Zero>(v: T) -> T { pub pure fn abs<T:Ord + Zero + Neg<T>>(v: T) -> T {
if v < Zero::zero() { v.neg() } else { v } if v < Zero::zero() { v.neg() } else { v }
} }
@ -109,7 +100,7 @@ pub trait FromStrRadix {
/// Dynamically calculates the value `inf` (`1/0`). /// Dynamically calculates the value `inf` (`1/0`).
/// Can fail on integer types. /// Can fail on integer types.
#[inline(always)] #[inline(always)]
pub pure fn infinity<T: Num One Zero>() -> T { pub pure fn infinity<T:One+Zero+Div<T,T>>() -> T {
let _0: T = Zero::zero(); let _0: T = Zero::zero();
let _1: T = One::one(); let _1: T = One::one();
_1 / _0 _1 / _0
@ -118,7 +109,7 @@ pub pure fn infinity<T: Num One Zero>() -> T {
/// Dynamically calculates the value `-inf` (`-1/0`). /// Dynamically calculates the value `-inf` (`-1/0`).
/// Can fail on integer types. /// Can fail on integer types.
#[inline(always)] #[inline(always)]
pub pure fn neg_infinity<T: Num One Zero>() -> T { pub pure fn neg_infinity<T:One+Zero+Div<T,T>+Neg<T>>() -> T {
let _0: T = Zero::zero(); let _0: T = Zero::zero();
let _1: T = One::one(); let _1: T = One::one();
- _1 / _0 - _1 / _0
@ -127,7 +118,7 @@ pub pure fn neg_infinity<T: Num One Zero>() -> T {
/// Dynamically calculates the value `NaN` (`0/0`). /// Dynamically calculates the value `NaN` (`0/0`).
/// Can fail on integer types. /// Can fail on integer types.
#[inline(always)] #[inline(always)]
pub pure fn NaN<T: Num Zero>() -> T { pub pure fn NaN<T:Zero+Div<T,T>>() -> T {
let _0: T = Zero::zero(); let _0: T = Zero::zero();
_0 / _0 _0 / _0
} }
@ -135,27 +126,28 @@ pub pure fn NaN<T: Num Zero>() -> T {
/// Returns `true` if `num` has the value `inf` (`1/0`). /// Returns `true` if `num` has the value `inf` (`1/0`).
/// Can fail on integer types. /// Can fail on integer types.
#[inline(always)] #[inline(always)]
pub pure fn is_infinity<T: Num One Zero Eq>(num: &T) -> bool { pub pure fn is_infinity<T:One+Zero+Eq+Div<T,T>>(num: &T) -> bool {
(*num) == (infinity::<T>()) (*num) == (infinity::<T>())
} }
/// Returns `true` if `num` has the value `-inf` (`-1/0`). /// Returns `true` if `num` has the value `-inf` (`-1/0`).
/// Can fail on integer types. /// Can fail on integer types.
#[inline(always)] #[inline(always)]
pub pure fn is_neg_infinity<T: Num One Zero Eq>(num: &T) -> bool { pub pure fn is_neg_infinity<T:One+Zero+Eq+Div<T,T>+Neg<T>>(num: &T)
-> bool {
(*num) == (neg_infinity::<T>()) (*num) == (neg_infinity::<T>())
} }
/// Returns `true` if `num` has the value `NaN` (is not equal to itself). /// Returns `true` if `num` has the value `NaN` (is not equal to itself).
#[inline(always)] #[inline(always)]
pub pure fn is_NaN<T: Num Eq>(num: &T) -> bool { pub pure fn is_NaN<T:Eq>(num: &T) -> bool {
(*num) != (*num) (*num) != (*num)
} }
/// Returns `true` if `num` has the value `-0` (`1/num == -1/0`). /// Returns `true` if `num` has the value `-0` (`1/num == -1/0`).
/// Can fail on integer types. /// Can fail on integer types.
#[inline(always)] #[inline(always)]
pub pure fn is_neg_zero<T: Num One Zero Eq>(num: &T) -> bool { pub pure fn is_neg_zero<T:One+Zero+Eq+Div<T,T>+Neg<T>>(num: &T) -> bool {
let _1: T = One::one(); let _1: T = One::one();
let _0: T = Zero::zero(); let _0: T = Zero::zero();
*num == _0 && is_neg_infinity(&(_1 / *num)) *num == _0 && is_neg_infinity(&(_1 / *num))
@ -174,8 +166,8 @@ pub pure fn is_neg_zero<T: Num One Zero Eq>(num: &T) -> bool {
* - If code written to use this function doesn't care about it, it's * - If code written to use this function doesn't care about it, it's
* probably assuming that `x^0` always equals `1`. * probably assuming that `x^0` always equals `1`.
*/ */
pub pure fn pow_with_uint<T: Num NumCast One Zero Copy>(radix: uint, pub pure fn pow_with_uint<T:NumCast+One+Zero+Copy+Div<T,T>+Mul<T,T>>(
pow: uint) -> T { radix: uint, pow: uint) -> T {
let _0: T = Zero::zero(); let _0: T = Zero::zero();
let _1: T = One::one(); let _1: T = One::one();
@ -256,7 +248,8 @@ pub enum SignFormat {
* those special values, and `special` is `false`, because then the * those special values, and `special` is `false`, because then the
* algorithm just does normal calculations on them. * algorithm just does normal calculations on them.
*/ */
pub pure fn to_str_bytes_common<T: Num NumCast Zero One Eq Ord Round Copy>( pub pure fn to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Round+Copy+Div<T,T>+
Neg<T>+Modulo<T,T>+Mul<T,T>>(
num: &T, radix: uint, special: bool, negative_zero: bool, num: &T, radix: uint, special: bool, negative_zero: bool,
sign: SignFormat, digits: SignificantDigits) -> (~[u8], bool) { sign: SignFormat, digits: SignificantDigits) -> (~[u8], bool) {
if radix as int < 2 { if radix as int < 2 {
@ -478,7 +471,8 @@ pub pure fn to_str_bytes_common<T: Num NumCast Zero One Eq Ord Round Copy>(
* `to_str_bytes_common()`, for details see there. * `to_str_bytes_common()`, for details see there.
*/ */
#[inline(always)] #[inline(always)]
pub pure fn to_str_common<T: Num NumCast Zero One Eq Ord Round Copy>( pub pure fn to_str_common<T:NumCast+Zero+One+Eq+Ord+Round+Copy+Div<T,T>+Neg<T>
+Modulo<T,T>+Mul<T,T>>(
num: &T, radix: uint, special: bool, negative_zero: bool, num: &T, radix: uint, special: bool, negative_zero: bool,
sign: SignFormat, digits: SignificantDigits) -> (~str, bool) { sign: SignFormat, digits: SignificantDigits) -> (~str, bool) {
let (bytes, special) = to_str_bytes_common(num, radix, special, let (bytes, special) = to_str_bytes_common(num, radix, special,
@ -533,7 +527,8 @@ priv const DIGIT_E_RADIX: uint = ('e' as uint) - ('a' as uint) + 11u;
* - Could accept option to allow ignoring underscores, allowing for numbers * - Could accept option to allow ignoring underscores, allowing for numbers
* formated like `FF_AE_FF_FF`. * formated like `FF_AE_FF_FF`.
*/ */
pub pure fn from_str_bytes_common<T: Num NumCast Zero One Ord Copy>( pub pure fn from_str_bytes_common<T:NumCast+Zero+One+Ord+Copy+Div<T,T>+
Mul<T,T>+Sub<T,T>+Neg<T>+Add<T,T>>(
buf: &[u8], radix: uint, negative: bool, fractional: bool, buf: &[u8], radix: uint, negative: bool, fractional: bool,
special: bool, exponent: ExponentFormat, empty_zero: bool special: bool, exponent: ExponentFormat, empty_zero: bool
) -> Option<T> { ) -> Option<T> {
@ -720,7 +715,8 @@ pub pure fn from_str_bytes_common<T: Num NumCast Zero One Ord Copy>(
* `from_str_bytes_common()`, for details see there. * `from_str_bytes_common()`, for details see there.
*/ */
#[inline(always)] #[inline(always)]
pub pure fn from_str_common<T: Num NumCast Zero One Ord Copy>( pub pure fn from_str_common<T:NumCast+Zero+One+Ord+Copy+Div<T,T>+Mul<T,T>+
Sub<T,T>+Neg<T>+Add<T,T>>(
buf: &str, radix: uint, negative: bool, fractional: bool, buf: &str, radix: uint, negative: bool, fractional: bool,
special: bool, exponent: ExponentFormat, empty_zero: bool special: bool, exponent: ExponentFormat, empty_zero: bool
) -> Option<T> { ) -> Option<T> {

View file

@ -130,21 +130,6 @@ impl T : Eq {
pure fn ne(&self, other: &T) -> bool { return (*self) != (*other); } pure fn ne(&self, other: &T) -> bool { return (*self) != (*other); }
} }
impl T: num::Num {
#[inline(always)]
pure fn add(&self, other: &T) -> T { return *self + *other; }
#[inline(always)]
pure fn sub(&self, other: &T) -> T { return *self - *other; }
#[inline(always)]
pure fn mul(&self, other: &T) -> T { return *self * *other; }
#[inline(always)]
pure fn div(&self, other: &T) -> T { return *self / *other; }
#[inline(always)]
pure fn modulo(&self, other: &T) -> T { return *self % *other; }
#[inline(always)]
pure fn neg(&self) -> T { return -*self; }
}
impl T: num::Zero { impl T: num::Zero {
#[inline(always)] #[inline(always)]
static pure fn zero() -> T { 0 } static pure fn zero() -> T { 0 }
@ -167,6 +152,31 @@ impl T: num::Round {
pure fn fract(&self) -> T { 0 } pure fn fract(&self) -> T { 0 }
} }
#[cfg(notest)]
impl ops::Add<T,T> for T {
pure fn add(&self, other: &T) -> T { *self + *other }
}
#[cfg(notest)]
impl ops::Sub<T,T> for T {
pure fn sub(&self, other: &T) -> T { *self - *other }
}
#[cfg(notest)]
impl ops::Mul<T,T> for T {
pure fn mul(&self, other: &T) -> T { *self * *other }
}
#[cfg(notest)]
impl ops::Div<T,T> for T {
pure fn div(&self, other: &T) -> T { *self / *other }
}
#[cfg(notest)]
impl ops::Modulo<T,T> for T {
pure fn modulo(&self, other: &T) -> T { *self % *other }
}
#[cfg(notest)]
impl ops::Neg<T> for T {
pure fn neg(&self) -> T { -*self }
}
// String conversion functions and impl str -> num // String conversion functions and impl str -> num
/// Parse a string as a number in base 10. /// Parse a string as a number in base 10.

View file

@ -29,7 +29,7 @@ pub use container::{Container, Mutable, Map, Set};
pub use hash::Hash; pub use hash::Hash;
pub use iter::{BaseIter, ExtendedIter, EqIter, CopyableIter}; pub use iter::{BaseIter, ExtendedIter, EqIter, CopyableIter};
pub use iter::{CopyableOrderedIter, CopyableNonstrictIter, Times}; pub use iter::{CopyableOrderedIter, CopyableNonstrictIter, Times};
pub use num::{Num, NumCast}; pub use num::NumCast;
pub use path::GenericPath; pub use path::GenericPath;
pub use path::Path; pub use path::Path;
pub use path::PosixPath; pub use path::PosixPath;

View file

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
// error-pattern: binary operation + cannot be applied to type // error-pattern: mismatched types
type clam = {x: @int, y: @int}; type clam = {x: @int, y: @int};
type fish = {a: @int}; type fish = {a: @int};

View file

@ -8,11 +8,11 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
pure fn Matrix4<T:Copy Num>(m11: T, m12: T, m13: T, m14: T, pure fn Matrix4<T:Copy>(m11: T, m12: T, m13: T, m14: T,
m21: T, m22: T, m23: T, m24: T, m21: T, m22: T, m23: T, m24: T,
m31: T, m32: T, m33: T, m34: T, m31: T, m32: T, m33: T, m34: T,
m41: T, m42: T, m43: T, m44: T) m41: T, m42: T, m43: T, m44: T)
-> Matrix4<T> { -> Matrix4<T> {
Matrix4 { Matrix4 {
m11: m11, m12: m12, m13: m13, m14: m14, m11: m11, m12: m12, m13: m13, m14: m14,

View file

@ -16,7 +16,7 @@ use num::NumCast::from;
extern mod std; extern mod std;
use std::cmp::FuzzyEq; use std::cmp::FuzzyEq;
pub trait NumExt: Num NumCast Eq Ord {} pub trait NumExt: NumCast Eq Ord {}
pub trait FloatExt: NumExt FuzzyEq<Self> {} pub trait FloatExt: NumExt FuzzyEq<Self> {}

View file

@ -8,12 +8,10 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
// Using the real Num from core
use cmp::Ord; use cmp::Ord;
use num::NumCast::from; use num::NumCast::from;
pub trait NumExt: Num NumCast Ord { } pub trait NumExt: NumCast Ord { }
fn greater_than_one<T:NumExt>(n: &T) -> bool { fn greater_than_one<T:NumExt>(n: &T) -> bool {
*n > from(1) *n > from(1)

View file

@ -38,7 +38,7 @@ pub impl f64: TypeExt {}
pub impl float: TypeExt {} pub impl float: TypeExt {}
pub trait NumExt: TypeExt Eq Ord Num NumCast {} pub trait NumExt: TypeExt Eq Ord NumCast {}
pub impl u8: NumExt {} pub impl u8: NumExt {}
pub impl u16: NumExt {} pub impl u16: NumExt {}

View file

@ -11,7 +11,7 @@
use cmp::{Eq, Ord}; use cmp::{Eq, Ord};
use num::NumCast::from; use num::NumCast::from;
pub trait NumExt: Eq Ord Num NumCast {} pub trait NumExt: Eq Ord NumCast {}
pub impl f32: NumExt {} pub impl f32: NumExt {}
@ -19,4 +19,4 @@ fn num_eq_one<T:NumExt>(n: T) { io::println(fmt!("%?", n == from(1))) }
pub fn main() { pub fn main() {
num_eq_one(1f32); // you need to actually use the function to trigger the ICE num_eq_one(1f32); // you need to actually use the function to trigger the ICE
} }

View file

@ -11,7 +11,7 @@
use cmp::{Eq, Ord}; use cmp::{Eq, Ord};
use num::NumCast::from; use num::NumCast::from;
pub trait NumExt: Eq Num NumCast {} pub trait NumExt: Eq NumCast {}
pub impl f32: NumExt {} pub impl f32: NumExt {}
pub impl int: NumExt {} pub impl int: NumExt {}