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 { 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; } @@ -307,6 +306,14 @@ unsafe fn to_option(&self) -> Option<&T> { #[inline] fn offset(&self, count: int) -> *T { 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(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] @@ -347,6 +354,18 @@ unsafe fn to_option(&self) -> Option<&T> { #[inline] fn offset(&self, count: int) -> *mut T { mut_offset(*self, count) } + /// 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(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. 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 @@ fn iter(self) -> VecIterator<'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 @@ fn mut_iter(self) -> VecMutIterator<'self, 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 @@ fn next(&mut self) -> Option<$elem> { // 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 @@ fn next_back(&mut self) -> Option<$elem> { // 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)) }