stdlib: Implement ivec::unsafe::set_len
This commit is contained in:
@@ -63,7 +63,7 @@ obj FILE_buf_reader(os::libc::FILE f, bool must_close) {
|
|||||||
auto buf = ~[];
|
auto buf = ~[];
|
||||||
ivec::reserve[u8](buf, len);
|
ivec::reserve[u8](buf, len);
|
||||||
auto read = os::libc_ivec::fread(ivec::to_ptr[u8](buf), 1u, len, f);
|
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;
|
ret buf;
|
||||||
}
|
}
|
||||||
fn read_byte() -> int { ret os::libc::fgetc(f); }
|
fn read_byte() -> int { ret os::libc::fgetc(f); }
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
import option::none;
|
import option::none;
|
||||||
import option::some;
|
import option::some;
|
||||||
import uint::next_power_of_two;
|
import uint::next_power_of_two;
|
||||||
|
import ptr::addr_of;
|
||||||
|
|
||||||
type operator2[T,U,V] = fn(&T, &U) -> V;
|
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);
|
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;
|
type init_op[T] = fn(uint) -> T;
|
||||||
|
|
||||||
fn init_fn[T](&init_op[T] op, uint n_elts) -> 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 {
|
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) {
|
fn copy_from_buf[T](&mutable T[] v, *T ptr, uint count) {
|
||||||
ret rustrt::ivec_copy_from_buf_shared(v, ptr, count);
|
ret rustrt::ivec_copy_from_buf_shared(v, ptr, count);
|
||||||
}
|
}
|
||||||
@@ -226,5 +228,16 @@ mod unsafe {
|
|||||||
copy_from_buf(v, ptr, bytes);
|
copy_from_buf(v, ptr, bytes);
|
||||||
ret v;
|
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.
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
|
|
||||||
|
import rustrt::size_of;
|
||||||
|
|
||||||
export rustrt;
|
export rustrt;
|
||||||
|
export size_of;
|
||||||
|
|
||||||
native "rust" mod rustrt {
|
native "rust" mod rustrt {
|
||||||
|
|
||||||
@@ -13,6 +16,7 @@ native "rust" mod rustrt {
|
|||||||
fn do_gc();
|
fn do_gc();
|
||||||
fn unsupervise();
|
fn unsupervise();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Local Variables:
|
// Local Variables:
|
||||||
// mode: rust;
|
// mode: rust;
|
||||||
// fill-column: 78;
|
// fill-column: 78;
|
||||||
|
|||||||
Reference in New Issue
Block a user