stdlib: Implement ivec::unsafe::set_len

This commit is contained in:
Patrick Walton 2011-07-11 17:26:34 -07:00
parent d21228fce7
commit 3275cad6d5
3 changed files with 22 additions and 5 deletions

View File

@ -63,7 +63,7 @@ obj FILE_buf_reader(os::libc::FILE f, bool must_close) {
auto buf = ~[];
ivec::reserve[u8](buf, len);
auto read = os::libc_ivec::fread(ivec::to_ptr[u8](buf), 1u, len, f);
ivec::len_set[u8](buf, read);
ivec::unsafe::set_len[u8](buf, read);
ret buf;
}
fn read_byte() -> int { ret os::libc::fgetc(f); }

View File

@ -3,6 +3,7 @@
import option::none;
import option::some;
import uint::next_power_of_two;
import ptr::addr_of;
type operator2[T,U,V] = fn(&T, &U) -> V;
@ -35,10 +36,6 @@ fn len[T](&T[mutable?] v) -> uint {
ret rusti::ivec_len(v);
}
fn len_set[T](&mutable T[mutable?] v, uint new_len) {
v = slice(v, 0u, new_len);
}
type init_op[T] = fn(uint) -> T;
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] {
}
mod unsafe {
type ivec_repr = rec(mutable uint fill,
mutable uint alloc,
*mutable ivec_heap_part heap_part);
type ivec_heap_part = rec(mutable uint fill);
fn copy_from_buf[T](&mutable T[] v, *T ptr, uint count) {
ret rustrt::ivec_copy_from_buf_shared(v, ptr, count);
}
@ -226,5 +228,16 @@ mod unsafe {
copy_from_buf(v, ptr, bytes);
ret v;
}
fn set_len[T](&mutable T[] v, uint new_len) {
auto new_fill = new_len * sys::size_of[T]();
let *mutable ivec_repr stack_part =
::unsafe::reinterpret_cast(addr_of(v));
if ((*stack_part).fill == 0u) {
(*(*stack_part).heap_part).fill = new_fill; // On heap.
} else {
(*stack_part).fill = new_fill; // On stack.
}
}
}

View File

@ -1,5 +1,8 @@
import rustrt::size_of;
export rustrt;
export size_of;
native "rust" mod rustrt {
@ -13,6 +16,7 @@ native "rust" mod rustrt {
fn do_gc();
fn unsupervise();
}
// Local Variables:
// mode: rust;
// fill-column: 78;