]> git.lizzy.rs Git - rust.git/commitdiff
mv the raw pointer {swap,replace}_ptr to std::ptr
authorDaniel Micay <danielmicay@gmail.com>
Fri, 31 May 2013 14:21:29 +0000 (10:21 -0400)
committerDaniel Micay <danielmicay@gmail.com>
Fri, 31 May 2013 14:31:26 +0000 (10:31 -0400)
doc/tutorial-ffi.md
src/libextra/rc.rs
src/libstd/ptr.rs
src/libstd/util.rs
src/libstd/vec.rs
src/test/run-pass/swap-overlapping.rs

index 88f2f374cf5dd721d76e0dff7bd77fb13db360d9..ceaf30bdb8fadc09fea206a308f399db54e384a1 100644 (file)
@@ -188,7 +188,7 @@ impl<T: Owned> Drop for Unique<T> {
         unsafe {
             let mut x = intrinsics::init(); // dummy value to swap in
             // moving the object out is needed to call the destructor
-            util::replace_ptr(self.ptr, x);
+            ptr::replace_ptr(self.ptr, x);
             free(self.ptr as *c_void)
         }
     }
index 381b8ac05ba3611752da152faba7cb5f1ae9eb57..8cd1c893bb6f04ed8d4b66221aec53eb75135b1a 100644 (file)
@@ -28,7 +28,6 @@
 use core::ptr;
 use core::sys;
 use core::unstable::intrinsics;
-use core::util;
 
 struct RcBox<T> {
     value: T,
@@ -73,7 +72,7 @@ fn finalize(&self) {
         unsafe {
             (*self.ptr).count -= 1;
             if (*self.ptr).count == 0 {
-                util::replace_ptr(self.ptr, intrinsics::uninit());
+                ptr::replace_ptr(self.ptr, intrinsics::uninit());
                 free(self.ptr as *c_void)
             }
         }
@@ -223,7 +222,7 @@ fn finalize(&self) {
         unsafe {
             (*self.ptr).count -= 1;
             if (*self.ptr).count == 0 {
-                util::replace_ptr(self.ptr, uninit());
+                ptr::replace_ptr(self.ptr, uninit());
                 free(self.ptr as *c_void)
             }
         }
index 0f7cf3f6bdf43b690122412032f3017ade5f2a6c..cdd99ee36038bdbcaea30eefbe0d9cb1b0843a00 100644 (file)
@@ -15,6 +15,7 @@
 #[cfg(stage0)] use libc::{c_void, size_t};
 use option::{Option, Some, None};
 use sys;
+use unstable::intrinsics;
 
 #[cfg(not(test))] use cmp::{Eq, Ord};
 use uint;
@@ -206,6 +207,36 @@ pub unsafe fn set_memory<T>(dst: *mut T, c: u8, count: uint) {
     memset64(dst, c, count as u64);
 }
 
+/**
+ * Swap the values at two mutable locations of the same type, without
+ * deinitialising or copying either one.
+ */
+#[inline]
+pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) {
+    // Give ourselves some scratch space to work with
+    let mut tmp: T = intrinsics::uninit();
+    let t: *mut T = &mut tmp;
+
+    // Perform the swap
+    copy_memory(t, x, 1);
+    copy_memory(x, y, 1);
+    copy_memory(y, t, 1);
+
+    // y and t now point to the same thing, but we need to completely forget `tmp`
+    // because it's no longer relevant.
+    cast::forget(tmp);
+}
+
+/**
+ * Replace the value at a mutable location with a new one, returning the old
+ * value, without deinitialising or copying either one.
+ */
+#[inline(always)]
+pub unsafe fn replace_ptr<T>(dest: *mut T, mut src: T) -> T {
+    swap_ptr(dest, &mut src);
+    src
+}
+
 /**
   Transform a region pointer - &T - to an unsafe pointer - *T.
   This is safe, but is implemented with an unsafe block due to
index 2b61356129e01c5dd0d65c980568ceeefa1322f0..21fbe2836cd68264e41de741abe91362a4b85a2c 100644 (file)
@@ -64,26 +64,6 @@ pub fn swap<T>(x: &mut T, y: &mut T) {
     }
 }
 
-/**
- * Swap the values at two mutable locations of the same type, without
- * deinitialising or copying either one.
- */
-#[inline]
-pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) {
-    // Give ourselves some scratch space to work with
-    let mut tmp: T = intrinsics::uninit();
-    let t: *mut T = &mut tmp;
-
-    // Perform the swap
-    ptr::copy_memory(t, x, 1);
-    ptr::copy_memory(x, y, 1);
-    ptr::copy_memory(y, t, 1);
-
-    // y and t now point to the same thing, but we need to completely forget `tmp`
-    // because it's no longer relevant.
-    cast::forget(tmp);
-}
-
 /**
  * Replace the value at a mutable location with a new one, returning the old
  * value, without deinitialising or copying either one.
@@ -94,16 +74,6 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
     src
 }
 
-/**
- * Replace the value at a mutable location with a new one, returning the old
- * value, without deinitialising or copying either one.
- */
-#[inline(always)]
-pub unsafe fn replace_ptr<T>(dest: *mut T, mut src: T) -> T {
-    swap_ptr(dest, ptr::to_mut_unsafe_ptr(&mut src));
-    src
-}
-
 /// A non-copyable dummy type.
 pub struct NonCopyable {
     priv i: (),
index c02d87923c04b4b6814ace76f84051f985d42f24..795c3cdb405e16da17c0c29b6493761a0e77596e 100644 (file)
@@ -506,7 +506,7 @@ pub fn shift<T>(v: &mut ~[T]) -> T {
         let vp = raw::to_mut_ptr(*v);
         let vp = ptr::mut_offset(vp, next_ln - 1);
 
-        util::replace_ptr(vp, work_elt)
+        ptr::replace_ptr(vp, work_elt)
     }
 }
 
@@ -570,7 +570,7 @@ pub fn consume<T>(mut v: ~[T], f: &fn(uint, v: T)) {
                 // elements during unwinding
                 let x = intrinsics::init();
                 let p = ptr::mut_offset(p, i);
-                f(i, util::replace_ptr(p, x));
+                f(i, ptr::replace_ptr(p, x));
             }
         }
 
