Skip to content

Commit 29aba80

Browse files
committed
mv the raw pointer {swap,replace}_ptr to std::ptr
1 parent 030f471 commit 29aba80

File tree

6 files changed

+48
-48
lines changed

6 files changed

+48
-48
lines changed

doc/tutorial-ffi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl<T: Owned> Drop for Unique<T> {
188188
unsafe {
189189
let mut x = intrinsics::init(); // dummy value to swap in
190190
// moving the object out is needed to call the destructor
191-
util::replace_ptr(self.ptr, x);
191+
ptr::replace_ptr(self.ptr, x);
192192
free(self.ptr as *c_void)
193193
}
194194
}

src/libextra/rc.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use core::libc::{c_void, size_t, malloc, free};
2828
use core::ptr;
2929
use core::sys;
3030
use core::unstable::intrinsics;
31-
use core::util;
3231

3332
struct RcBox<T> {
3433
value: T,
@@ -73,7 +72,7 @@ impl<T> Drop for Rc<T> {
7372
unsafe {
7473
(*self.ptr).count -= 1;
7574
if (*self.ptr).count == 0 {
76-
util::replace_ptr(self.ptr, intrinsics::uninit());
75+
ptr::replace_ptr(self.ptr, intrinsics::uninit());
7776
free(self.ptr as *c_void)
7877
}
7978
}
@@ -223,7 +222,7 @@ impl<T> Drop for RcMut<T> {
223222
unsafe {
224223
(*self.ptr).count -= 1;
225224
if (*self.ptr).count == 0 {
226-
util::replace_ptr(self.ptr, uninit());
225+
ptr::replace_ptr(self.ptr, uninit());
227226
free(self.ptr as *c_void)
228227
}
229228
}

src/libstd/ptr.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use cast;
1515
#[cfg(stage0)] use libc::{c_void, size_t};
1616
use option::{Option, Some, None};
1717
use sys;
18+
use unstable::intrinsics;
1819

1920
#[cfg(not(test))] use cmp::{Eq, Ord};
2021
use uint;
@@ -206,6 +207,36 @@ pub unsafe fn set_memory<T>(dst: *mut T, c: u8, count: uint) {
206207
memset64(dst, c, count as u64);
207208
}
208209

210+
/**
211+
* Swap the values at two mutable locations of the same type, without
212+
* deinitialising or copying either one.
213+
*/
214+
#[inline]
215+
pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) {
216+
// Give ourselves some scratch space to work with
217+
let mut tmp: T = intrinsics::uninit();
218+
let t: *mut T = &mut tmp;
219+
220+
// Perform the swap
221+
copy_memory(t, x, 1);
222+
copy_memory(x, y, 1);
223+
copy_memory(y, t, 1);
224+
225+
// y and t now point to the same thing, but we need to completely forget `tmp`
226+
// because it's no longer relevant.
227+
cast::forget(tmp);
228+
}
229+
230+
/**
231+
* Replace the value at a mutable location with a new one, returning the old
232+
* value, without deinitialising or copying either one.
233+
*/
234+
#[inline(always)]
235+
pub unsafe fn replace_ptr<T>(dest: *mut T, mut src: T) -> T {
236+
swap_ptr(dest, &mut src);
237+
src
238+
}
239+
209240
/**
210241
Transform a region pointer - &T - to an unsafe pointer - *T.
211242
This is safe, but is implemented with an unsafe block due to

src/libstd/util.rs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,6 @@ pub fn swap<T>(x: &mut T, y: &mut T) {
6464
}
6565
}
6666

67-
/**
68-
* Swap the values at two mutable locations of the same type, without
69-
* deinitialising or copying either one.
70-
*/
71-
#[inline]
72-
pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) {
73-
// Give ourselves some scratch space to work with
74-
let mut tmp: T = intrinsics::uninit();
75-
let t: *mut T = &mut tmp;
76-
77-
// Perform the swap
78-
ptr::copy_memory(t, x, 1);
79-
ptr::copy_memory(x, y, 1);
80-
ptr::copy_memory(y, t, 1);
81-
82-
// y and t now point to the same thing, but we need to completely forget `tmp`
83-
// because it's no longer relevant.
84-
cast::forget(tmp);
85-
}
86-
8767
/**
8868
* Replace the value at a mutable location with a new one, returning the old
8969
* value, without deinitialising or copying either one.
@@ -94,16 +74,6 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
9474
src
9575
}
9676

97-
/**
98-
* Replace the value at a mutable location with a new one, returning the old
99-
* value, without deinitialising or copying either one.
100-
*/
101-
#[inline(always)]
102-
pub unsafe fn replace_ptr<T>(dest: *mut T, mut src: T) -> T {
103-
swap_ptr(dest, ptr::to_mut_unsafe_ptr(&mut src));
104-
src
105-
}
106-
10777
/// A non-copyable dummy type.
10878
pub struct NonCopyable {
10979
priv i: (),

src/libstd/vec.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ pub fn shift<T>(v: &mut ~[T]) -> T {
506506
let vp = raw::to_mut_ptr(*v);
507507
let vp = ptr::mut_offset(vp, next_ln - 1);
508508

509-
util::replace_ptr(vp, work_elt)
509+
ptr::replace_ptr(vp, work_elt)
510510
}
511511
}
512512

@@ -570,7 +570,7 @@ pub fn consume<T>(mut v: ~[T], f: &fn(uint, v: T)) {
570570
// elements during unwinding
571571
let x = intrinsics::init();
572572
let p = ptr::mut_offset(p, i);
573-
f(i, util::replace_ptr(p, x));
573+
f(i, ptr::replace_ptr(p, x));
574574
}
575575
}
576576

