about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/libstd/ptr.rs21
-rw-r--r--src/libstd/vec.rs8
2 files changed, 24 insertions, 5 deletions
diff --git a/src/libstd/ptr.rs b/src/libstd/ptr.rs
index 98fb132672c..5a2bd0c4de9 100644
--- a/src/libstd/ptr.rs
+++ b/src/libstd/ptr.rs
@@ -272,7 +272,6 @@ pub trait RawPtr<T> {
     fn is_not_null(&self) -> bool;
     unsafe fn to_option(&self) -> Option<&T>;
     fn offset(&self, count: int) -> Self;
-    #[cfg(not(stage0))]
     unsafe fn offset_inbounds(self, count: int) -> Self;
 }
 
@@ -310,6 +309,14 @@ impl<T> RawPtr<T> for *T {
     /// Calculates the offset from a pointer. The offset *must* be in-bounds of
     /// the object, or one-byte-past-the-end.
     #[inline]
+    #[cfg(stage0)]
+    unsafe fn offset_inbounds(self, count: int) -> *T {
+        intrinsics::offset(self, count)
+    }
+
+    /// Calculates the offset from a pointer. The offset *must* be in-bounds of
+    /// the object, or one-byte-past-the-end.
+    #[inline]
     #[cfg(not(stage0))]
     unsafe fn offset_inbounds(self, count: int) -> *T {
         intrinsics::offset_inbounds(self, count)
@@ -354,6 +361,18 @@ impl<T> RawPtr<T> for *mut T {
     /// This method should be preferred over `offset` when the guarantee can be
     /// satisfied, to enable better optimization.
     #[inline]
+    #[cfg(stage0)]
+    unsafe fn offset_inbounds(self, count: int) -> *mut T {
+        intrinsics::offset(self as *T, count) as *mut T
+    }
+
+    /// Calculates the offset from a pointer. The offset *must* be in-bounds of
+    /// the object, or one-byte-past-the-end. An arithmetic overflow is also
+    /// undefined behaviour.
+    ///
+    /// This method should be preferred over `offset` when the guarantee can be
+    /// satisfied, to enable better optimization.
+    #[inline]
     #[cfg(not(stage0))]
     unsafe fn offset_inbounds(self, count: int) -> *mut T {
         intrinsics::offset_inbounds(self as *T, count) as *mut T
diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs
index 8dbfb3ec543..36201dc5e82 100644
--- a/src/libstd/vec.rs
+++ b/src/libstd/vec.rs
@@ -855,7 +855,7 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
                             lifetime: cast::transmute(p)}
             } else {
                 VecIterator{ptr: p,
-                            end: p.offset(self.len() as int),
+                            end: p.offset_inbounds(self.len() as int),
                             lifetime: cast::transmute(p)}
             }
         }
@@ -1837,7 +1837,7 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
                                lifetime: cast::transmute(p)}
             } else {
                 VecMutIterator{ptr: p,
-                               end: p.offset(self.len() as int),
+                               end: p.offset_inbounds(self.len() as int),
                                lifetime: cast::transmute(p)}
             }
         }
@@ -2193,7 +2193,7 @@ macro_rules! iterator {
                             // same pointer.
                             cast::transmute(self.ptr as uint + 1)
                         } else {
-                            self.ptr.offset(1)
+                            self.ptr.offset_inbounds(1)
                         };
 
                         Some(cast::transmute(old))
@@ -2225,7 +2225,7 @@ macro_rules! double_ended_iterator {
                             // See above for why 'ptr.offset' isn't used
                             cast::transmute(self.end as uint - 1)
                         } else {
-                            self.end.offset(-1)
+                            self.end.offset_inbounds(-1)
                         };
                         Some(cast::transmute(self.end))
                     }