@@ -597,7 +597,7 @@ pub fn consume_reverse<T>(mut v: ~[T], f: &fn(uint, v: T)) {
                 // elements during unwinding
                 let x = intrinsics::init();
                 let p = ptr::mut_offset(p, i);
-                f(i, util::replace_ptr(p, x));
+                f(i, ptr::replace_ptr(p, x));
             }
         }
 
@@ -613,7 +613,7 @@ pub fn pop<T>(v: &mut ~[T]) -> T {
     }
     let valptr = ptr::to_mut_unsafe_ptr(&mut v[ln - 1u]);
     unsafe {
-        let val = util::replace_ptr(valptr, intrinsics::init());
+        let val = ptr::replace_ptr(valptr, intrinsics::init());
         raw::set_len(v, ln - 1u);
         val
     }
@@ -707,8 +707,8 @@ pub fn push_all_move<T>(v: &mut ~[T], mut rhs: ~[T]) {
     unsafe {
         do as_mut_buf(rhs) |p, len| {
             for uint::range(0, len) |i| {
-                let x = util::replace_ptr(ptr::mut_offset(p, i),
-                                          intrinsics::uninit());
+                let x = ptr::replace_ptr(ptr::mut_offset(p, i),
+                                         intrinsics::uninit());
                 push(&mut *v, x);
             }
         }
@@ -723,7 +723,7 @@ pub fn truncate<T>(v: &mut ~[T], newlen: uint) {
         unsafe {
             // This loop is optimized out for non-drop types.
             for uint::range(newlen, oldlen) |i| {
-                util::replace_ptr(ptr::mut_offset(p, i), intrinsics::uninit());
+                ptr::replace_ptr(ptr::mut_offset(p, i), intrinsics::uninit());
             }
         }
     }
@@ -747,14 +747,14 @@ pub fn dedup<T:Eq>(v: &mut ~[T]) {
                 // last_written < next_to_read < ln
                 if *ptr::mut_offset(p, next_to_read) ==
                     *ptr::mut_offset(p, last_written) {
-                    util::replace_ptr(ptr::mut_offset(p, next_to_read),
-                                      intrinsics::uninit());
+                    ptr::replace_ptr(ptr::mut_offset(p, next_to_read),
+                                     intrinsics::uninit());
                 } else {
                     last_written += 1;
                     // last_written <= next_to_read < ln
                     if next_to_read != last_written {
-                        util::swap_ptr(ptr::mut_offset(p, last_written),
-                                       ptr::mut_offset(p, next_to_read));
+                        ptr::swap_ptr(ptr::mut_offset(p, last_written),
+                                      ptr::mut_offset(p, next_to_read));
                     }
                 }
                 // last_written <= next_to_read < ln
@@ -1398,7 +1398,7 @@ pub fn swap<T>(v: &mut [T], a: uint, b: uint) {
         // them to their raw pointers to do the swap
         let pa: *mut T = ptr::to_mut_unsafe_ptr(&mut v[a]);
         let pb: *mut T = ptr::to_mut_unsafe_ptr(&mut v[b]);
-        util::swap_ptr(pa, pb);
+        ptr::swap_ptr(pa, pb);
     }
 }
 
index d1d84137334082fd379eee56345677608a8070f3..07d1226d3829734ac48d012cd47a48dedd7b497a 100644 (file)
@@ -26,8 +26,8 @@ pub fn main() {
 
 fn do_swap(test: &mut TestDescAndFn) {
     unsafe {
-        util::swap_ptr(ptr::to_mut_unsafe_ptr(test),
-                       ptr::to_mut_unsafe_ptr(test));
+        ptr::swap_ptr(ptr::to_mut_unsafe_ptr(test),
+                      ptr::to_mut_unsafe_ptr(test));
     }
 }