diff options
| author | Scott McMurray <scottmcm@users.noreply.github.com> | 2018-03-31 22:35:37 -0700 |
|---|---|---|
| committer | Scott McMurray <scottmcm@users.noreply.github.com> | 2018-03-31 22:35:37 -0700 |
| commit | b394165538bc52063f79a1820135cfefa19370e7 (patch) | |
| tree | a5f7dd9904f273e68887449cd4fc6d755e37ec3e /src/libcore/slice | |
| parent | 80785a547d29519dbdb5781437ec318fb210b980 (diff) | |
| download | rust-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.rs | 11 |
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 } } |
