Skip to content

Commit b2bcafe

Browse files
committed
stdlib: Add some more ivec functions, untested as of yet
1 parent 5894e40 commit b2bcafe

File tree

1 file changed

+66
-3
lines changed

1 file changed

+66
-3
lines changed

src/lib/ivec.rs

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ native "rust-intrinsic" mod rusti {
1010
}
1111

1212
native "rust" mod rustrt {
13-
fn ivec_reserve[T](&mutable T[] v, uint n);
13+
fn ivec_reserve[T](&mutable T[mutable?] v, uint n);
1414
fn ivec_on_heap[T](&T[] v) -> bool;
1515
fn ivec_to_ptr[T](&T[] v) -> *T;
16-
fn ivec_copy_from_buf[T](&mutable T[] v, *T ptr, uint count);
16+
fn ivec_copy_from_buf[T](&mutable T[mutable?] v, *T ptr, uint count);
1717
}
1818

1919
/// Reserves space for `n` elements in the given vector.
20-
fn reserve[T](&mutable T[] v, uint n) {
20+
fn reserve[T](&mutable T[mutable?] v, uint n) {
2121
rustrt::ivec_reserve(v, n);
2222
}
2323

@@ -43,6 +43,69 @@ fn init_fn[T](&init_op[T] op, uint n_elts) -> T[] {
4343
ret v;
4444
}
4545

46+
// TODO: Remove me once we have slots.
47+
fn init_fn_mut[T](&init_op[T] op, uint n_elts) -> T[mutable] {
48+
auto v = ~[mutable];
49+
reserve(v, n_elts);
50+
let uint i = 0u;
51+
while (i < n_elts) { v += ~[mutable op(i)]; i += 1u; }
52+
ret v;
53+
}
54+
55+
fn init_elt[T](&T t, uint n_elts) -> T[] {
56+
auto v = ~[];
57+
reserve(v, n_elts);
58+
let uint i = 0u;
59+
while (i < n_elts) { v += ~[t]; i += 1u; }
60+
ret v;
61+
}
62+
63+
// TODO: Remove me once we have slots.
64+
fn init_elt_mut[T](&T t, uint n_elts) -> T[mutable] {
65+
auto v = ~[mutable];
66+
reserve(v, n_elts);
67+
let uint i = 0u;
68+
while (i < n_elts) { v += ~[mutable t]; i += 1u; }
69+
ret v;
70+
}
71+
72+
73+
// Accessors
74+
75+
/// Returns the last element of `v`.
76+
fn last[T](&T[mutable?] v) -> option::t[T] {
77+
if (len(v) == 0u) { ret none; }
78+
ret some(v.(len(v) - 1u));
79+
}
80+
81+
/// Returns a copy of the elements from [`start`..`end`) from `v`.
82+
fn slice[T](&T[mutable?] v, uint start, uint end) -> T[] {
83+
assert (start <= end);
84+
assert (end <= len(v));
85+
auto result = ~[];
86+
reserve(result, end - start);
87+
auto i = start;
88+
while (i < end) { result += ~[v.(i)]; i += 1u; }
89+
ret result;
90+
}
91+
92+
// TODO: Remove me once we have slots.
93+
fn slice_mut[T](&T[mutable?] v, uint start, uint end) -> T[mutable] {
94+
assert (start <= end);
95+
assert (end <= len(v));
96+
auto result = ~[mutable];
97+
reserve(result, end - start);
98+
auto i = start;
99+
while (i < end) { result += ~[mutable v.(i)]; i += 1u; }
100+
ret result;
101+
}
102+
103+
104+
// Mutators
105+
106+
// TODO
107+
108+
46109
mod unsafe {
47110
fn copy_from_buf[T](&mutable T[] v, *T ptr, uint count) {
48111
ret rustrt::ivec_copy_from_buf(v, ptr, count);

0 commit comments

Comments
 (0)