Skip to content

Commit e76b3f3

Browse files
committed
Rename unsigned_offset_from to sub_ptr
1 parent 89a18cb commit e76b3f3

File tree

11 files changed

+46
-22
lines changed

11 files changed

+46
-22
lines changed

compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
721721
let diff_bytes = fx.bcx.ins().isub(ptr, base);
722722
// FIXME this can be an exact division.
723723
let diff = if intrinsic == sym::ptr_offset_from_unsigned {
724-
// Because diff_bytes ULT isize::MAX, this would be fine as signed,
724+
// Because diff_bytes ULE isize::MAX, this would be fine as signed,
725725
// but unsigned is slightly easier to codegen, so might as well.
726726
fx.bcx.ins().udiv_imm(diff_bytes, pointee_size as i64)
727727
} else {

library/alloc/src/slice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ where
10561056
fn drop(&mut self) {
10571057
// `T` is not a zero-sized type, and these are pointers into a slice's elements.
10581058
unsafe {
1059-
let len = self.end.unsigned_offset_from(self.start);
1059+
let len = self.end.sub_ptr(self.start);
10601060
ptr::copy_nonoverlapping(self.start, self.dest, len);
10611061
}
10621062
}

library/alloc/src/vec/drain.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> {
163163
// it from the original vec but also avoid creating a &mut to the front since that could
164164
// invalidate raw pointers to it which some unsafe code might rely on.
165165
let vec_ptr = vec.as_mut().as_mut_ptr();
166-
let drop_offset = drop_ptr.unsigned_offset_from(vec_ptr);
166+
let drop_offset = drop_ptr.sub_ptr(vec_ptr);
167167
let to_drop = ptr::slice_from_raw_parts_mut(vec_ptr.add(drop_offset), drop_len);
168168
ptr::drop_in_place(to_drop);
169169
}

library/alloc/src/vec/in_place_collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ where
250250
let sink =
251251
self.try_fold::<_, _, Result<_, !>>(sink, write_in_place_with_drop(end)).unwrap();
252252
// iteration succeeded, don't drop head
253-
unsafe { ManuallyDrop::new(sink).dst.unsigned_offset_from(dst_buf) }
253+
unsafe { ManuallyDrop::new(sink).dst.sub_ptr(dst_buf) }
254254
}
255255
}
256256

library/alloc/src/vec/in_place_drop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub(super) struct InPlaceDrop<T> {
1010

1111
impl<T> InPlaceDrop<T> {
1212
fn len(&self) -> usize {
13-
unsafe { self.dst.unsigned_offset_from(self.inner) }
13+
unsafe { self.dst.sub_ptr(self.inner) }
1414
}
1515
}
1616

library/alloc/src/vec/into_iter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
169169
let exact = if mem::size_of::<T>() == 0 {
170170
self.end.addr().wrapping_sub(self.ptr.addr())
171171
} else {
172-
unsafe { self.end.unsigned_offset_from(self.ptr) }
172+
unsafe { self.end.sub_ptr(self.ptr) }
173173
};
174174
(exact, Some(exact))
175175
}

