提交 bc3f5e71 编写于 作者: M Marijn Haverbeke

rustc: Replace intrinsic vec_len with unsafe Rust code

Preparation for #1981
上级 cce27514
......@@ -12,7 +12,6 @@
#[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);
}
......@@ -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
}
......
......@@ -74,11 +74,6 @@
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,
......@@ -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.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册