rewrite vec to be more unsafe, more inlined

This commit is contained in:
Niko Matsakis 2012-03-02 20:06:08 -08:00
parent 0416a946b7
commit 3269a4043c
4 changed files with 22 additions and 2 deletions

View File

@ -32,6 +32,7 @@ Function: offset
Calculate the offset from a pointer
*/
#[inline(always)]
fn offset<T>(ptr: *T, count: uint) -> *T {
ret rusti::ptr_offset(ptr, count);
}

View File

@ -131,6 +131,7 @@ Function: range
Iterate over the range [`lo`..`hi`)
*/
#[inline(always)]
fn range(lo: uint, hi: uint, it: fn(uint)) {
let i = lo;
while i < hi { it(i); i += 1u; }

View File

@ -78,6 +78,7 @@ Function: len
Returns the length of a vector
*/
#[inline(always)]
pure fn len<T>(v: [const T]) -> uint { unchecked { rusti::vec_len(v) } }
/*
@ -885,9 +886,17 @@ Iterates over vector `v` and, for each element, calls function `f` with the
element's value.
*/
#[inline]
#[inline(always)]
fn iter<T>(v: [const T], f: fn(T)) {
iteri(v) { |_i, v| f(v) }
unsafe {
let mut n = vec::len(v);
let mut p = unsafe::to_ptr(v);
while n > 0u {
f(*p);
p = ptr::offset(p, 1u);
n -= 1u;
}
}
}
/*
@ -896,6 +905,7 @@ Function: iter2
Iterates over two vectors in parallel
*/
#[inline]
fn iter2<U, T>(v: [ U], v2: [const T], f: fn(U, T)) {
let i = 0;
for elt in v { f(elt, v2[i]); i += 1; }
@ -909,6 +919,7 @@ Iterates over a vector's elements and indexes
Iterates over vector `v` and, for each element, calls function `f` with the
element's value and index.
*/
#[inline(always)]
fn iteri<T>(v: [const T], f: fn(uint, T)) {
let i = 0u, l = len(v);
while i < l { f(i, v[i]); i += 1u; }
@ -1001,6 +1012,7 @@ fn as_mut_buf<E,T>(v: [mutable E], f: fn(*mutable E) -> T) -> T unsafe {
}
impl vec_len<T> for [T] {
#[inline(always)]
fn len() -> uint { len(self) }
}
@ -1020,6 +1032,7 @@ mod unsafe {
ptr - An unsafe pointer to a buffer of `T`
elts - The number of elements in the buffer
*/
#[inline(always)]
unsafe fn from_buf<T>(ptr: *T, elts: uint) -> [T] {
ret rustrt::vec_from_buf_shared(sys::get_type_desc::<T>(),
ptr, elts);
@ -1034,6 +1047,7 @@ mod unsafe {
modifing its buffers, so it is up to the caller to ensure that
the vector is actually the specified size.
*/
#[inline(always)]
unsafe fn set_len<T>(&v: [const T], new_len: uint) {
let repr: **vec_repr = ::unsafe::reinterpret_cast(addr_of(v));
(**repr).fill = new_len * sys::size_of::<T>();
@ -1050,6 +1064,7 @@ mod unsafe {
Modifying the vector may cause its buffer to be reallocated, which
would also make any pointers to it invalid.
*/
#[inline(always)]
unsafe fn to_ptr<T>(v: [const T]) -> *T {
let repr: **vec_repr = ::unsafe::reinterpret_cast(addr_of(v));
ret ::unsafe::reinterpret_cast(addr_of((**repr).data));

View File

@ -72,6 +72,9 @@ fn get_item_path(tcx: ty::ctxt, def: ast::def_id) -> ast_map::path {
let cstore = tcx.sess.cstore;
let cdata = cstore::get_crate_data(cstore, def.crate);
let path = decoder::get_item_path(cdata, def.node);
// FIXME #1920: This path is not always correct if the crate is not linked
// into the root namespace.
[ast_map::path_mod(cdata.name)] + path
}