about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLuqman Aden <laden@csclub.uwaterloo.ca>2014-12-03 17:21:51 -0500
committerLuqman Aden <laden@csclub.uwaterloo.ca>2014-12-28 19:40:47 -0500
commitef5da14edb09d6425b1b6576d418e12c268ddf17 (patch)
tree4efcc9b980e199e82c20b1d00d7e8d7c736decc9
parent46e73764896316ef1384591656cfca01280c5e5c (diff)
downloadrust-ef5da14edb09d6425b1b6576d418e12c268ddf17.tar.gz
rust-ef5da14edb09d6425b1b6576d418e12c268ddf17.zip
libcore: Add NonZero lang item and implement some methods.
-rw-r--r--src/libcore/ptr.rs70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs
index 8c9d77a0e9c..910204edf70 100644
--- a/src/libcore/ptr.rs
+++ b/src/libcore/ptr.rs
@@ -90,6 +90,7 @@
 use mem;
 use clone::Clone;
 use intrinsics;
+use kinds::Copy;
 use option::Option;
 use option::Option::{Some, None};
 use kinds::{Send, Sync};
@@ -109,6 +110,15 @@ pub use intrinsics::copy_memory;
 #[experimental = "uncertain about naming and semantics"]
 pub use intrinsics::set_memory;
 
+
+/// A wrapper type for raw pointers and integers that will never be
+/// NULL or 0 that might allow certain optimizations.
+#[lang="non_zero"]
+#[deriving(Clone, PartialEq, Eq, PartialOrd)]
+pub struct NonZero<T>(pub T);
+
+impl<T: Copy> Copy for NonZero<T> {}
+
 /// Creates a null raw pointer.
 ///
 /// # Examples
@@ -313,6 +323,32 @@ impl<T> RawPtr<T> for *const T {
     }
 }
 
+impl<T> RawPtr<T> for NonZero<*const T> {
+    #[inline]
+    fn null() -> NonZero<*const T> { NonZero(null()) }
+
+    #[inline]
+    fn is_null(&self) -> bool { false }
+
+    #[inline]
+    fn to_uint(&self) -> uint {
+        let NonZero(p) = *self;
+        p as uint
+    }
+
+    #[inline]
+    unsafe fn offset(self, count: int) -> NonZero<*const T> {
+        let NonZero(p) = self;
+        NonZero(intrinsics::offset(p, count))
+    }
+
+    #[inline]
+    unsafe fn as_ref<'a>(&self) -> Option<&'a T> {
+        let NonZero(p) = *self;
+        Some(&*p)
+    }
+}
+
 impl<T> RawPtr<T> for *mut T {
     #[inline]
     fn null() -> *mut T { null_mut() }
@@ -338,6 +374,32 @@ impl<T> RawPtr<T> for *mut T {
     }
 }
 
+impl<T> RawPtr<T> for NonZero<*mut T> {
+    #[inline]
+    fn null() -> NonZero<*mut T> { NonZero(null_mut()) }
+
+    #[inline]
+    fn is_null(&self) -> bool { false }
+
+    #[inline]
+    fn to_uint(&self) -> uint {
+        let NonZero(p) = *self;
+        p as uint
+    }
+
+    #[inline]
+    unsafe fn offset(self, count: int) -> NonZero<*mut T> {
+        let NonZero(p) = self;
+        NonZero(intrinsics::offset(p as *const T, count) as *mut T)
+    }
+
+    #[inline]
+    unsafe fn as_ref<'a>(&self) -> Option<&'a T> {
+        let NonZero(p) = *self;
+        Some(&*p)
+    }
+}
+
 impl<T> RawMutPtr<T> for *mut T {
     #[inline]
     unsafe fn as_mut<'a>(&self) -> Option<&'a mut T> {
@@ -349,6 +411,14 @@ impl<T> RawMutPtr<T> for *mut T {
     }
 }
 
+impl<T> RawMutPtr<T> for NonZero<*mut T> {
+    #[inline]
+    unsafe fn as_mut<'a>(&self) -> Option<&'a mut T> {
+        let NonZero(p) = *self;
+        Some(&mut *p)
+    }
+}
+
 // Equality for pointers
 impl<T> PartialEq for *const T {
     #[inline]