about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorLuqman Aden <laden@csclub.uwaterloo.ca>2014-12-04 14:58:21 -0500
committerLuqman Aden <laden@csclub.uwaterloo.ca>2014-12-28 19:40:48 -0500
commit466135bfef4d110213a9aeb46f8199fa89a5f267 (patch)
tree381e70fefb4baaa32da847cd2d236dcc0c0e171a /src/libcore
parent4af50548b9ed283acb62768624a8cd942eabe964 (diff)
libcore: Make it unsafe to create NonZero and impl Deref.
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/ptr.rs24
1 files changed, 21 insertions, 3 deletions
diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs
index 910204edf70..23eb117680a 100644
--- a/src/libcore/ptr.rs
+++ b/src/libcore/ptr.rs
@@ -90,10 +90,10 @@
 use mem;
 use clone::Clone;
 use intrinsics;
-use kinds::Copy;
+use kinds::{Copy, Send, Sync};
+use ops::Deref;
 use option::Option;
 use option::Option::{Some, None};
-use kinds::{Send, Sync};
 
 use cmp::{PartialEq, Eq, Ord, PartialOrd, Equiv};
 use cmp::Ordering;
@@ -115,7 +115,25 @@ pub use intrinsics::set_memory;
 /// NULL or 0 that might allow certain optimizations.
 #[lang="non_zero"]
 #[deriving(Clone, PartialEq, Eq, PartialOrd)]
-pub struct NonZero<T>(pub T);
+#[experimental]
+pub struct NonZero<T>(T);
+
+impl<T> NonZero<T> {
+    /// Create an instance of NonZero with the provided value.
+    /// You must indeed ensure that the value is actually "non-zero".
+    #[inline(always)]
+    pub unsafe fn new(inner: T) -> NonZero<T> {
+        NonZero(inner)
+    }
+}
+
+impl<T> Deref<T> for NonZero<T> {
+    #[inline]
+    fn deref<'a>(&'a self) -> &'a T {
+        let NonZero(ref inner) = *self;
+        inner
+    }
+}
 
 impl<T: Copy> Copy for NonZero<T> {}