Skip to content

Commit 43a48ca

Browse files
bstriebrson
authored andcommitted
Automatically export methods on core numeric types
Each numeric type now contains an extensions module that is automatically exported. At the moment each extensions module contains only the impl for the `num::num` iface. Other impls soon to follow (hopefully).
1 parent 07d5686 commit 43a48ca

File tree

7 files changed

+104
-55
lines changed

7 files changed

+104
-55
lines changed

src/libcore/core.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,24 @@ import option_iter::extensions;
1212
import ptr::extensions;
1313
import rand::extensions;
1414
import result::extensions;
15+
import int::extensions::*;
16+
import i8::extensions::*;
17+
import i16::extensions::*;
18+
import i32::extensions::*;
19+
import i64::extensions::*;
20+
import uint::extensions::*;
21+
import u8::extensions::*;
22+
import u16::extensions::*;
23+
import u32::extensions::*;
24+
import u64::extensions::*;
25+
import float::extensions::*;
26+
import f32::extensions::*;
27+
import f64::extensions::*;
1528

1629
export path, option, some, none, unreachable;
1730
export extensions;
31+
// The following exports are the extension impls for numeric types
32+
export num;
1833

1934
// Export the log levels as global constants. Higher levels mean
2035
// more-verbosity. Error is the bottom level, default logging level is

src/libcore/f32.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export mul_add, fmax, fmin, nextafter, frexp, hypot, ldexp;
1818
export lgamma, ln, log_radix, ln1p, log10, log2, ilog_radix;
1919
export modf, pow, round, sin, sinh, sqrt, tan, tanh, tgamma, trunc;
2020
export signbit;
21-
export num;
21+
export extensions;
2222

2323
// These are not defined inside consts:: for consistency with
2424
// the integer types
@@ -173,16 +173,18 @@ pure fn log2(n: f32) -> f32 {
173173
ret ln(n) / consts::ln_2;
174174
}
175175

