Skip to content

Commit 3275cad

Browse files
committed
stdlib: Implement ivec::unsafe::set_len
1 parent d21228f commit 3275cad

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/lib/ioivec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ obj FILE_buf_reader(os::libc::FILE f, bool must_close) {
6363
auto buf = ~[];
6464
ivec::reserve[u8](buf, len);
6565
auto read = os::libc_ivec::fread(ivec::to_ptr[u8](buf), 1u, len, f);
66-
ivec::len_set[u8](buf, read);
66+
ivec::unsafe::set_len[u8](buf, read);
6767
ret buf;
6868
}
6969
fn read_byte() -> int { ret os::libc::fgetc(f); }

src/lib/ivec.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import option::none;
44
import option::some;
55
import uint::next_power_of_two;
6+
import ptr::addr_of;
67

78
type operator2[T,U,V] = fn(&T, &U) -> V;
89

@@ -35,10 +36,6 @@ fn len[T](&T[mutable?] v) -> uint {
3536
ret rusti::ivec_len(v);
3637
}
3738

38-
fn len_set[T](&mutable T[mutable?] v, uint new_len) {
39-
v = slice(v, 0u, new_len);
40-
}
41-
4239
type init_op[T] = fn(uint) -> T;
4340

4441
fn init_fn[T](&init_op[T] op, uint n_elts) -> T[] {
@@ -217,6 +214,11 @@ fn find[T](fn(&T) -> bool f, &T[] v) -> option::t[T] {
217214
}
218215

219216
mod unsafe {
217+
type ivec_repr = rec(mutable uint fill,
218+
mutable uint alloc,
219+
*mutable ivec_heap_part heap_part);
220+
type ivec_heap_part = rec(mutable uint fill);
221+
220222
fn copy_from_buf[T](&mutable T[] v, *T ptr, uint count) {
221223
ret rustrt::ivec_copy_from_buf_shared(v, ptr, count);
222224
}
@@ -226,5 +228,16 @@ mod unsafe {
226228
copy_from_buf(v, ptr, bytes);
227229
ret v;
228230
}
231+
232+
fn set_len[T](&mutable T[] v, uint new_len) {
233+
auto new_fill = new_len * sys::size_of[T]();
234+
let *mutable ivec_repr stack_part =
235+
::unsafe::reinterpret_cast(addr_of(v));
236+
if ((*stack_part).fill == 0u) {
237+
(*(*stack_part).heap_part).fill = new_fill; // On heap.
238+
} else {
239+
(*stack_part).fill = new_fill; // On stack.
240+
}
241+
}
229242
}
230243

src/lib/sys.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11

2+
import rustrt::size_of;
3+
24
export rustrt;
5+
export size_of;
36

47
native "rust" mod rustrt {
58

@@ -13,6 +16,7 @@ native "rust" mod rustrt {
1316
fn do_gc();
1417
fn unsupervise();
1518
}
19+
1620
// Local Variables:
1721
// mode: rust;
1822
// fill-column: 78;

0 commit comments

Comments
 (0)