1
Fork 0

Move impls of Num out of core::num and clean up imports

This commit is contained in:
Brendan Zabarauskas 2013-04-24 20:08:08 +10:00
parent 593bdd9be3
commit 03932f0b84
6 changed files with 53 additions and 77 deletions

View file

@ -10,20 +10,10 @@
//! Operations and constants for `f32`
use num::strconv;
use num::Signed;
use num;
use option::Option;
use from_str;
use to_str;
#[cfg(notest)] use cmp::{Eq, Ord};
#[cfg(stage0,notest)]
use ops::{Add, Sub, Mul, Div, Modulo, Neg};
#[cfg(stage1,notest)]
#[cfg(stage2,notest)]
#[cfg(stage3,notest)]
use ops::{Add, Sub, Mul, Quot, Rem, Neg};
use libc::c_int;
use num::strconv;
use prelude::*;
pub use cmath::c_float_targ_consts::*;
@ -233,6 +223,8 @@ pub fn logarithm(n: f32, b: f32) -> f32 {
return log2(n) / log2(b);
}
impl Num for f32 {}
#[cfg(notest)]
impl Eq for f32 {
#[inline(always)]
@ -588,6 +580,13 @@ impl num::FromStrRadix for f32 {
#[cfg(test)]
mod tests {
use f32::*;
use super::*;
use prelude::*;
#[test]
fn test_num() {
num::test_num(10f32, 2f32);
}
#[test]
pub fn test_signed() {

View file

@ -10,20 +10,10 @@
//! Operations and constants for `f64`
use num::strconv;
use num::Signed;
use num;
use option::Option;
use to_str;
use from_str;
#[cfg(notest)] use cmp::{Eq, Ord};
#[cfg(stage0,notest)]
use ops::{Add, Sub, Mul, Div, Modulo, Neg};
#[cfg(stage1,notest)]
#[cfg(stage2,notest)]
#[cfg(stage3,notest)]
use ops::{Add, Sub, Mul, Quot, Rem, Neg};
use libc::c_int;
use num::strconv;
use prelude::*;
pub use cmath::c_double_targ_consts::*;
pub use cmp::{min, max};
@ -254,6 +244,8 @@ pub fn logarithm(n: f64, b: f64) -> f64 {
return log2(n) / log2(b);
}
impl Num for f64 {}
#[cfg(notest)]
impl Eq for f64 {
#[inline(always)]
@ -596,6 +588,13 @@ impl num::FromStrRadix for f64 {
#[cfg(test)]
mod tests {
use f64::*;
use super::*;
use prelude::*;
#[test]
fn test_num() {
num::test_num(10f64, 2f64);
}
#[test]
pub fn test_signed() {

View file

@ -20,21 +20,10 @@
// PORT this must match in width according to architecture
use f64;
use num::strconv;
use num::Signed;
use num;
use option::Option;
use to_str;
use from_str;
#[cfg(notest)] use cmp::{Eq, Ord};
#[cfg(stage0,notest)]
use ops::{Add, Sub, Mul, Div, Modulo, Neg};
#[cfg(stage1,notest)]
#[cfg(stage2,notest)]
#[cfg(stage3,notest)]
use ops::{Add, Sub, Mul, Quot, Rem, Neg};
use libc::c_int;
use num::strconv;
use prelude::*;
pub use f64::{add, sub, mul, quot, rem, lt, le, eq, ne, ge, gt};
pub use f64::logarithm;
@ -382,6 +371,8 @@ pub fn tan(x: float) -> float {
f64::tan(x as f64) as float
}
impl Num for float {}
#[cfg(notest)]
impl Eq for float {
#[inline(always)]
@ -524,6 +515,11 @@ mod tests {
use super::*;
use prelude::*;
#[test]
fn test_num() {
num::test_num(10f, 2f);
}
#[test]
pub fn test_signed() {
assert_eq!(infinity.abs(), infinity);

View file

@ -10,12 +10,9 @@
use T = self::inst::T;
use to_str::ToStr;
use from_str::FromStr;
use num::{ToStrRadix, FromStrRadix};
use num::strconv;
use num::Signed;
use num;
use prelude::*;
pub use cmp::{min, max};
@ -133,6 +130,8 @@ pub fn compl(i: T) -> T {
#[inline(always)]
pub fn abs(i: T) -> T { i.abs() }
impl Num for T {}
#[cfg(notest)]
impl Ord for T {
#[inline(always)]
@ -522,6 +521,11 @@ mod tests {
use super::inst::T;
use prelude::*;
#[test]
fn test_num() {
num::test_num(10 as T, 2 as T);
}
#[test]
pub fn test_signed() {
assert_eq!((1 as T).abs(), 1 as T);

View file

@ -33,30 +33,18 @@ pub trait Num: Eq + Zero + One
+ Quot<Self,Self>
+ Rem<Self,Self> {}
impl Num for u8 {}
impl Num for u16 {}
impl Num for u32 {}
impl Num for u64 {}
impl Num for uint {}
impl Num for i8 {}
impl Num for i16 {}
impl Num for i32 {}
impl Num for i64 {}
impl Num for int {}
impl Num for f32 {}
impl Num for f64 {}
impl Num for float {}
pub trait IntConvertible {
fn to_int(&self) -> int;
fn from_int(n: int) -> Self;
}
pub trait Zero {
// FIXME (#5527): These should be associated constants
fn zero() -> Self;
}
pub trait One {
// FIXME (#5527): These should be associated constants
fn one() -> Self;
}
@ -230,8 +218,9 @@ pub fn pow_with_uint<T:NumCast+One+Zero+Copy+Quot<T,T>+Mul<T,T>>(
total
}
/// Helper function for testing numeric operations
#[cfg(stage0,test)]
fn test_num<T:Num + NumCast>(ten: T, two: T) {
pub fn test_num<T:Num + NumCast>(ten: T, two: T) {
assert_eq!(ten.add(&two), cast(12));
assert_eq!(ten.sub(&two), cast(8));
assert_eq!(ten.mul(&two), cast(20));
@ -247,7 +236,7 @@ fn test_num<T:Num + NumCast>(ten: T, two: T) {
#[cfg(stage1,test)]
#[cfg(stage2,test)]
#[cfg(stage3,test)]
fn test_num<T:Num + NumCast>(ten: T, two: T) {
pub fn test_num<T:Num + NumCast>(ten: T, two: T) {
assert_eq!(ten.add(&two), cast(12));
assert_eq!(ten.sub(&two), cast(8));
assert_eq!(ten.mul(&two), cast(20));
@ -261,20 +250,6 @@ fn test_num<T:Num + NumCast>(ten: T, two: T) {
assert_eq!(ten.rem(&two), ten % two);
}
#[test] fn test_u8_num() { test_num(10u8, 2u8) }
#[test] fn test_u16_num() { test_num(10u16, 2u16) }
#[test] fn test_u32_num() { test_num(10u32, 2u32) }
#[test] fn test_u64_num() { test_num(10u64, 2u64) }
#[test] fn test_uint_num() { test_num(10u, 2u) }
#[test] fn test_i8_num() { test_num(10i8, 2i8) }
#[test] fn test_i16_num() { test_num(10i16, 2i16) }
#[test] fn test_i32_num() { test_num(10i32, 2i32) }
#[test] fn test_i64_num() { test_num(10i64, 2i64) }
#[test] fn test_int_num() { test_num(10i, 2i) }
#[test] fn test_f32_num() { test_num(10f32, 2f32) }
#[test] fn test_f64_num() { test_num(10f64, 2f64) }
#[test] fn test_float_num() { test_num(10f, 2f) }
macro_rules! test_cast_20(
($_20:expr) => ({
let _20 = $_20;

View file

@ -11,13 +11,9 @@
use T = self::inst::T;
use T_SIGNED = self::inst::T_SIGNED;
use to_str::ToStr;
use from_str::FromStr;
use num::{ToStrRadix, FromStrRadix};
use num::strconv;
use num::Unsigned;
use num;
use option::Option;
use prelude::*;
pub use cmp::{min, max};
@ -100,6 +96,8 @@ pub fn compl(i: T) -> T {
max_value ^ i
}
impl Num for T {}
#[cfg(notest)]
impl Ord for T {
#[inline(always)]
@ -356,6 +354,11 @@ mod tests {
use super::inst::T;
use prelude::*;
#[test]
fn test_num() {
num::test_num(10 as T, 2 as T);
}
#[test]
fn test_gcd() {
assert_eq!((10 as T).gcd(2), 2 as T);