Skip to content

Commit 03932f0

Browse files
committed
Move impls of Num out of core::num and clean up imports
1 parent 593bdd9 commit 03932f0

File tree

6 files changed

+53
-77
lines changed

6 files changed

+53
-77
lines changed

src/libcore/num/f32.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,10 @@
1010

1111
//! Operations and constants for `f32`
1212
13-
use num::strconv;
14-
use num::Signed;
15-
use num;
16-
use option::Option;
1713
use from_str;
18-
use to_str;
19-
20-
#[cfg(notest)] use cmp::{Eq, Ord};
21-
#[cfg(stage0,notest)]
22-
use ops::{Add, Sub, Mul, Div, Modulo, Neg};
23-
#[cfg(stage1,notest)]
24-
#[cfg(stage2,notest)]
25-
#[cfg(stage3,notest)]
26-
use ops::{Add, Sub, Mul, Quot, Rem, Neg};
14+
use libc::c_int;
15+
use num::strconv;
16+
use prelude::*;
2717

2818
pub use cmath::c_float_targ_consts::*;
2919

@@ -233,6 +223,8 @@ pub fn logarithm(n: f32, b: f32) -> f32 {
233223
return log2(n) / log2(b);
234224
}
235225

226+
impl Num for f32 {}
227+
236228
#[cfg(notest)]
237229
impl Eq for f32 {
238230
#[inline(always)]
@@ -588,6 +580,13 @@ impl num::FromStrRadix for f32 {
588580
#[cfg(test)]
589581
mod tests {
590582
use f32::*;
583+
use super::*;
584+
use prelude::*;
585+
586+
#[test]
587+
fn test_num() {
588+
num::test_num(10f32, 2f32);
589+
}
591590

592591
#[test]
593592
pub fn test_signed() {

src/libcore/num/f64.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,10 @@
1010

1111
//! Operations and constants for `f64`
1212
13-
use num::strconv;
14-
use num::Signed;
15-
use num;
16-
use option::Option;
17-
use to_str;
1813
use from_str;
19-
20-
#[cfg(notest)] use cmp::{Eq, Ord};
21-
#[cfg(stage0,notest)]
22-
use ops::{Add, Sub, Mul, Div, Modulo, Neg};
23-
#[cfg(stage1,notest)]
24-
#[cfg(stage2,notest)]
25-
#[cfg(stage3,notest)]
26-
use ops::{Add, Sub, Mul, Quot, Rem, Neg};
14+
use libc::c_int;
15+
use num::strconv;
16+
use prelude::*;
2717

2818
pub use cmath::c_double_targ_consts::*;
2919
pub use cmp::{min, max};
@@ -254,6 +244,8 @@ pub fn logarithm(n: f64, b: f64) -> f64 {
254244
return log2(n) / log2(b);
255245
}
256246

247+
impl Num for f64 {}
248+
257249
#[cfg(notest)]
258250
impl Eq for f64 {
259251
#[inline(always)]
@@ -596,6 +588,13 @@ impl num::FromStrRadix for f64 {
596588
#[cfg(test)]
597589
mod tests {
598590
use f64::*;
591+
use super::*;
592+
use prelude::*;
593+
594+
#[test]
595+
fn test_num() {
596+
num::test_num(10f64, 2f64);
597+
}
599598

600599
#[test]
601600
pub fn test_signed() {

src/libcore/num/float.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,10 @@
2020

2121
// PORT this must match in width according to architecture
2222

23-
use f64;
24-
use num::strconv;
25-
use num::Signed;
26-
use num;
27-
use option::Option;
28-
use to_str;
2923
use from_str;
30-
31-
#[cfg(notest)] use cmp::{Eq, Ord};
32-
#[cfg(stage0,notest)]
33-
use ops::{Add, Sub, Mul, Div, Modulo, Neg};
34-
#[cfg(stage1,notest)]
35-
#[cfg(stage2,notest)]
36-
#[cfg(stage3,notest)]
37-
use ops::{Add, Sub, Mul, Quot, Rem, Neg};
24+
use libc::c_int;
25+
use num::strconv;
26+
use prelude::*;
3827

3928
pub use f64::{add, sub, mul, quot, rem, lt, le, eq, ne, ge, gt};
4029
pub use f64::logarithm;
@@ -382,6 +371,8 @@ pub fn tan(x: float) -> float {
382371
f64::tan(x as f64) as float
383372
}
384373
374+
impl Num for float {}
375+
385376
#[cfg(notest)]
386377
impl Eq for float {
387378
#[inline(always)]
@@ -524,6 +515,11 @@ mod tests {
524515
use super::*;
525516
use prelude::*;
526517
518+
#[test]
519+
fn test_num() {
520+
num::test_num(10f, 2f);
521+
}
522+
527523
#[test]
528524
pub fn test_signed() {
529525
assert_eq!(infinity.abs(), infinity);

src/libcore/num/int-template.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@
1010

1111
use T = self::inst::T;
1212

13-
use to_str::ToStr;
1413
use from_str::FromStr;
1514
use num::{ToStrRadix, FromStrRadix};
1615
use num::strconv;
17-
use num::Signed;
18-
use num;
1916
use prelude::*;
2017

2118
pub use cmp::{min, max};
@@ -133,6 +130,8 @@ pub fn compl(i: T) -> T {
133130
#[inline(always)]
134131
pub fn abs(i: T) -> T { i.abs() }
135132
133+
impl Num for T {}
134+
136135
#[cfg(notest)]
137136
impl Ord for T {
138137
#[inline(always)]
@@ -522,6 +521,11 @@ mod tests {
522521
use super::inst::T;
523522
use prelude::*;
524523
524+
#[test]
525+
fn test_num() {
526+
num::test_num(10 as T, 2 as T);
527+
}
528+
525529
#[test]
526530
pub fn test_signed() {
527531
assert_eq!((1 as T).abs(), 1 as T);

src/libcore/num/num.rs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,18 @@ pub trait Num: Eq + Zero + One
3333
+ Quot<Self,Self>
3434
+ Rem<Self,Self> {}
3535

36-
impl Num for u8 {}
37-
impl Num for u16 {}
38-
impl Num for u32 {}
39-
impl Num for u64 {}
40-
impl Num for uint {}
41-
impl Num for i8 {}
42-
impl Num for i16 {}
43-
impl Num for i32 {}
44-
impl Num for i64 {}
45-
impl Num for int {}
46-
impl Num for f32 {}
47-
impl Num for f64 {}
48-
impl Num for float {}
49-
5036
pub trait IntConvertible {
5137
fn to_int(&self) -> int;
5238
fn from_int(n: int) -> Self;
5339
}
5440

5541
pub trait Zero {
42+
// FIXME (#5527): These should be associated constants
5643
fn zero() -> Self;
5744
}
5845

5946
pub trait One {
47+
// FIXME (#5527): These should be associated constants
6048
fn one() -> Self;
6149
}
6250

@@ -230,8 +218,9 @@ pub fn pow_with_uint<T:NumCast+One+Zero+Copy+Quot<T,T>+Mul<T,T>>(
230218
total
231219
}
232220

221+
/// Helper function for testing numeric operations
233222
#[cfg(stage0,test)]
234-
fn test_num<T:Num + NumCast>(ten: T, two: T) {
223+
pub fn test_num<T:Num + NumCast>(ten: T, two: T) {
235224
assert_eq!(ten.add(&two), cast(12));
236225
assert_eq!(ten.sub(&two), cast(8));
237226
assert_eq!(ten.mul(&two), cast(20));
@@ -247,7 +236,7 @@ fn test_num<T:Num + NumCast>(ten: T, two: T) {
247236
#[cfg(stage1,test)]
248237
#[cfg(stage2,test)]
249238
#[cfg(stage3,test)]
250-
fn test_num<T:Num + NumCast>(ten: T, two: T) {
239+
pub fn test_num<T:Num + NumCast>(ten: T, two: T) {
251240
assert_eq!(ten.add(&two), cast(12));
252241
assert_eq!(ten.sub(&two), cast(8));
253242
assert_eq!(ten.mul(&two), cast(20));
@@ -261,20 +250,6 @@ fn test_num<T:Num + NumCast>(ten: T, two: T) {
261250
assert_eq!(ten.rem(&two), ten % two);
262251
}
263252

264-
#[test] fn test_u8_num() { test_num(10u8, 2u8) }
265-
#[test] fn test_u16_num() { test_num(10u16, 2u16) }
266-
#[test] fn test_u32_num() { test_num(10u32, 2u32) }
267-
#[test] fn test_u64_num() { test_num(10u64, 2u64) }
268-
#[test] fn test_uint_num() { test_num(10u, 2u) }
269-
#[test] fn test_i8_num() { test_num(10i8, 2i8) }
270-
#[test] fn test_i16_num() { test_num(10i16, 2i16) }
271-
#[test] fn test_i32_num() { test_num(10i32, 2i32) }
272-
#[test] fn test_i64_num() { test_num(10i64, 2i64) }
273-
#[test] fn test_int_num() { test_num(10i, 2i) }
274-
#[test] fn test_f32_num() { test_num(10f32, 2f32) }
275-
#[test] fn test_f64_num() { test_num(10f64, 2f64) }
276-
#[test] fn test_float_num() { test_num(10f, 2f) }
277-
278253
macro_rules! test_cast_20(
279254
($_20:expr) => ({
280255
let _20 = $_20;

src/libcore/num/uint-template.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@
1111
use T = self::inst::T;
1212
use T_SIGNED = self::inst::T_SIGNED;
1313

14-
use to_str::ToStr;
1514
use from_str::FromStr;
1615
use num::{ToStrRadix, FromStrRadix};
1716
use num::strconv;
18-
use num::Unsigned;
19-
use num;
20-
use option::Option;
2117
use prelude::*;
2218

2319
pub use cmp::{min, max};
@@ -100,6 +96,8 @@ pub fn compl(i: T) -> T {
10096
max_value ^ i
10197
}
10298
99+
impl Num for T {}
100+
103101
#[cfg(notest)]
104102
impl Ord for T {
105103
#[inline(always)]
@@ -356,6 +354,11 @@ mod tests {
356354
use super::inst::T;
357355
use prelude::*;
358356
357+
#[test]
358+
fn test_num() {
359+
num::test_num(10 as T, 2 as T);
360+
}
361+
359362
#[test]
360363
fn test_gcd() {
361364
assert_eq!((10 as T).gcd(2), 2 as T);

0 commit comments

Comments
 (0)