Skip to content

Commit 9afed64

Browse files
committed
Auto merge of #49551 - scottmcm:deprecate-offset_to, r=KodrAus
Deprecate offset_to; switch core&alloc to using offset_from instead Bonus: might make code than uses `.len()` on slice iterators faster cc #41079
2 parents 252a459 + b394165 commit 9afed64

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

src/liballoc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@
9999
#![feature(lang_items)]
100100
#![feature(needs_allocator)]
101101
#![feature(nonzero)]
102-
#![feature(offset_to)]
103102
#![feature(optin_builtin_traits)]
104103
#![feature(pattern)]
105104
#![feature(pin)]
106105
#![feature(ptr_internals)]
106+
#![feature(ptr_offset_from)]
107107
#![feature(rustc_attrs)]
108108
#![feature(slice_get_slice)]
109109
#![feature(slice_rsplit)]

src/liballoc/vec.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2394,9 +2394,10 @@ impl<T> Iterator for IntoIter<T> {
23942394

23952395
#[inline]
23962396
fn size_hint(&self) -> (usize, Option<usize>) {
2397-
let exact = match self.ptr.offset_to(self.end) {
2398-
Some(x) => x as usize,
2399-
None => (self.end as usize).wrapping_sub(self.ptr as usize),
2397+
let exact = if mem::size_of::<T>() == 0 {
2398+
(self.end as usize).wrapping_sub(self.ptr as usize)
2399+
} else {
2400+
unsafe { self.end.offset_from(self.ptr) as usize }
24002401
};
24012402
(exact, Some(exact))
24022403
}

src/libcore/ptr.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ impl<T: ?Sized> *const T {
677677
///
678678
/// ```
679679
/// #![feature(offset_to)]
680+
/// #![allow(deprecated)]
680681
///
681682
/// fn main() {
682683
/// let a = [0; 5];
@@ -689,14 +690,15 @@ impl<T: ?Sized> *const T {
689690
/// }
690691
/// ```
691692
#[unstable(feature = "offset_to", issue = "41079")]
693+
#[rustc_deprecated(since = "1.27.0", reason = "Replaced by `wrapping_offset_from`, with the \
694+
opposite argument order. If you're writing unsafe code, consider `offset_from`.")]
692695
#[inline]
693696
pub fn offset_to(self, other: *const T) -> Option<isize> where T: Sized {
694697
let size = mem::size_of::<T>();
695698
if size == 0 {
696699
None
697700
} else {
698-
let diff = (other as isize).wrapping_sub(self as isize);
699-
Some(diff / size as isize)
701+
Some(other.wrapping_offset_from(self))
700702
}
701703
}
702704

@@ -1442,6 +1444,7 @@ impl<T: ?Sized> *mut T {
14421444
///
14431445
/// ```
14441446
/// #![feature(offset_to)]
1447+
/// #![allow(deprecated)]
14451448
///
14461449
/// fn main() {
14471450
/// let mut a = [0; 5];
@@ -1454,14 +1457,15 @@ impl<T: ?Sized> *mut T {
14541457
/// }
14551458
/// ```
14561459
#[unstable(feature = "offset_to", issue = "41079")]
1460+
#[rustc_deprecated(since = "1.27.0", reason = "Replaced by `wrapping_offset_from`, with the \
1461+
opposite argument order. If you're writing unsafe code, consider `offset_from`.")]
14571462
#[inline]
14581463
pub fn offset_to(self, other: *const T) -> Option<isize> where T: Sized {
14591464
let size = mem::size_of::<T>();
14601465
if size == 0 {
14611466
None
14621467
} else {
1463-
let diff = (other as isize).wrapping_sub(self as isize);
1464-
Some(diff / size as isize)
1468+
Some(other.wrapping_offset_from(self))
14651469
}
14661470
}
14671471

src/libcore/slice/mod.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ macro_rules! iterator {
11851185

11861186
#[inline]
11871187
fn size_hint(&self) -> (usize, Option<usize>) {
1188-
let exact = ptrdistance(self.ptr, self.end);
1188+
let exact = unsafe { ptrdistance(self.ptr, self.end) };
11891189
(exact, Some(exact))
11901190
}
11911191

@@ -1593,10 +1593,11 @@ unsafe impl<'a, T> TrustedLen for IterMut<'a, T> {}
15931593
// Return the number of elements of `T` from `start` to `end`.
15941594
// Return the arithmetic difference if `T` is zero size.
15951595
#[inline(always)]
1596-
fn ptrdistance<T>(start: *const T, end: *const T) -> usize {
1597-
match start.offset_to(end) {
1598-
Some(x) => x as usize,
1599-
None => (end as usize).wrapping_sub(start as usize),
1596+
unsafe fn ptrdistance<T>(start: *const T, end: *const T) -> usize {
1597+
if mem::size_of::<T>() == 0 {
1598+
(end as usize).wrapping_sub(start as usize)
1599+
} else {
1600+
end.offset_from(start) as usize
16001601
}
16011602
}
16021603

0 commit comments

Comments
 (0)