Skip to content

Commit

Permalink
rustc: Replace intrinsic vec_len with unsafe Rust code
Browse files Browse the repository at this point in the history
Preparation for #1981
  • Loading branch information
marijnh committed Mar 21, 2012
1 parent cce2751 commit bc3f5e7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
7 changes: 3 additions & 4 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export memmove;
#[abi = "rust-intrinsic"]
native mod rusti {
fn addr_of<T>(val: T) -> *T;
fn ptr_offset<T>(ptr: *T, count: libc::uintptr_t) -> *T;
fn memcpy<T>(dst: *T, src: *T, count: libc::uintptr_t);
fn memmove<T>(dst: *T, src: *T, count: libc::uintptr_t);
}
Expand All @@ -29,14 +28,14 @@ fn mut_addr_of<T>(val: T) -> *mutable T unsafe {

#[doc = "Calculate the offset from a pointer"]
#[inline(always)]
fn offset<T>(ptr: *T, count: uint) -> *T {
ret rusti::ptr_offset(ptr, count);
fn offset<T>(ptr: *T, count: uint) -> *T unsafe {
(ptr as uint + count * sys::size_of::<T>()) as *T
}

#[doc = "Calculate the offset from a mutable pointer"]
#[inline(always)]
fn mut_offset<T>(ptr: *mutable T, count: uint) -> *mutable T {
ret rusti::ptr_offset(ptr as *T, count) as *mutable T;
(ptr as uint + count * sys::size_of::<T>()) as *mutable T
}


Expand Down
10 changes: 4 additions & 6 deletions src/libcore/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ export vec_len;
export unsafe;
export u8;

#[abi = "rust-intrinsic"]
native mod rusti {
fn vec_len<T>(&&v: [const T]) -> libc::size_t;
}

#[abi = "cdecl"]
native mod rustrt {
fn vec_reserve_shared<T>(t: *sys::type_desc,
Expand Down Expand Up @@ -122,7 +117,10 @@ fn reserve<T>(&v: [const T], n: uint) {

#[doc = "Returns the length of a vector"]
#[inline(always)]
pure fn len<T>(v: [const T]) -> uint { unchecked { rusti::vec_len(v) } }
pure fn len<T>(&&v: [const T]) -> uint unsafe {
let repr: **unsafe::vec_repr = ::unsafe::reinterpret_cast(addr_of(v));
(**repr).fill / sys::size_of::<T>()
}

#[doc = "
Creates and initializes an immutable vector.
Expand Down

0 comments on commit bc3f5e7

Please sign in to comment.