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-06-25 13:29:41 -0700
committerTim Chevalier <chevalier@alum.wellesley.edu>2012-06-25 13:29:41 -0700
commitda470ff5b8b970c90b4dce3885a2908d477c1021 (patch)
treef65aa2d105f18cdeb79321a37e47e26a5f592149 /src/libcore/ptr.rs
parent44d0a061be0ec9cae51c61b7bb335e8c4b8c6cf5 (diff)
parentfad307d7b4aa5e7ff6cf30194b069062aaad1a89 (diff)
Merge
Diffstat (limited to 'src/libcore/ptr.rs')
-rw-r--r--src/libcore/ptr.rs90
1 files changed, 50 insertions, 40 deletions
diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs
index 2a89abc040f..d6288ea2126 100644
--- a/src/libcore/ptr.rs
+++ b/src/libcore/ptr.rs
@@ -39,20 +39,26 @@ pure fn addr_of<T>(val: T) -> *T { unchecked { rusti::addr_of(val) } }
 
 #[doc = "Get an unsafe mut pointer to a value"]
 #[inline(always)]
-pure fn mut_addr_of<T>(val: T) -> *mut T unsafe {
-    unsafe::reinterpret_cast(rusti::addr_of(val))
+pure fn mut_addr_of<T>(val: T) -> *mut T {
+    unsafe {
+        unsafe::reinterpret_cast(rusti::addr_of(val))
+    }
 }
 
 #[doc = "Calculate the offset from a pointer"]
 #[inline(always)]
-fn offset<T>(ptr: *T, count: uint) -> *T unsafe {
-    (ptr as uint + count * sys::size_of::<T>()) as *T
+fn offset<T>(ptr: *T, count: uint) -> *T {
+    unsafe {
+        (ptr as uint + count * sys::size_of::<T>()) as *T
+    }
 }
 
 #[doc = "Calculate the offset from a const pointer"]
 #[inline(always)]
-fn const_offset<T>(ptr: *const T, count: uint) -> *const T unsafe {
-    (ptr as uint + count * sys::size_of::<T>()) as *T
+fn const_offset<T>(ptr: *const T, count: uint) -> *const T {
+    unsafe {
+        (ptr as uint + count * sys::size_of::<T>()) as *T
+    }
 }
 
 #[doc = "Calculate the offset from a mut pointer"]
@@ -79,7 +85,7 @@ unsafe fn position<T>(buf: *T, f: fn(T) -> bool) -> uint {
 
 #[doc = "Create an unsafe null pointer"]
 #[inline(always)]
-pure fn null<T>() -> *T unsafe { ret unsafe::reinterpret_cast(0u); }
+pure fn null<T>() -> *T { unsafe { unsafe::reinterpret_cast(0u) } }
 
 #[doc = "Returns true if the pointer is equal to the null pointer."]
 pure fn is_null<T>(ptr: *const T) -> bool { ptr == null() }
@@ -127,48 +133,52 @@ impl extensions<T> for *T {
 }
 
 #[test]
-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 iptr: *mut int = unsafe::reinterpret_cast(pptr);
-    assert (*iptr == 10);;
-    *iptr = 30;
-    assert (*iptr == 30);
-    assert (p.fst == 30);;
-
-    *pptr = {mut fst: 50, mut snd: 60};
-    assert (*iptr == 50);
-    assert (p.fst == 50);
-    assert (p.snd == 60);
-
-    let v0 = [32000u16, 32001u16, 32002u16];
-    let v1 = [0u16, 0u16, 0u16];
-
-    ptr::memcpy(ptr::offset(vec::unsafe::to_ptr(v1), 1u),
-                ptr::offset(vec::unsafe::to_ptr(v0), 1u), 1u);
-    assert (v1[0] == 0u16 && v1[1] == 32001u16 && v1[2] == 0u16);
-    ptr::memcpy(vec::unsafe::to_ptr(v1),
-                ptr::offset(vec::unsafe::to_ptr(v0), 2u), 1u);
-    assert (v1[0] == 32002u16 && v1[1] == 32001u16 && v1[2] == 0u16);
-    ptr::memcpy(ptr::offset(vec::unsafe::to_ptr(v1), 2u),
-                vec::unsafe::to_ptr(v0), 1u);
-    assert (v1[0] == 32002u16 && v1[1] == 32001u16 && v1[2] == 32000u16);
+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 iptr: *mut int = unsafe::reinterpret_cast(pptr);
+        assert (*iptr == 10);;
+        *iptr = 30;
+        assert (*iptr == 30);
+        assert (p.fst == 30);;
+
+        *pptr = {mut fst: 50, mut snd: 60};
+        assert (*iptr == 50);
+        assert (p.fst == 50);
+        assert (p.snd == 60);
+
+        let v0 = [32000u16, 32001u16, 32002u16];
+        let v1 = [0u16, 0u16, 0u16];
+
+        ptr::memcpy(ptr::offset(vec::unsafe::to_ptr(v1), 1u),
+                    ptr::offset(vec::unsafe::to_ptr(v0), 1u), 1u);
+        assert (v1[0] == 0u16 && v1[1] == 32001u16 && v1[2] == 0u16);
+        ptr::memcpy(vec::unsafe::to_ptr(v1),
+                    ptr::offset(vec::unsafe::to_ptr(v0), 2u), 1u);
+        assert (v1[0] == 32002u16 && v1[1] == 32001u16 && v1[2] == 0u16);
+        ptr::memcpy(ptr::offset(vec::unsafe::to_ptr(v1), 2u),
+                    vec::unsafe::to_ptr(v0), 1u);
+        assert (v1[0] == 32002u16 && v1[1] == 32001u16 && v1[2] == 32000u16);
+    }
 }
 
 #[test]
-fn test_position() unsafe {
+fn test_position() {
     import str::as_c_str;
     import libc::c_char;
 
     let s = "hello";
-    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 } };
+    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 } };
+    }
 }
 
 #[test]
-fn test_buf_len() unsafe {
+fn test_buf_len() {
     let s0 = "hello";
     let s1 = "there";
     let s2 = "thing";
@@ -177,7 +187,7 @@ fn test_buf_len() unsafe {
             str::as_c_str(s2) {|p2|
                 let v = [p0, p1, p2, null()];
                 vec::as_buf(v) {|vp|
-                    assert buf_len(vp) == 3u;
+                    assert unsafe { buf_len(vp) } == 3u;
                 }
             }
         }