176-
impl num of num for f32 {
177-
fn add(&&other: f32) -> f32 { ret self + other; }
178-
fn sub(&&other: f32) -> f32 { ret self - other; }
179-
fn mul(&&other: f32) -> f32 { ret self * other; }
180-
fn div(&&other: f32) -> f32 { ret self / other; }
181-
fn modulo(&&other: f32) -> f32 { ret self % other; }
182-
fn neg() -> f32 { ret -self; }
183-
184-
fn to_int() -> int { ret self as int; }
185-
fn from_int(n: int) -> f32 { ret n as f32; }
176+
mod extensions {
177+
impl num of num for f32 {
178+
fn add(&&other: f32) -> f32 { ret self + other; }
179+
fn sub(&&other: f32) -> f32 { ret self - other; }
180+
fn mul(&&other: f32) -> f32 { ret self * other; }
181+
fn div(&&other: f32) -> f32 { ret self / other; }
182+
fn modulo(&&other: f32) -> f32 { ret self % other; }
183+
fn neg() -> f32 { ret -self; }
184+
185+
fn to_int() -> int { ret self as int; }
186+
fn from_int(n: int) -> f32 { ret n as f32; }
187+
}
186188
}
187189

188190
//

src/libcore/f64.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export lgamma, ln, log_radix, ln1p, log10, log2, ilog_radix;
2222
export modf, pow, round, sin, sinh, sqrt, tan, tanh, tgamma, trunc;
2323
export signbit;
2424
export epsilon;
25-
export num;
25+
export extensions;
2626

2727
// These are not defined inside consts:: for consistency with
2828
// the integer types
@@ -196,16 +196,18 @@ pure fn log2(n: f64) -> f64 {
196196
ret ln(n) / consts::ln_2;
197197
}
198198

199-
impl num of num for f64 {
200-
fn add(&&other: f64) -> f64 { ret self + other; }
201-
fn sub(&&other: f64) -> f64 { ret self - other; }
202-
fn mul(&&other: f64) -> f64 { ret self * other; }
203-
fn div(&&other: f64) -> f64 { ret self / other; }
204-
fn modulo(&&other: f64) -> f64 { ret self % other; }
205-
fn neg() -> f64 { ret -self; }
206-
207-
fn to_int() -> int { ret self as int; }
208-
fn from_int(n: int) -> f64 { ret n as f64; }
199+
mod extensions {
200+
impl num of num for f64 {
201+
fn add(&&other: f64) -> f64 { ret self + other; }
202+
fn sub(&&other: f64) -> f64 { ret self - other; }
203+
fn mul(&&other: f64) -> f64 { ret self * other; }
204+
fn div(&&other: f64) -> f64 { ret self / other; }
205+
fn modulo(&&other: f64) -> f64 { ret self % other; }
206+
fn neg() -> f64 { ret -self; }
207+
208+
fn to_int() -> int { ret self as int; }
209+
fn from_int(n: int) -> f64 { ret n as f64; }
210+
}
209211
}
210212

211213
//

src/libcore/float.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export lgamma, ln, log_radix, ln1p, log10, log2, ilog_radix;
1717
export modf, pow, round, sin, sinh, sqrt, tan, tanh, tgamma, trunc;
1818
export signbit;
1919
export pow_with_uint;
20-
export num;
20+
export extensions;
2121

2222
// export when m_float == c_double
2323

@@ -410,16 +410,18 @@ fn sin(x: float) -> float { f64::sin(x as f64) as float }
410410
fn cos(x: float) -> float { f64::cos(x as f64) as float }
411411
fn tan(x: float) -> float { f64::tan(x as f64) as float }
412412

413-
impl num of num for float {
414-
fn add(&&other: float) -> float { ret self + other; }
415-
fn sub(&&other: float) -> float { ret self - other; }
416-
fn mul(&&other: float) -> float { ret self * other; }
417-
fn div(&&other: float) -> float { ret self / other; }
418-
fn modulo(&&other: float) -> float { ret self % other; }
419-
fn neg() -> float { ret -self; }
420-
421-
fn to_int() -> int { ret self as int; }
422-
fn from_int(n: int) -> float { ret n as float; }
413+
mod extensions {
414+
impl num of num for float {
415+
fn add(&&other: float) -> float { ret self + other; }
416+
fn sub(&&other: float) -> float { ret self - other; }
417+
fn mul(&&other: float) -> float { ret self * other; }
418+
fn div(&&other: float) -> float { ret self / other; }
419+
fn modulo(&&other: float) -> float { ret self % other; }
420+
fn neg() -> float { ret -self; }
421+
422+
fn to_int() -> int { ret self as int; }
423+
fn from_int(n: int) -> float { ret n as float; }
424+
}
423425
}
424426

425427
#[test]

src/libcore/int-template.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export range;
1212
export compl;
1313
export abs;
1414
export parse_buf, from_str, to_str, to_str_bytes, str;
15-
export ord, eq, num;
15+
export ord, eq, extensions;
1616

1717
const min_value: T = -1 as T << (inst::bits - 1 as T);
1818
const max_value: T = min_value - 1 as T;
@@ -124,16 +124,18 @@ impl eq of eq for T {
124124
}
125125
}
126126

127-
impl num of num::num for T {
128-
fn add(&&other: T) -> T { ret self + other; }
129-
fn sub(&&other: T) -> T { ret self - other; }
130-
fn mul(&&other: T) -> T { ret self * other; }
131-
fn div(&&other: T) -> T { ret self / other; }
132-
fn modulo(&&other: T) -> T { ret self % other; }
133-
fn neg() -> T { ret -self; }
134-
135-
fn to_int() -> int { ret self as int; }
136-
fn from_int(n: int) -> T { ret n as T; }
127+
mod extensions {
128+
impl num of num::num for T {
129+
fn add(&&other: T) -> T { ret self + other; }
130+
fn sub(&&other: T) -> T { ret self - other; }
131+
fn mul(&&other: T) -> T { ret self * other; }
132+
fn div(&&other: T) -> T { ret self / other; }
133+
fn modulo(&&other: T) -> T { ret self % other; }
134+
fn neg() -> T { ret -self; }
135+
136+
fn to_int() -> int { ret self as int; }
137+
fn from_int(n: int) -> T { ret n as T; }
138+
}
137139
}
138140

139141

src/libcore/uint-template.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export range;
1212
export compl;
1313
export to_str, to_str_bytes;
1414
export from_str, from_str_radix, str, parse_buf;
15-
export ord, eq, num;
15+
export ord, eq, extensions;
1616

1717
const min_value: T = 0 as T;
1818
const max_value: T = 0 as T - 1 as T;
@@ -65,16 +65,18 @@ impl eq of eq for T {
6565
}
6666
}
6767

68-
impl num of num::num for T {
69-
fn add(&&other: T) -> T { ret self + other; }
70-
fn sub(&&other: T) -> T { ret self - other; }
71-
fn mul(&&other: T) -> T { ret self * other; }
72-
fn div(&&other: T) -> T { ret self / other; }
73-
fn modulo(&&other: T) -> T { ret self % other; }
74-
fn neg() -> T { ret -self; }
75-
76-
fn to_int() -> int { ret self as int; }
77-
fn from_int(n: int) -> T { ret n as T; }
68+
mod extensions {
69+
impl num of num::num for T {
70+
fn add(&&other: T) -> T { ret self + other; }
71+
fn sub(&&other: T) -> T { ret self - other; }
72+
fn mul(&&other: T) -> T { ret self * other; }
73+
fn div(&&other: T) -> T { ret self / other; }
74+
fn modulo(&&other: T) -> T { ret self % other; }
75+
fn neg() -> T { ret -self; }
76+
77+
fn to_int() -> int { ret self as int; }
78+
fn from_int(n: int) -> T { ret n as T; }
79+
}
7880
}
7981

8082
#[doc = "
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// This file is intended to test only that methods are automatically
2+
// reachable for each numeric type, for each exported impl, with no imports
3+
// necessary. Testing the methods of the impls is done within the source
4+
// file for each numeric type.
5+
fn main() {
6+
// extensions::num
7+
assert 15.add(6) == 21;
8+
assert 15i8.add(6i8) == 21i8;
9+
assert 15i16.add(6i16) == 21i16;
10+
assert 15i32.add(6i32) == 21i32;
11+
assert 15i64.add(6i64) == 21i64;
12+
13+
// extensions::num
14+
assert 15u.add(6u) == 21u;
15+
assert 15u8.add(6u8) == 21u8;
16+
assert 15u16.add(6u16) == 21u16;
17+
assert 15u32.add(6u32) == 21u32;
18+
assert 15u64.add(6u64) == 21u64;
19+
20+
// extensions::num
21+
assert 10f.to_int() == 10;
22+
assert 10f32.to_int() == 10;
23+
assert 10f64.to_int() == 10;
24+
}

0 commit comments

Comments
 (0)