Skip to content

Commit f360d22

Browse files
committed
Uint-ify various bits of _str and _vec, enrich _vec a bit.
1 parent fdf2eaa commit f360d22

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

src/lib/_str.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ native "rust" mod rustrt {
44
type sbuf;
55
fn str_buf(str s) -> sbuf;
66
fn str_len(str s) -> uint;
7-
fn str_alloc(int n_bytes) -> str;
7+
fn str_alloc(uint n_bytes) -> str;
88
}
99

1010
fn is_utf8(vec[u8] v) -> bool {
1111
}
1212

13-
fn alloc(int n_bytes) -> str {
13+
fn alloc(uint n_bytes) -> str {
1414
ret rustrt.str_alloc(n_bytes);
1515
}
1616

src/lib/_vec.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
11
import vbuf = rustrt.vbuf;
2+
import op = util.operator;
23

34
native "rust" mod rustrt {
45
type vbuf;
56
fn vec_buf[T](vec[T] v) -> vbuf;
67
fn vec_len[T](vec[T] v) -> uint;
7-
fn vec_alloc[T](int n_elts) -> vec[T];
8+
fn vec_alloc[T](uint n_elts) -> vec[T];
89
}
910

10-
fn alloc[T](int n_elts) -> vec[T] {
11+
fn alloc[T](uint n_elts) -> vec[T] {
1112
ret rustrt.vec_alloc[T](n_elts);
1213
}
1314

14-
fn init[T](&T t, int n_elts) -> vec[T] {
15+
type init_op[T] = fn(uint i) -> T;
16+
17+
fn init_fn[T](&init_op[T] op, uint n_elts) -> vec[T] {
1518
let vec[T] v = alloc[T](n_elts);
16-
let int i = n_elts;
17-
while (i > 0) {
18-
i -= 1;
19+
let uint i = n_elts;
20+
while (i > uint(0)) {
21+
i -= uint(1);
22+
v += vec(op(i));
23+
}
24+
ret v;
25+
}
26+
27+
fn init_elt[T](&T t, uint n_elts) -> vec[T] {
28+
// FIXME: should be:
29+
// fn elt_op[X](X x, uint i) -> X { ret x; }
30+
// auto inner = bind elt_op[T](t, _);
31+
// ret init_fn[T](inner, n_elts);
32+
// but this does not work presently.
33+
let vec[T] v = alloc[T](n_elts);
34+
let uint i = n_elts;
35+
while (i > uint(0)) {
36+
i -= uint(1);
1937
v += vec(t);
2038
}
2139
ret v;
@@ -39,3 +57,15 @@ fn grow[T](mutable vec[T] v, int n, T initval) -> vec[T] {
3957
}
4058
ret v;
4159
}
60+
61+
fn map[T,U](&op[T,U] f, &vec[T] v) -> vec[U] {
62+
// FIXME: should be
63+
// let vec[U] u = alloc[U](len[T](v));
64+
// but this does not work presently.
65+
let vec[U] u = vec();
66+
for (T ve in v) {
67+
u += vec(f[T,U](ve));
68+
}
69+
ret u;
70+
}
71+

src/test/run-pass/user.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use std (name = "std",
55
uuid = _, ver = _);
66

77
fn main() {
8-
auto s = std._str.alloc(10);
8+
auto s = std._str.alloc(uint(10));
99
s += "hello ";
1010
log s;
1111
s += "there";
1212
log s;
13-
auto z = std._vec.alloc[int](10);
13+
auto z = std._vec.alloc[int](uint(10));
1414
}

0 commit comments

Comments
 (0)