diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index 69fc007ab7c..5ca39442342 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -99,11 +99,11 @@ #![feature(lang_items)] #![feature(needs_allocator)] #![feature(nonzero)] -#![feature(offset_to)] #![feature(optin_builtin_traits)] #![feature(pattern)] #![feature(pin)] #![feature(ptr_internals)] +#![feature(ptr_offset_from)] #![feature(rustc_attrs)] #![feature(slice_get_slice)] #![feature(slice_rsplit)] diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 4b883b5bce7..0f74743ca49 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -2394,9 +2394,10 @@ impl Iterator for IntoIter { #[inline] fn size_hint(&self) -> (usize, Option) { - let exact = match self.ptr.offset_to(self.end) { - Some(x) => x as usize, - None => (self.end as usize).wrapping_sub(self.ptr as usize), + let exact = if mem::size_of::() == 0 { + (self.end as usize).wrapping_sub(self.ptr as usize) + } else { + unsafe { self.end.offset_from(self.ptr) as usize } }; (exact, Some(exact)) } diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 5a54de06b5e..c1e150e9fb9 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -677,6 +677,7 @@ impl *const T { /// /// ``` /// #![feature(offset_to)] + /// #![allow(deprecated)] /// /// fn main() { /// let a = [0; 5]; @@ -689,14 +690,15 @@ impl *const T { /// } /// ``` #[unstable(feature = "offset_to", issue = "41079")] + #[rustc_deprecated(since = "1.27.0", reason = "Replaced by `wrapping_offset_from`, with the \ + opposite argument order. If you're writing unsafe code, consider `offset_from`.")] #[inline] pub fn offset_to(self, other: *const T) -> Option where T: Sized { let size = mem::size_of::(); if size == 0 { None } else { - let diff = (other as isize).wrapping_sub(self as isize); - Some(diff / size as isize) + Some(other.wrapping_offset_from(self)) } } @@ -1442,6 +1444,7 @@ impl *mut T { /// /// ``` /// #![feature(offset_to)] + /// #![allow(deprecated)] /// /// fn main() { /// let mut a = [0; 5]; @@ -1454,14 +1457,15 @@ impl *mut T { /// } /// ``` #[unstable(feature = "offset_to", issue = "41079")] + #[rustc_deprecated(since = "1.27.0", reason = "Replaced by `wrapping_offset_from`, with the \ + opposite argument order. If you're writing unsafe code, consider `offset_from`.")] #[inline] pub fn offset_to(self, other: *const T) -> Option where T: Sized { let size = mem::size_of::(); if size == 0 { None } else { - let diff = (other as isize).wrapping_sub(self as isize); - Some(diff / size as isize) + Some(other.wrapping_offset_from(self)) } } diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index 0f1b7cb8fcc..0a22028da81 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -1185,7 +1185,7 @@ macro_rules! iterator { #[inline] fn size_hint(&self) -> (usize, Option) { - let exact = ptrdistance(self.ptr, self.end); + let exact = unsafe { ptrdistance(self.ptr, self.end) }; (exact, Some(exact)) } @@ -1593,10 +1593,11 @@ unsafe impl<'a, T> TrustedLen for IterMut<'a, T> {} // Return the number of elements of `T` from `start` to `end`. // Return the arithmetic difference if `T` is zero size. #[inline(always)] -fn ptrdistance(start: *const T, end: *const T) -> usize { - match start.offset_to(end) { - Some(x) => x as usize, - None => (end as usize).wrapping_sub(start as usize), +unsafe fn ptrdistance(start: *const T, end: *const T) -> usize { + if mem::size_of::() == 0 { + (end as usize).wrapping_sub(start as usize) + } else { + end.offset_from(start) as usize } }