Skip to content

Commit bc3f5e7

Browse files
committed
rustc: Replace intrinsic vec_len with unsafe Rust code
Preparation for #1981
1 parent cce2751 commit bc3f5e7

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

src/libcore/ptr.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export memmove;
1212
#[abi = "rust-intrinsic"]
1313
native mod rusti {
1414
fn addr_of<T>(val: T) -> *T;
15-
fn ptr_offset<T>(ptr: *T, count: libc::uintptr_t) -> *T;
1615
fn memcpy<T>(dst: *T, src: *T, count: libc::uintptr_t);
1716
fn memmove<T>(dst: *T, src: *T, count: libc::uintptr_t);
1817
}
@@ -29,14 +28,14 @@ fn mut_addr_of<T>(val: T) -> *mutable T unsafe {
2928

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

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

4241

src/libcore/vec.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@ export vec_len;
7474
export unsafe;
7575
export u8;
7676

77-
#[abi = "rust-intrinsic"]
78-
native mod rusti {
79-
fn vec_len<T>(&&v: [const T]) -> libc::size_t;
80-
}
81-
8277
#[abi = "cdecl"]
8378
native mod rustrt {
8479
fn vec_reserve_shared<T>(t: *sys::type_desc,
@@ -122,7 +117,10 @@ fn reserve<T>(&v: [const T], n: uint) {
122117

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

127125
#[doc = "
128126
Creates and initializes an immutable vector.

0 commit comments

Comments
 (0)