From bc3f5e716039b8f41f31f30fe3769390a298e78d Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Wed, 21 Mar 2012 14:55:36 +0100 Subject: [PATCH] rustc: Replace intrinsic vec_len with unsafe Rust code Preparation for #1981 --- src/libcore/ptr.rs | 7 +++---- src/libcore/vec.rs | 10 ++++------ 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 72552d318d297..750dbdd3e4e80 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -12,7 +12,6 @@ export memmove; #[abi = "rust-intrinsic"] native mod rusti { fn addr_of(val: T) -> *T; - fn ptr_offset(ptr: *T, count: libc::uintptr_t) -> *T; fn memcpy(dst: *T, src: *T, count: libc::uintptr_t); fn memmove(dst: *T, src: *T, count: libc::uintptr_t); } @@ -29,14 +28,14 @@ fn mut_addr_of(val: T) -> *mutable T unsafe { #[doc = "Calculate the offset from a pointer"] #[inline(always)] -fn offset(ptr: *T, count: uint) -> *T { - ret rusti::ptr_offset(ptr, count); +fn offset(ptr: *T, count: uint) -> *T unsafe { + (ptr as uint + count * sys::size_of::()) as *T } #[doc = "Calculate the offset from a mutable pointer"] #[inline(always)] fn mut_offset(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::()) as *mutable T } diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index 3950aa817072c..8fcc4b6b821e8 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -74,11 +74,6 @@ export vec_len; export unsafe; export u8; -#[abi = "rust-intrinsic"] -native mod rusti { - fn vec_len(&&v: [const T]) -> libc::size_t; -} - #[abi = "cdecl"] native mod rustrt { fn vec_reserve_shared(t: *sys::type_desc, @@ -122,7 +117,10 @@ fn reserve(&v: [const T], n: uint) { #[doc = "Returns the length of a vector"] #[inline(always)] -pure fn len(v: [const T]) -> uint { unchecked { rusti::vec_len(v) } } +pure fn len(&&v: [const T]) -> uint unsafe { + let repr: **unsafe::vec_repr = ::unsafe::reinterpret_cast(addr_of(v)); + (**repr).fill / sys::size_of::() +} #[doc = " Creates and initializes an immutable vector.