Skip to content

Commit 6bb1813

Browse files
committed
core: Factor out int/i8/16/32/64 mods into int-template
1 parent 7a2d7aa commit 6bb1813

File tree

11 files changed

+105
-226
lines changed

11 files changed

+105
-226
lines changed

src/libcore/core.rc

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,48 @@ export priv;
4444

4545
// Built-in-type support modules
4646

47+
#[doc = "Operations and constants for `int`"]
48+
#[path = "int-template"]
49+
mod int {
50+
import inst::{ hash, parse_buf, from_str, to_str, str, pow };
51+
export hash, parse_buf, from_str, to_str, str, pow;
52+
#[path = "int.rs"]
53+
mod inst;
54+
}
55+
56+
#[doc = "Operations and constants for `i8`"]
57+
#[path = "int-template"]
58+
mod i8 {
59+
#[path = "i8.rs"]
60+
mod inst;
61+
}
62+
63+
#[doc = "Operations and constants for `i16`"]
64+
#[path = "int-template"]
65+
mod i16 {
66+
#[path = "i16.rs"]
67+
mod inst;
68+
}
69+
70+
#[doc = "Operations and constants for `i32`"]
71+
#[path = "int-template"]
72+
mod i32 {
73+
#[path = "i32.rs"]
74+
mod inst;
75+
}
76+
77+
#[doc = "Operations and constants for `i64`"]
78+
#[path = "int-template"]
79+
mod i64 {
80+
#[path = "i64.rs"]
81+
mod inst;
82+
}
83+
4784
mod box;
4885
mod char;
4986
mod float;
5087
mod f32;
5188
mod f64;
52-
mod int;
53-
mod i8;
54-
mod i16;
55-
mod i32;
56-
mod i64;
5789
mod str;
5890
mod ptr;
5991
mod uint;

src/libcore/i16.rs

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/libcore/i32.rs

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/libcore/i64.rs

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/libcore/i8.rs

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/libcore/int-template.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import T = inst::T;
2+
3+
export min_value, max_value;
4+
export min, max;
5+
export add, sub, mul, div, rem;
6+
export lt, le, eq, ne, ge, gt;
7+
export is_positive, is_negative;
8+
export is_nonpositive, is_nonnegative;
9+
export range;
10+
export compl;
11+
export abs;
12+
13+
const min_value: T = -1 as T << (inst::bits - 1 as T);
14+
const max_value: T = min_value - 1 as T;
15+
16+
pure fn min(x: T, y: T) -> T { if x < y { x } else { y } }
17+
pure fn max(x: T, y: T) -> T { if x > y { x } else { y } }
18+
19+
pure fn add(x: T, y: T) -> T { x + y }
20+
pure fn sub(x: T, y: T) -> T { x - y }
21+
pure fn mul(x: T, y: T) -> T { x * y }
22+
pure fn div(x: T, y: T) -> T { x / y }
23+
pure fn rem(x: T, y: T) -> T { x % y }
24+
25+
pure fn lt(x: T, y: T) -> bool { x < y }
26+
pure fn le(x: T, y: T) -> bool { x <= y }
27+
pure fn eq(x: T, y: T) -> bool { x == y }
28+
pure fn ne(x: T, y: T) -> bool { x != y }
29+
pure fn ge(x: T, y: T) -> bool { x >= y }
30+
pure fn gt(x: T, y: T) -> bool { x > y }
31+
32+
pure fn is_positive(x: T) -> bool { x > 0 as T }
33+
pure fn is_negative(x: T) -> bool { x < 0 as T }
34+
pure fn is_nonpositive(x: T) -> bool { x <= 0 as T }
35+
pure fn is_nonnegative(x: T) -> bool { x >= 0 as T }
36+
37+
#[doc = "Iterate over the range [`lo`..`hi`)"]
38+
fn range(lo: T, hi: T, it: fn(T)) {
39+
let mut i = lo;
40+
while i < hi { it(i); i += 1 as T; }
41+
}
42+
43+
#[doc = "Computes the bitwise complement"]
44+
pure fn compl(i: T) -> T {
45+
-1 as T ^ i
46+
}
47+
48+
#[doc = "Computes the absolute value"]
49+
pure fn abs(i: T) -> T {
50+
if is_negative(i) { -i } else { i }
51+
}

