Skip to content

Commit 55c23bc

Browse files
committed
auto merge of #6862 : thestinger/rust/swap, r=bstrie
I don't like the `util` module in general, and `ptr` is a much better place for these.
2 parents 91a7073 + ed93cc1 commit 55c23bc

File tree

6 files changed

+66
-107
lines changed

6 files changed

+66
-107
lines changed

doc/tutorial-ffi.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ use std::cast;
153153
use std::libc::{c_void, size_t, malloc, free};
154154
use std::ptr;
155155
use std::unstable::intrinsics;
156-
use std::util;
157156
158157
// a wrapper around the handle returned by the foreign code
159158
pub struct Unique<T> {
@@ -186,9 +185,9 @@ pub impl<T: Owned> Unique<T> {
186185
impl<T: Owned> Drop for Unique<T> {
187186
fn finalize(&self) {
188187
unsafe {
189-
let mut x = intrinsics::init(); // dummy value to swap in
188+
let x = intrinsics::init(); // dummy value to swap in
190189
// moving the object out is needed to call the destructor
191-
util::replace_ptr(self.ptr, x);
190+
ptr::replace_ptr(self.ptr, x);
192191
free(self.ptr as *c_void)
193192
}
194193
}

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: 48 additions & 57 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;
@@ -71,11 +72,11 @@ pub unsafe fn position<T>(buf: *T, f: &fn(&T) -> bool) -> uint {
7172

7273
/// Create an unsafe null pointer
7374
#[inline(always)]
74-
pub fn null<T>() -> *T { unsafe { cast::transmute(0u) } }
75+
pub fn null<T>() -> *T { 0 as *T }
7576

7677
/// Create an unsafe mutable null pointer
7778
#[inline(always)]
78-
pub fn mut_null<T>() -> *mut T { unsafe { cast::transmute(0u) } }
79+
pub fn mut_null<T>() -> *mut T { 0 as *mut T }
7980

8081
/// Returns true if the pointer is equal to the null pointer.
8182
#[inline(always)]
@@ -207,47 +208,57 @@ pub unsafe fn set_memory<T>(dst: *mut T, c: u8, count: uint) {
207208
}
208209

209210
/**
210-
Transform a region pointer - &T - to an unsafe pointer - *T.
211-
This is safe, but is implemented with an unsafe block due to
212-
transmute.
213-
*/
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+
240+
/// Transform a region pointer - &T - to an unsafe pointer - *T.
214241
#[inline(always)]
215242
pub fn to_unsafe_ptr<T>(thing: &T) -> *T {
216-
unsafe { cast::transmute(thing) }
243+
thing as *T
217244
}
218245

219-
/**
220-
Transform a const region pointer - &const T - to a const unsafe pointer -
221-
*const T. This is safe, but is implemented with an unsafe block due to
222-
transmute.
223-
*/
246+
/// Transform a const region pointer - &const T - to a const unsafe pointer - *const T.
224247
#[inline(always)]
225248
pub fn to_const_unsafe_ptr<T>(thing: &const T) -> *const T {
226-
unsafe { cast::transmute(thing) }
249+
thing as *const T
227250
}
228251

229-
/**
230-
Transform a mutable region pointer - &mut T - to a mutable unsafe pointer -
231-
*mut T. This is safe, but is implemented with an unsafe block due to
232-
transmute.
233-
*/
252+
/// Transform a mutable region pointer - &mut T - to a mutable unsafe pointer - *mut T.
234253
#[inline(always)]
235254
pub fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T {
236-
unsafe { cast::transmute(thing) }
255+
thing as *mut T
237256
}
238257

239-
/**
240-
Cast a region pointer - &T - to a uint.
241-
This is safe, but is implemented with an unsafe block due to
242-
transmute.
243-
244-
(I couldn't think of a cutesy name for this one.)
245-
*/
258+
/// Cast a region pointer - &T - to a uint.
246259
#[inline(always)]
247260
pub fn to_uint<T>(thing: &T) -> uint {
248-
unsafe {
249-
cast::transmute(thing)
250-
}
261+
thing as *T as uint
251262
}
252263

253264
/// Determine if two borrowed pointers point to the same thing.
@@ -373,50 +384,30 @@ impl<T> Ptr<T> for *mut T {
373384
impl<T> Eq for *const T {
374385
#[inline(always)]
375386
fn eq(&self, other: &*const T) -> bool {
376-
unsafe {
377-
let a: uint = cast::transmute(*self);
378-
let b: uint = cast::transmute(*other);
379-
return a == b;
380-
}
387+
(*self as uint) == (*other as uint)
381388
}
382389
#[inline(always)]
383-
fn ne(&self, other: &*const T) -> bool { !(*self).eq(other) }
390+
fn ne(&self, other: &*const T) -> bool { !self.eq(other) }
384391
}
385392

386393
// Comparison for pointers
387394
#[cfg(not(test))]
388395
impl<T> Ord for *const T {
389396
#[inline(always)]
390397
fn lt(&self, other: &*const T) -> bool {
391-
unsafe {
392-
let a: uint = cast::transmute(*self);
393-
let b: uint = cast::transmute(*other);
394-
return a < b;
395-
}
398+
(*self as uint) < (*other as uint)
396399
}
397400
#[inline(always)]
398401
fn le(&self, other: &*const T) -> bool {
399-
unsafe {
400-
let a: uint = cast::transmute(*self);
401-
let b: uint = cast::transmute(*other);
402-
return a <= b;
403-
}
402+
(*self as uint) <= (*other as uint)
404403
}
405404
#[inline(always)]
406405
fn ge(&self, other: &*const T) -> bool {
407-
unsafe {
408-
let a: uint = cast::transmute(*self);
409-
let b: uint = cast::transmute(*other);
410-
return a >= b;
411-
}
406+
(*self as uint) >= (*other as uint)
412407
}
413408
#[inline(always)]
414409
fn gt(&self, other: &*const T) -> bool {
415-
unsafe {
416-
let a: uint = cast::transmute(*self);
417-
let b: uint = cast::transmute(*other);
418-
return a > b;
419-
}
410+
(*self as uint) > (*other as uint)
420411
}
421412
}
422413

@@ -425,11 +416,11 @@ impl<T> Ord for *const T {
425416
impl<'self,T:Eq> Eq for &'self T {
426417
#[inline(always)]
427418
fn eq(&self, other: & &'self T) -> bool {
428-
return *(*self) == *(*other);
419+
*(*self) == *(*other)
429420
}
430421
#[inline(always)]
431422
fn ne(&self, other: & &'self T) -> bool {
432-
return *(*self) != *(*other);
423+
*(*self) != *(*other)
433424
}
434425
}
435426

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)