about summary refs log tree commit diff
path: root/src/libcore/ptr.rs
diff options
context:
space:
mode:
authorBrendan Zabarauskas <bjzaba@yahoo.com.au>2013-05-20 22:07:11 +1000
committerBrendan Zabarauskas <bjzaba@yahoo.com.au>2013-05-20 22:07:35 +1000
commiteef03c39cf2f25f2e2182a68d0fcad14f378d5ac (patch)
tree8d3af5043f639f731061e621c508d74c4bec8442 /src/libcore/ptr.rs
parentd1e091a27a6ee0d32fcda0830ec5f9a7ad585d9e (diff)
downloadrust-eef03c39cf2f25f2e2182a68d0fcad14f378d5ac.tar.gz
rust-eef03c39cf2f25f2e2182a68d0fcad14f378d5ac.zip
Update to stop unsolicited copying and mark methods as unsafe
Diffstat (limited to 'src/libcore/ptr.rs')
-rw-r--r--src/libcore/ptr.rs40
1 files changed, 29 insertions, 11 deletions
diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs
index 6254d3349d3..dfd38584364 100644
--- a/src/libcore/ptr.rs
+++ b/src/libcore/ptr.rs
@@ -210,7 +210,7 @@ pub unsafe fn array_each<T>(arr: **T, cb: &fn(*T)) {
 pub trait Ptr<T> {
     fn is_null(&const self) -> bool;
     fn is_not_null(&const self) -> bool;
-    fn to_option(&const self) -> Option<T>;
+    unsafe fn to_option(&const self) -> Option<&T>;
     fn offset(&self, count: uint) -> Self;
 }
 
@@ -224,11 +224,20 @@ impl<T> Ptr<T> for *T {
     #[inline(always)]
     fn is_not_null(&const self) -> bool { is_not_null(*self) }
 
-    /// Returns `None` if the pointer is null, or else returns the value wrapped in `Some`.
+    ///
+    /// Returns `None` if the pointer is null, or else returns the value wrapped
+    /// in `Some`.
+    ///
+    /// # Safety Notes
+    ///
+    /// While this method is useful for null-safety, it is important to note
+    /// that this is still an unsafe operation because the returned value could
+    /// be pointing to invalid memory.
+    ///
     #[inline(always)]
-    fn to_option(&const self) -> Option<T> {
+    unsafe fn to_option(&const self) -> Option<&T> {
         if self.is_null() { None } else {
-            Some(unsafe { **self })
+            Some(cast::transmute(*self))
         }
     }
 
@@ -247,11 +256,20 @@ impl<T> Ptr<T> for *mut T {
     #[inline(always)]
     fn is_not_null(&const self) -> bool { is_not_null(*self) }
 
-    /// Returns `None` if the pointer is null, or else returns the value wrapped in `Some`.
+    ///
+    /// Returns `None` if the pointer is null, or else returns the value wrapped
+    /// in `Some`.
+    ///
+    /// # Safety Notes
+    ///
+    /// While this method is useful for null-safety, it is important to note
+    /// that this is still an unsafe operation because the returned value could
+    /// be pointing to invalid memory.
+    ///
     #[inline(always)]
-    fn to_option(&const self) -> Option<T> {
+    unsafe fn to_option(&const self) -> Option<&T> {
         if self.is_null() { None } else {
-            Some(unsafe { **self })
+            Some(cast::transmute(*self))
         }
     }
 
@@ -442,19 +460,19 @@ pub mod ptr_tests {
     }
 
     #[test]
-    #[allow(unused_mut)]
     fn test_to_option() {
         let p: *int = null();
+        // FIXME (#6641): Usage of unsafe methods in safe code doesn't cause an error.
         assert_eq!(p.to_option(), None);
 
         let q: *int = &2;
-        assert_eq!(q.to_option(), Some(2));
+        assert_eq!(q.to_option().unwrap(), &2);     // FIXME (#6641)
 
         let p: *mut int = mut_null();
-        assert_eq!(p.to_option(), None);
+        assert_eq!(p.to_option(), None);            // FIXME (#6641)
 
         let q: *mut int = &mut 2;
-        assert_eq!(q.to_option(), Some(2));
+        assert_eq!(q.to_option().unwrap(), &2);     // FIXME (#6641)
     }
 
     #[test]