mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-27 09:14:20 +00:00
stdlib: Introduce an ivec module into the standard library; add a minimal test case
This commit is contained in:
parent
94cd2985b2
commit
050f62983d
25
src/lib/ivec.rs
Normal file
25
src/lib/ivec.rs
Normal 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);
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ mod int;
|
||||
mod uint;
|
||||
mod u8;
|
||||
mod vec;
|
||||
mod ivec;
|
||||
mod str;
|
||||
|
||||
// General io and system-services modules.
|
||||
|
@ -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:
|
||||
|
@ -9,6 +9,7 @@ debug_trap
|
||||
debug_tydesc
|
||||
do_gc
|
||||
get_time
|
||||
ivec_on_heap
|
||||
ivec_reserve
|
||||
last_os_error
|
||||
rand_free
|
||||
|
16
src/test/run-pass/lib-ivec.rs
Normal file
16
src/test/run-pass/lib-ivec.rs
Normal 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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user