about summary refs log tree commit diff
path: root/src/libcore/ptr.rs
diff options
context:
space:
mode:
authorTim Chevalier <chevalier@alum.wellesley.edu>2012-09-28 21:51:14 -0700
committerTim Chevalier <chevalier@alum.wellesley.edu>2012-09-28 22:19:01 -0700
commit3639d38d5c982193eab8304f2adfe0fcd2dd84bd (patch)
tree84d883ad752071f14a4300e5f59a9a4944651e0b /src/libcore/ptr.rs
parentf1014c43fd4e22fa1a8190e642e05dc6891d6eaa (diff)
downloadrust-3639d38d5c982193eab8304f2adfe0fcd2dd84bd.tar.gz
rust-3639d38d5c982193eab8304f2adfe0fcd2dd84bd.zip
Add a demoded version of ptr::addr_of
Currently, the new version is ptr::p2::addr_of and the old one is
ptr::addr_of. This is kind of cheesy, but I need a snapshot before I
can ditch the old version, since the pipe compiler generates calls to
addr_of.

core is converted over to use the new version, std is not.
Diffstat (limited to 'src/libcore/ptr.rs')
-rw-r--r--src/libcore/ptr.rs31
1 files changed, 20 insertions, 11 deletions
diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs
index 0cc283e89a4..d0b848adde2 100644
--- a/src/libcore/ptr.rs
+++ b/src/libcore/ptr.rs
@@ -24,15 +24,24 @@ extern mod rusti {
     fn addr_of<T>(val: T) -> *T;
 }
 
+/*
+Remove this after snapshot; make p2::addr_of addr_of
+*/
 /// Get an unsafe pointer to a value
 #[inline(always)]
 pub pure fn addr_of<T>(val: T) -> *T { unsafe { rusti::addr_of(val) } }
 
+pub mod p2 {
+    /// Get an unsafe pointer to a value
+    #[inline(always)]
+    pub pure fn addr_of<T>(val: &T) -> *T { unsafe { rusti::addr_of(*val) } }
+}
+
 /// Get an unsafe mut pointer to a value
 #[inline(always)]
-pub pure fn mut_addr_of<T>(val: T) -> *mut T {
+pub pure fn mut_addr_of<T>(val: &T) -> *mut T {
     unsafe {
-        cast::reinterpret_cast(&rusti::addr_of(val))
+        cast::reinterpret_cast(&rusti::addr_of(*val))
     }
 }
 
@@ -61,16 +70,16 @@ pub fn mut_offset<T>(ptr: *mut T, count: uint) -> *mut T {
 /// Return the offset of the first null pointer in `buf`.
 #[inline(always)]
 pub unsafe fn buf_len<T>(buf: **T) -> uint {
-    position(buf, |i| i == null())
+    position(buf, |i| *i == null())
 }
 
 /// Return the first offset `i` such that `f(buf[i]) == true`.
 #[inline(always)]
-pub unsafe fn position<T>(buf: *T, f: fn(T) -> bool) -> uint {
-    let mut i = 0u;
+pub unsafe fn position<T>(buf: *T, f: fn(&T) -> bool) -> uint {
+    let mut i = 0;
     loop {
-        if f(*offset(buf, i)) { return i; }
-        else { i += 1u; }
+        if f(&(*offset(buf, i))) { return i; }
+        else { i += 1; }
     }
 }
 
@@ -234,7 +243,7 @@ pub fn test() {
     unsafe {
         type Pair = {mut fst: int, mut snd: int};
         let p = {mut fst: 10, mut snd: 20};
-        let pptr: *mut Pair = mut_addr_of(p);
+        let pptr: *mut Pair = mut_addr_of(&p);
         let iptr: *mut int = cast::reinterpret_cast(&pptr);
         assert (*iptr == 10);;
         *iptr = 30;
@@ -268,9 +277,9 @@ pub fn test_position() {
 
     let s = ~"hello";
     unsafe {
-        assert 2u == as_c_str(s, |p| position(p, |c| c == 'l' as c_char));
-        assert 4u == as_c_str(s, |p| position(p, |c| c == 'o' as c_char));
-        assert 5u == as_c_str(s, |p| position(p, |c| c == 0 as c_char));
+        assert 2u == as_c_str(s, |p| position(p, |c| *c == 'l' as c_char));
+        assert 4u == as_c_str(s, |p| position(p, |c| *c == 'o' as c_char));
+        assert 5u == as_c_str(s, |p| position(p, |c| *c == 0 as c_char));
     }
 }