]> git.lizzy.rs Git - rust.git/commitdiff
Deprecate offset_to; switch core&alloc to using offset_from instead
authorScott McMurray <scottmcm@users.noreply.github.com>
Sun, 1 Apr 2018 05:35:37 +0000 (22:35 -0700)
committerScott McMurray <scottmcm@users.noreply.github.com>
Sun, 1 Apr 2018 05:35:37 +0000 (22:35 -0700)
Bonus: might make code than uses `.len()` on slice iterators faster

src/liballoc/lib.rs
src/liballoc/vec.rs
src/libcore/ptr.rs
src/libcore/slice/mod.rs

index e98b58994bfbb3bf5e63b23d83535e065b7e4011..009441e1680ddbbb1236d545c7acedd695c9a066 100644 (file)
 #![feature(lang_items)]
 #![feature(needs_allocator)]
 #![feature(nonzero)]
-#![feature(offset_to)]
 #![feature(optin_builtin_traits)]
 #![feature(pattern)]
 #![feature(pin)]
 #![feature(placement_in_syntax)]
 #![feature(placement_new_protocol)]
 #![feature(ptr_internals)]
+#![feature(ptr_offset_from)]
 #![feature(rustc_attrs)]
 #![feature(slice_get_slice)]
 #![feature(slice_rsplit)]
index 2eedb964f88ba4f4792b45c45b87935467d8a02b..d8ec956df8a7a164780fa0e1c4a8c3b7aa956276 100644 (file)
@@ -2338,9 +2338,10 @@ fn next(&mut self) -> Option<T> {
 
     #[inline]
     fn size_hint(&self) -> (usize, Option<usize>) {
-        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::<T>() == 0 {
+            (self.end as usize).wrapping_sub(self.ptr as usize)
+        } else {
+            unsafe { self.end.offset_from(self.ptr) as usize }
         };
         (exact, Some(exact))
     }
index 5a54de06b5ef2bf55cd99509d1233582a5996db4..c1e150e9fb909906e41d4b35835b64c4df4e8b47 100644 (file)
@@ -677,6 +677,7 @@ pub fn wrapping_offset(self, count: isize) -> *const T where T: Sized {
     ///
     /// ```
     /// #![feature(offset_to)]
+    /// #![allow(deprecated)]
     ///
     /// fn main() {
     ///     let a = [0; 5];
@@ -689,14 +690,15 @@ pub fn wrapping_offset(self, count: isize) -> *const T where T: Sized {
     /// }
     /// ```
     #[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<isize> where T: Sized {
         let size = mem::size_of::<T>();
         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 @@ pub unsafe fn as_mut<'a>(self) -> Option<&'a mut T> {
     ///
     /// ```
     /// #![feature(offset_to)]
+    /// #![allow(deprecated)]
     ///
     /// fn main() {
     ///     let mut a = [0; 5];
@@ -1454,14 +1457,15 @@ pub unsafe fn as_mut<'a>(self) -> Option<&'a 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<isize> where T: Sized {
         let size = mem::size_of::<T>();
         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))
         }
     }
 
index 0f1b7cb8fcc00ea34af430396864d97bb741ab9d..0a22028da81f4b3293015033eb8c8a535fdd497a 100644 (file)
@@ -1185,7 +1185,7 @@ fn next(&mut self) -> Option<$elem> {
 
             #[inline]
             fn size_hint(&self) -> (usize, Option<usize>) {
-                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<T>(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<T>(start: *const T, end: *const T) -> usize {
+    if mem::size_of::<T>() == 0 {
+        (end as usize).wrapping_sub(start as usize)
+    } else {
+        end.offset_from(start) as usize
     }
 }