src/libcore/int-template/i16.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type T = i16;
2+
3+
const bits: T = 16 as T;

src/libcore/int-template/i32.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type T = i32;
2+
3+
const bits: T = 32 as T;

src/libcore/int-template/i64.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type T = i64;
2+
3+
const bits: T = 64 as T;

src/libcore/int-template/i8.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type T = i8;
2+
3+
const bits: T = 8 as T;

src/libcore/int.rs renamed to src/libcore/int-template/int.rs

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,14 @@
1-
#[doc = "Operations and constants for `int`"];
1+
type T = int;
22

3-
#[cfg(target_arch="x86")]
4-
const min_value: int = -1 << 31;
3+
#[cfg(target_arch = "x86")]
4+
const bits: T = 32 as T;
55

6-
#[cfg(target_arch="x86_64")]
7-
const min_value: int = -1 << 63;
8-
9-
// FIXME: Find another way to access the machine word size in a const expr
10-
// (See Issue #2001)
11-
#[cfg(target_arch="x86")]
12-
const max_value: int = (-1 << 31)-1;
13-
14-
#[cfg(target_arch="x86_64")]
15-
const max_value: int = (-1 << 63)-1;
16-
17-
pure fn min(x: int, y: int) -> int { if x < y { x } else { y } }
18-
pure fn max(x: int, y: int) -> int { if x > y { x } else { y } }
19-
20-
pure fn add(x: int, y: int) -> int { ret x + y; }
21-
pure fn sub(x: int, y: int) -> int { ret x - y; }
22-
pure fn mul(x: int, y: int) -> int { ret x * y; }
23-
pure fn div(x: int, y: int) -> int { ret x / y; }
24-
pure fn rem(x: int, y: int) -> int { ret x % y; }
25-
26-
pure fn lt(x: int, y: int) -> bool { ret x < y; }
27-
pure fn le(x: int, y: int) -> bool { ret x <= y; }
28-
pure fn eq(x: int, y: int) -> bool { ret x == y; }
29-
pure fn ne(x: int, y: int) -> bool { ret x != y; }
30-
pure fn ge(x: int, y: int) -> bool { ret x >= y; }
31-
pure fn gt(x: int, y: int) -> bool { ret x > y; }
32-
33-
pure fn is_positive(x: int) -> bool { ret x > 0; }
34-
pure fn is_negative(x: int) -> bool { ret x < 0; }
35-
pure fn is_nonpositive(x: int) -> bool { ret x <= 0; }
36-
pure fn is_nonnegative(x: int) -> bool { ret x >= 0; }
6+
#[cfg(target_arch = "x86_64")]
7+
const bits: T = 64 as T;
378

389
#[doc = "Produce a uint suitable for use in a hash table"]
3910
pure fn hash(x: int) -> uint { ret x as uint; }
4011

41-
#[doc = "Iterate over the range `[lo..hi)`"]
42-
fn range(lo: int, hi: int, it: fn(int)) {
43-
let mut i = lo;
44-
while i < hi { it(i); i += 1; }
45-
}
46-
4712
#[doc = "
4813
Parse a buffer of bytes
4914
@@ -105,15 +70,6 @@ fn pow(base: int, exponent: uint) -> int {
10570
ret acc;
10671
}
10772

108-
#[doc = "Computes the bitwise complement"]
109-
pure fn compl(i: int) -> int {
110-
uint::compl(i as uint) as int
111-
}
112-
113-
#[doc = "Computes the absolute value"]
114-
fn abs(i: int) -> int {
115-
if is_negative(i) { -i } else { i }
116-
}
11773

11874
#[test]
11975
fn test_from_str() {
@@ -186,11 +142,3 @@ fn test_overflows() {
186142
assert (min_value <= 0);
187143
assert (min_value + max_value + 1 == 0);
188144
}
189-
190-
// Local Variables:
191-
// mode: rust;
192-
// fill-column: 78;
193-
// indent-tabs-mode: nil
194-
// c-basic-offset: 4
195-
// buffer-file-coding-system: utf-8-unix
196-
// End:

0 commit comments

Comments
 (0)