about summary refs log tree commit diff
path: root/src/libcore/slice
diff options
context:
space:
mode:
authorAmanieu d'Antras <amanieu@gmail.com>2017-03-31 13:52:46 +0100
committerAmanieu d'Antras <amanieu@gmail.com>2017-04-03 01:36:56 +0100
commit7b89bd7ccacd0908d7e22a5cf383c8cc147bc3d5 (patch)
treef07ae40b18015742cbd15859b22ec37f5b4d9aa3 /src/libcore/slice
parentc82f1325cfb5f3fd5028225f692cb134517016f1 (diff)
downloadrust-7b89bd7ccacd0908d7e22a5cf383c8cc147bc3d5.tar.gz
rust-7b89bd7ccacd0908d7e22a5cf383c8cc147bc3d5.zip
Add ptr::offset_to
Diffstat (limited to 'src/libcore/slice')
-rw-r--r--src/libcore/slice/mod.rs7
1 files changed, 4 insertions, 3 deletions
diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs
index af492b3c639..5a978ccc741 100644
--- a/src/libcore/slice/mod.rs
+++ b/src/libcore/slice/mod.rs
@@ -1502,9 +1502,10 @@ unsafe impl<'a, T> TrustedLen for IterMut<'a, T> {}
 // Return the arithmetic difference if `T` is zero size.
 #[inline(always)]
 fn ptrdistance<T>(start: *const T, end: *const T) -> usize {
-    let diff = (end as usize).wrapping_sub(start as usize);
-    let size = mem::size_of::<T>();
-    diff / (if size == 0 { 1 } else { size })
+    match start.offset_to(end) {
+        Some(x) => x as usize,
+        None => (end as usize).wrapping_sub(start as usize),
+    }
 }
 
 // Extension methods for raw pointers, used by the iterators