Skip to content

Commit 903cb0e

Browse files
committed
core: Factor out uint/u8/16/32/64 mods into uint-template
1 parent 6bb1813 commit 903cb0e

File tree

11 files changed

+351
-442
lines changed

11 files changed

+351
-442
lines changed

src/libcore/core.rc

+48-5
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,61 @@ mod i64 {
8181
mod inst;
8282
}
8383

84+
#[doc = "Operations and constants for `uint`"]
85+
#[path = "uint-template"]
86+
mod uint {
87+
import inst::{
88+
div_ceil, div_round, div_floor, hash, iterate,
89+
next_power_of_two, parse_buf, from_str, to_str, str
90+
};
91+
export div_ceil, div_round, div_floor, hash, iterate,
92+
next_power_of_two, parse_buf, from_str, to_str, str;
93+
94+
#[path = "uint.rs"]
95+
mod inst;
96+
}
97+
98+
#[doc = "Operations and constants for `u8`"]
99+
#[path = "uint-template"]
100+
mod u8 {
101+
import inst::is_ascii;
102+
export is_ascii;
103+
104+
#[path = "u8.rs"]
105+
mod inst;
106+
}
107+
108+
#[doc = "Operations and constants for `u16`"]
109+
#[path = "uint-template"]
110+
mod u16 {
111+
#[path = "u16.rs"]
112+
mod inst;
113+
}
114+
115+
#[doc = "Operations and constants for `u32`"]
116+
#[path = "uint-template"]
117+
mod u32 {
118+
#[path = "u32.rs"]
119+
mod inst;
120+
}
121+
122+
#[doc = "Operations and constants for `u64`"]
123+
#[path = "uint-template"]
124+
mod u64 {
125+
import inst::{ to_str, str, from_str };
126+
export to_str, str, from_str;
127+
128+
#[path = "u64.rs"]
129+
mod inst;
130+
}
131+
84132
mod box;
85133
mod char;
86134
mod float;
87135
mod f32;
88136
mod f64;
89137
mod str;
90138
mod ptr;
91-
mod uint;
92-
mod u8;
93-
mod u16;
94-
mod u32;
95-
mod u64;
96139
mod vec;
97140
mod bool;
98141

src/libcore/u16.rs

-36
This file was deleted.

src/libcore/u32.rs

-41
This file was deleted.

src/libcore/u8.rs

-41
This file was deleted.

src/libcore/uint-template.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
12+
const min_value: T = 0 as T;
13+
const max_value: T = 0 as T - 1 as T;
14+
15+
pure fn min(x: T, y: T) -> T { if x < y { x } else { y } }
16+
pure fn max(x: T, y: T) -> T { if x > y { x } else { y } }
17+
18+
pure fn add(x: T, y: T) -> T { x + y }
19+
pure fn sub(x: T, y: T) -> T { x - y }
20+
pure fn mul(x: T, y: T) -> T { x * y }
21+
pure fn div(x: T, y: T) -> T { x / y }
22+
pure fn rem(x: T, y: T) -> T { x % y }
23+
24+
pure fn lt(x: T, y: T) -> bool { x < y }
25+
pure fn le(x: T, y: T) -> bool { x <= y }
26+
pure fn eq(x: T, y: T) -> bool { x == y }
27+
pure fn ne(x: T, y: T) -> bool { x != y }
28+
pure fn ge(x: T, y: T) -> bool { x >= y }
29+
pure fn gt(x: T, y: T) -> bool { x > y }
30+
31+
pure fn is_positive(x: T) -> bool { x > 0 as T }
32+
pure fn is_negative(x: T) -> bool { x < 0 as T }
33+
pure fn is_nonpositive(x: T) -> bool { x <= 0 as T }
34+
pure fn is_nonnegative(x: T) -> bool { x >= 0 as T }
35+
36+
#[doc = "Iterate over the range [`lo`..`hi`)"]
37+
fn range(lo: T, hi: T, it: fn(T)) {
38+
let mut i = lo;
39+
while i < hi { it(i); i += 1 as T; }
40+
}
41+
42+
#[doc = "Computes the bitwise complement"]
43+
pure fn compl(i: T) -> T {
44+
max_value ^ i
45+
}

src/libcore/uint-template/u16.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type T = u16;

src/libcore/uint-template/u32.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type T = u32;

src/libcore/u64.rs renamed to src/libcore/uint-template/u64.rs

+4-29
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
1-
#[doc = "Operations and constants for `u64`"];
1+
type T = u64;
22

3-
const min_value: u64 = 0u64;
4-
const max_value: u64 = 0u64 - 1u64;
3+
// Type-specific functions here. These must be reexported by the
4+
// parent module so that they appear in core::u8 and not core::u8::u8;
55

6-
pure fn min(x: u64, y: u64) -> u64 { if x < y { x } else { y } }
7-
pure fn max(x: u64, y: u64) -> u64 { if x > y { x } else { y } }
8-
9-
pure fn add(x: u64, y: u64) -> u64 { ret x + y; }
10-
pure fn sub(x: u64, y: u64) -> u64 { ret x - y; }
11-
pure fn mul(x: u64, y: u64) -> u64 { ret x * y; }
12-
pure fn div(x: u64, y: u64) -> u64 { ret x / y; }
13-
pure fn rem(x: u64, y: u64) -> u64 { ret x % y; }
14-
15-
pure fn lt(x: u64, y: u64) -> bool { ret x < y; }
16-
pure fn le(x: u64, y: u64) -> bool { ret x <= y; }
17-
pure fn eq(x: u64, y: u64) -> bool { ret x == y; }
18-
pure fn ne(x: u64, y: u64) -> bool { ret x != y; }
19-
pure fn ge(x: u64, y: u64) -> bool { ret x >= y; }
20-
pure fn gt(x: u64, y: u64) -> bool { ret x > y; }
21-
22-
#[doc = "Iterate over the range [`lo`..`hi`)"]
23-
fn range(lo: u64, hi: u64, it: fn(u64)) {
24-
let mut i = lo;
25-
while i < hi { it(i); i += 1u64; }
26-
}
276

7+
// FIXME: Surely we can generalize this to apply to all uint types
288
#[doc = "Convert to a string in a given base"]
299
fn to_str(n: u64, radix: uint) -> str {
3010
assert (0u < radix && radix <= 16u);
@@ -80,8 +60,3 @@ fn from_str(buf: str, radix: u64) -> option<u64> {
8060
i -= 1u;
8161
};
8262
}
83-
84-
#[doc = "Computes the bitwise complement"]
85-
pure fn compl(i: u64) -> u64 {
86-
max_value ^ i
87-
}

src/libcore/uint-template/u8.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type T = u8;
2+
3+
// Type-specific functions here. These must be reexported by the
4+
// parent module so that they appear in core::u8 and not core::u8::u8;
5+
6+
pure fn is_ascii(x: T) -> bool { ret 0 as T == x & 128 as T; }

0 commit comments

Comments
 (0)