@@ -597,7 +597,7 @@ pub fn consume_reverse<T>(mut v: ~[T], f: &fn(uint, v: T)) {
597597
// elements during unwinding
598598
let x = intrinsics::init();
599599
let p = ptr::mut_offset(p, i);
600-
f(i, util::replace_ptr(p, x));
600+
f(i, ptr::replace_ptr(p, x));
601601
}
602602
}
603603

@@ -613,7 +613,7 @@ pub fn pop<T>(v: &mut ~[T]) -> T {
613613
}
614614
let valptr = ptr::to_mut_unsafe_ptr(&mut v[ln - 1u]);
615615
unsafe {
616-
let val = util::replace_ptr(valptr, intrinsics::init());
616+
let val = ptr::replace_ptr(valptr, intrinsics::init());
617617
raw::set_len(v, ln - 1u);
618618
val
619619
}
@@ -707,8 +707,8 @@ pub fn push_all_move<T>(v: &mut ~[T], mut rhs: ~[T]) {
707707
unsafe {
708708
do as_mut_buf(rhs) |p, len| {
709709
for uint::range(0, len) |i| {
710-
let x = util::replace_ptr(ptr::mut_offset(p, i),
711-
intrinsics::uninit());
710+
let x = ptr::replace_ptr(ptr::mut_offset(p, i),
711+
intrinsics::uninit());
712712
push(&mut *v, x);
713713
}
714714
}
@@ -723,7 +723,7 @@ pub fn truncate<T>(v: &mut ~[T], newlen: uint) {
723723
unsafe {
724724
// This loop is optimized out for non-drop types.
725725
for uint::range(newlen, oldlen) |i| {
726-
util::replace_ptr(ptr::mut_offset(p, i), intrinsics::uninit());
726+
ptr::replace_ptr(ptr::mut_offset(p, i), intrinsics::uninit());
727727
}
728728
}
729729
}
@@ -747,14 +747,14 @@ pub fn dedup<T:Eq>(v: &mut ~[T]) {
747747
// last_written < next_to_read < ln
748748
if *ptr::mut_offset(p, next_to_read) ==
749749
*ptr::mut_offset(p, last_written) {
750-
util::replace_ptr(ptr::mut_offset(p, next_to_read),
751-
intrinsics::uninit());
750+
ptr::replace_ptr(ptr::mut_offset(p, next_to_read),
751+
intrinsics::uninit());
752752
} else {
753753
last_written += 1;
754754
// last_written <= next_to_read < ln
755755
if next_to_read != last_written {
756-
util::swap_ptr(ptr::mut_offset(p, last_written),
757-
ptr::mut_offset(p, next_to_read));
756+
ptr::swap_ptr(ptr::mut_offset(p, last_written),
757+
ptr::mut_offset(p, next_to_read));
758758
}
759759
}
760760
// last_written <= next_to_read < ln
@@ -1398,7 +1398,7 @@ pub fn swap<T>(v: &mut [T], a: uint, b: uint) {
13981398
// them to their raw pointers to do the swap
13991399
let pa: *mut T = ptr::to_mut_unsafe_ptr(&mut v[a]);
14001400
let pb: *mut T = ptr::to_mut_unsafe_ptr(&mut v[b]);
1401-
util::swap_ptr(pa, pb);
1401+
ptr::swap_ptr(pa, pb);
14021402
}
14031403
}
14041404

src/test/run-pass/swap-overlapping.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ pub fn main() {
2626

2727
fn do_swap(test: &mut TestDescAndFn) {
2828
unsafe {
29-
util::swap_ptr(ptr::to_mut_unsafe_ptr(test),
30-
ptr::to_mut_unsafe_ptr(test));
29+
ptr::swap_ptr(ptr::to_mut_unsafe_ptr(test),
30+
ptr::to_mut_unsafe_ptr(test));
3131
}
3232
}
3333

0 commit comments

Comments
 (0)