about summary refs log tree commit diff
path: root/src/libcore/slice
diff options
context:
space:
mode:
authorScott McMurray <scottmcm@users.noreply.github.com>2018-03-31 22:35:37 -0700
committerScott McMurray <scottmcm@users.noreply.github.com>2018-03-31 22:35:37 -0700
commitb394165538bc52063f79a1820135cfefa19370e7 (patch)
treea5f7dd9904f273e68887449cd4fc6d755e37ec3e /src/libcore/slice
parent80785a547d29519dbdb5781437ec318fb210b980 (diff)
downloadrust-b394165538bc52063f79a1820135cfefa19370e7.tar.gz
rust-b394165538bc52063f79a1820135cfefa19370e7.zip
Deprecate offset_to; switch core&alloc to using offset_from instead
Bonus: might make code than uses `.len()` on slice iterators faster
Diffstat (limited to 'src/libcore/slice')
-rw-r--r--src/libcore/slice/mod.rs11
1 files changed, 6 insertions, 5 deletions
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<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
     }
 }