library/core/src/intrinsics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1903,7 +1903,7 @@ extern "rust-intrinsic" {
19031903
#[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")]
19041904
pub fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
19051905

1906-
/// See documentation of `<*const T>::unsigned_offset_from` for details.
1906+
/// See documentation of `<*const T>::sub_ptr` for details.
19071907
#[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")]
19081908
#[cfg(not(bootstrap))]
19091909
pub fn ptr_offset_from_unsigned<T>(ptr: *const T, base: *const T) -> usize;

library/core/src/ptr/const_ptr.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,20 @@ impl<T: ?Sized> *const T {
622622
/// but it provides slightly more information to the optimizer, which can
623623
/// sometimes allow it to optimize slightly better with some backends.
624624
///
625-
/// This method is the inverse of [`add`](#method.add) (and, with the parameters
626-
/// in the other order, of [`sub`](#method.sub)).
625+
/// This method can be though of as recovering the `count` that was passed
626+
/// to [`add`](#method.add) (or, with the parameters in the other order,
627+
/// to [`sub`](#method.sub)). The following are all equivalent, assuming
628+
/// that their safety preconditions are met:
629+
/// ```rust
630+
/// # #![feature(ptr_unsigned_offset_from)]
631+
/// # unsafe fn blah(ptr: *const i32, origin: *const i32, count: usize) -> bool {
632+
/// ptr.sub_ptr(origin) == count
633+
/// # &&
634+
/// origin.add(count) == ptr
635+
/// # &&
636+
/// ptr.sub(count) == origin
637+
/// # }
638+
/// ```
627639
///
628640
/// # Safety
629641
///
@@ -650,10 +662,10 @@ impl<T: ?Sized> *const T {
650662
/// let ptr1: *const i32 = &a[1];
651663
/// let ptr2: *const i32 = &a[3];
652664
/// unsafe {
653-
/// assert_eq!(ptr2.unsigned_offset_from(ptr1), 2);
665+
/// assert_eq!(ptr2.sub_ptr(ptr1), 2);
654666
/// assert_eq!(ptr1.add(2), ptr2);
655667
/// assert_eq!(ptr2.sub(2), ptr1);
656-
/// assert_eq!(ptr2.unsigned_offset_from(ptr2), 0);
668+
/// assert_eq!(ptr2.sub_ptr(ptr2), 0);
657669
/// }
658670
///
659671
/// // This would be incorrect, as the pointers are not correctly ordered:
@@ -662,7 +674,7 @@ impl<T: ?Sized> *const T {
662674
#[unstable(feature = "ptr_unsigned_offset_from", issue = "88888888")]
663675
#[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")]
664676
#[inline]
665-
pub const unsafe fn unsigned_offset_from(self, origin: *const T) -> usize
677+
pub const unsafe fn sub_ptr(self, origin: *const T) -> usize
666678
where
667679
T: Sized,
668680
{

library/core/src/ptr/mut_ptr.rs

+19-7
Original file line numberDiff line numberDiff line change
@@ -798,8 +798,20 @@ impl<T: ?Sized> *mut T {
798798
/// but it provides slightly more information to the optimizer, which can
799799
/// sometimes allow it to optimize slightly better with some backends.
800800
///
801-
/// This method is the inverse of [`add`](#method.add) (and, with the parameters
802-
/// in the other order, of [`sub`](#method.sub)).
801+
/// This method can be though of as recovering the `count` that was passed
802+
/// to [`add`](#method.add) (or, with the parameters in the other order,
803+
/// to [`sub`](#method.sub)). The following are all equivalent, assuming
804+
/// that their safety preconditions are met:
805+
/// ```rust
806+
/// # #![feature(ptr_unsigned_offset_from)]
807+
/// # unsafe fn blah(ptr: *mut i32, origin: *mut i32, count: usize) -> bool {
808+
/// ptr.sub_ptr(origin) == count
809+
/// # &&
810+
/// origin.add(count) == ptr
811+
/// # &&
812+
/// ptr.sub(count) == origin
813+
/// # }
814+
/// ```
803815
///
804816
/// # Safety
805817
///
@@ -828,23 +840,23 @@ impl<T: ?Sized> *mut T {
828840
/// let ptr1: *mut i32 = p.add(1);
829841
/// let ptr2: *mut i32 = p.add(3);
830842
///
831-
/// assert_eq!(ptr2.unsigned_offset_from(ptr1), 2);
843+
/// assert_eq!(ptr2.sub_ptr(ptr1), 2);
832844
/// assert_eq!(ptr1.add(2), ptr2);
833845
/// assert_eq!(ptr2.sub(2), ptr1);
834-
/// assert_eq!(ptr2.unsigned_offset_from(ptr2), 0);
846+
/// assert_eq!(ptr2.sub_ptr(ptr2), 0);
835847
/// }
836848
///
837849
/// // This would be incorrect, as the pointers are not correctly ordered:
838850
/// // ptr1.offset_from(ptr2)
839851
#[unstable(feature = "ptr_unsigned_offset_from", issue = "88888888")]
840852
#[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")]
841853
#[inline]
842-
pub const unsafe fn unsigned_offset_from(self, origin: *const T) -> usize
854+
pub const unsafe fn sub_ptr(self, origin: *const T) -> usize
843855
where
844856
T: Sized,
845857
{
846-
// SAFETY: the caller must uphold the safety contract for `unsigned_offset_from`.
847-
unsafe { (self as *const T).unsigned_offset_from(origin) }
858+
// SAFETY: the caller must uphold the safety contract for `sub_ptr`.
859+
unsafe { (self as *const T).sub_ptr(origin) }
848860
}
849861

850862
/// Calculates the offset from a pointer (convenience for `.offset(count as isize)`).

library/core/src/slice/raw.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ pub const fn from_mut<T>(s: &mut T) -> &mut [T] {
215215
#[unstable(feature = "slice_from_ptr_range", issue = "89792")]
216216
pub unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
217217
// SAFETY: the caller must uphold the safety contract for `from_ptr_range`.
218-
unsafe { from_raw_parts(range.start, range.end.unsigned_offset_from(range.start)) }
218+
unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) }
219219
}
220220

221221
/// Performs the same functionality as [`from_ptr_range`], except that a
@@ -265,5 +265,5 @@ pub unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
265265
#[unstable(feature = "slice_from_ptr_range", issue = "89792")]
266266
pub unsafe fn from_mut_ptr_range<'a, T>(range: Range<*mut T>) -> &'a mut [T] {
267267
// SAFETY: the caller must uphold the safety contract for `from_mut_ptr_range`.
268-
unsafe { from_raw_parts_mut(range.start, range.end.unsigned_offset_from(range.start)) }
268+
unsafe { from_raw_parts_mut(range.start, range.end.sub_ptr(range.start)) }
269269
}

src/test/ui/consts/offset_from.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub const OFFSET_EQUAL_INTS: isize = {
4747
pub const OFFSET_UNSIGNED: usize = {
4848
let a = ['a', 'b', 'c'];
4949
let ptr = a.as_ptr();
50-
unsafe { ptr.add(2).unsigned_offset_from(ptr) }
50+
unsafe { ptr.add(2).sub_ptr(ptr) }
5151
};
5252

5353
fn main() {

0 commit comments

Comments
 (0)