about summary refs log tree commit diff
path: root/src/libstd/ptr.rs
diff options
context:
space:
mode:
authorDaniel Micay <danielmicay@gmail.com>2013-05-31 10:21:29 -0400
committerDaniel Micay <danielmicay@gmail.com>2013-05-31 10:31:26 -0400
commit29aba8033afa4cab0261c82d5a4eded4b79af656 (patch)
tree863de7278108500a2dd1dd24d433128fbdfed19d /src/libstd/ptr.rs
parent030f471f26dbb6642c54a1e12ce63f7989db01ab (diff)
downloadrust-29aba8033afa4cab0261c82d5a4eded4b79af656.tar.gz
rust-29aba8033afa4cab0261c82d5a4eded4b79af656.zip
mv the raw pointer {swap,replace}_ptr to std::ptr
Diffstat (limited to 'src/libstd/ptr.rs')
-rw-r--r--src/libstd/ptr.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/libstd/ptr.rs b/src/libstd/ptr.rs
index 0f7cf3f6bdf..cdd99ee3603 100644
--- a/src/libstd/ptr.rs
+++ b/src/libstd/ptr.rs
@@ -15,6 +15,7 @@ use cast;
 #[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;
@@ -207,6 +208,36 @@ pub unsafe fn set_memory<T>(dst: *mut T, c: u8, count: uint) {
 }
 
 /**
+ * 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
   transmute.