Skip to content

Commit 7611e42

Browse files
committed
core: Add ptrdistance to slice module
1 parent c1db77e commit 7611e42

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/libcore/slice.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -833,9 +833,7 @@ macro_rules! iterator {
833833

834834
#[inline]
835835
fn size_hint(&self) -> (usize, Option<usize>) {
836-
let diff = (self.end as usize).wrapping_sub(self.ptr as usize);
837-
let size = mem::size_of::<T>();
838-
let exact = diff / (if size == 0 {1} else {size});
836+
let exact = ptrdistance(self.ptr, self.end);
839837
(exact, Some(exact))
840838
}
841839

@@ -1127,6 +1125,15 @@ impl<'a, T> FusedIterator for IterMut<'a, T> {}
11271125
unsafe impl<'a, T> TrustedLen for IterMut<'a, T> {}
11281126

11291127

1128+
// Return the number of elements of `T` from `start` to `end`.
1129+
// Return the arithmetic difference if `T` is zero size.
1130+
#[inline(always)]
1131+
fn ptrdistance<T>(start: *const T, end: *const T) -> usize {
1132+
let diff = (end as usize).wrapping_sub(start as usize);
1133+
let size = mem::size_of::<T>();
1134+
diff / (if size == 0 { 1 } else { size })
1135+
}
1136+
11301137
// Extension methods for raw pointers, used by the iterators
11311138
trait PointerExt : Copy {
11321139
unsafe fn slice_offset(self, i: isize) -> Self;

0 commit comments

Comments
 (0)