about summary refs log tree commit diff
path: root/src/libcoretest
diff options
context:
space:
mode:
authorAndrew Poelstra <apoelstra@wpsoftware.net>2014-08-20 13:06:34 -0700
committerAndrew Poelstra <apoelstra@wpsoftware.net>2014-08-31 13:33:55 -0500
commit00ff5aac4ef48615321610b73f30da825700fb78 (patch)
treecdb9b2e004f96c91cf6781845e7ca4bd175bcd5b /src/libcoretest
parent27e8d5bca79c09258c757e9be6e13aaa24086d84 (diff)
Rename `RawPtr::to_option()` to `RawPtr::as_ref()`
As outlined in

  https://aturon.github.io/style/naming/conversions.html

`to_` functions names should only be used for expensive operations.
Thus `to_option` is better named `as_option`. Also, putting type
names into method names is considered bad style; what the user is
really trying to get is a reference. This `as_ref` is even better.

Also, we are missing a mutable version of this method. So add a
new trait `RawMutPtr` with a corresponding `as_mut` methode.

Finally, there is a bug in the signature of `to_option` which has
been around since lifetime elision: originally the returned reference
had 'static lifetime, but since the elision changes this become
the lifetime of the raw pointer (which does not make sense, since
the pointer lifetime and referent lifetime are unrelated). Fix
the bug to return a reference with a fresh lifetime (which will
be inferred from the calling context).

[breaking-change]
Diffstat (limited to 'src/libcoretest')
-rw-r--r--src/libcoretest/ptr.rs35
1 files changed, 30 insertions, 5 deletions
diff --git a/src/libcoretest/ptr.rs b/src/libcoretest/ptr.rs
index 9058ae56c45..754391a284d 100644
--- a/src/libcoretest/ptr.rs
+++ b/src/libcoretest/ptr.rs
@@ -102,19 +102,44 @@ fn test_is_null() {
 }
 
 #[test]
-fn test_to_option() {
+fn test_as_ref() {
     unsafe {
         let p: *const int = null();
-        assert_eq!(p.to_option(), None);
+        assert_eq!(p.as_ref(), None);
 
         let q: *const int = &2;
-        assert_eq!(q.to_option().unwrap(), &2);
+        assert_eq!(q.as_ref().unwrap(), &2);
 
         let p: *mut int = mut_null();
-        assert_eq!(p.to_option(), None);
+        assert_eq!(p.as_ref(), None);
 
         let q: *mut int = &mut 2;
-        assert_eq!(q.to_option().unwrap(), &2);
+        assert_eq!(q.as_ref().unwrap(), &2);
+
+        // Lifetime inference
+        let u = 2i;
+        {
+            let p: *const int = &u as *const _;
+            assert_eq!(p.as_ref().unwrap(), &2);
+        }
+    }
+}
+
+#[test]
+fn test_as_mut() {
+    unsafe {
+        let p: *mut int = mut_null();
+        assert!(p.as_mut() == None);
+
+        let q: *mut int = &mut 2;
+        assert!(q.as_mut().unwrap() == &mut 2);
+
+        // Lifetime inference
+        let mut u = 2i;
+        {
+            let p: *mut int = &mut u as *mut _;
+            assert!(p.as_mut().unwrap() == &mut 2);
+        }
     }
 }