Skip to content

Commit 3d7400f

Browse files
committed
Add a Num typeclass
1 parent d542e67 commit 3d7400f

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

src/libcore/core.rc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export to_str;
4646
export swappable;
4747
export dvec, dvec_iter;
4848
export cmp;
49+
export num;
4950

5051
// NDM seems to be necessary for resolve to work
5152
export option_iter;
@@ -155,6 +156,7 @@ mod tuple;
155156
// Ubiquitous-utility-type modules
156157

157158
mod cmp;
159+
mod num;
158160
mod either;
159161
mod iter;
160162
mod logging;

src/libcore/int-template.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import T = inst::T;
22
import cmp::{eq, ord};
3+
import num::num;
34

45
export min_value, max_value;
56
export min, max;
@@ -11,7 +12,7 @@ export range;
1112
export compl;
1213
export abs;
1314
export parse_buf, from_str, to_str, to_str_bytes, str;
14-
export ord, eq;
15+
export ord, eq, num;
1516

1617
const min_value: T = -1 as T << (inst::bits - 1 as T);
1718
const max_value: T = min_value - 1 as T;
@@ -122,6 +123,17 @@ impl eq of eq for T {
122123
}
123124
}
124125

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

126138
// FIXME: Has alignment issues on windows and 32-bit linux
127139
#[test]
@@ -179,3 +191,22 @@ fn test_to_str() {
179191
assert (eq(to_str(127 as T, 16u), "7f"));
180192
assert (eq(to_str(100 as T, 10u), "100"));
181193
}
194+
195+
#[test]
196+
fn test_ifaces() {
197+
fn test<U:num>(ten: U) {
198+
assert (ten.to_int() == 10);
199+
200+
let two = ten.from_int(2);
201+
assert (two.to_int() == 2);
202+
203+
assert (ten.add(two) == ten.from_int(12));
204+
assert (ten.sub(two) == ten.from_int(8));
205+
assert (ten.mul(two) == ten.from_int(20));
206+
assert (ten.div(two) == ten.from_int(5));
207+
assert (ten.modulo(two) == ten.from_int(0));
208+
}
209+
210+
test(10 as T);
211+
}
212+

src/libcore/uint-template.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import T = inst::T;
22
import cmp::{eq, ord};
3+
import num::num;
34

45
export min_value, max_value;
56
export min, max;
@@ -11,7 +12,7 @@ export range;
1112
export compl;
1213
export to_str, to_str_bytes;
1314
export from_str, from_str_radix, str, parse_buf;
14-
export ord, eq;
15+
export ord, eq, num;
1516

1617
const min_value: T = 0 as T;
1718
const max_value: T = 0 as T - 1 as T;
@@ -63,6 +64,17 @@ impl eq of eq for T {
6364
}
6465
}
6566

67+
impl num of num for T {
68+
fn add(&&other: T) -> T { ret self + other; }
69+
fn sub(&&other: T) -> T { ret self - other; }
70+
fn mul(&&other: T) -> T { ret self * other; }
71+
fn div(&&other: T) -> T { ret self / other; }
72+
fn modulo(&&other: T) -> T { ret self % other; }
73+
74+
fn to_int() -> int { ret self as int; }
75+
fn from_int(n: int) -> T { ret n as T; }
76+
}
77+
6678
#[doc = "
6779
Parse a buffer of bytes
6880

0 commit comments

Comments
 (0)