stdlib: Introduce an ivec module into the standard library; add a minimal test case

This commit is contained in:
Patrick Walton 2011-06-16 16:08:26 -07:00
parent 94cd2985b2
commit 050f62983d
5 changed files with 53 additions and 0 deletions

25
src/lib/ivec.rs Normal file
View File

@ -0,0 +1,25 @@
// Interior vector utility functions.
import option::none;
import option::some;
type operator2[T,U,V] = fn(&T, &U) -> V;
native "rust-intrinsic" mod rusti {
fn ivec_len[T](&T[] v) -> uint;
}
native "rust" mod rustrt {
fn ivec_reserve[T](&mutable T[] v, uint n);
fn ivec_on_heap[T](&T[] v) -> bool;
}
/// Reserves space for `n` elements in the given vector.
fn reserve[T](&mutable T[] v, uint n) {
rustrt::ivec_reserve(v, n);
}
fn on_heap[T](&T[] v) -> bool {
ret rustrt::ivec_on_heap(v);
}

View File

@ -12,6 +12,7 @@ mod int;
mod uint;
mod u8;
mod vec;
mod ivec;
mod str;
// General io and system-services modules.

View File

@ -606,6 +606,16 @@ ivec_reserve(rust_task *task, type_desc *ty, rust_ivec *v, size_t n_elems)
v->alloc = new_alloc;
}
/**
* Returns true if the given vector is on the heap and false if it's on the
* stack.
*/
extern "C" bool
ivec_on_heap(rust_task *task, type_desc *ty, rust_ivec *v)
{
return !v->fill && v->payload.ptr;
}
//
// Local Variables:

View File

@ -9,6 +9,7 @@ debug_trap
debug_tydesc
do_gc
get_time
ivec_on_heap
ivec_reserve
last_os_error
rand_free

View File

@ -0,0 +1,16 @@
// xfail-stage0
use std;
import std::ivec;
fn test_reserve_and_on_heap() {
let int[] v = ~[ 1, 2 ];
assert (!ivec::on_heap(v));
ivec::reserve(v, 8u);
assert (ivec::on_heap(v));
}
fn main() {
test_reserve_and_on_heap();
}