Skip to content

Commit c7caf81

Browse files
tgross35gitbot
authored and
gitbot
committed
Rollup merge of rust-lang#137483 - bend-n:😅, r=Noratrieb
rename sub_ptr to offset_from_unsigned i also made `byte_sub_ptr` `byte_offset_from_unsigned` fixes rust-lang#137121 tracking issue rust-lang#95892
2 parents b468e6f + 5d96c0e commit c7caf81

File tree

12 files changed

+38
-38
lines changed

12 files changed

+38
-38
lines changed

‎alloc/src/vec/drain.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> {
232232
// it from the original vec but also avoid creating a &mut to the front since that could
233233
// invalidate raw pointers to it which some unsafe code might rely on.
234234
let vec_ptr = vec.as_mut().as_mut_ptr();
235-
let drop_offset = drop_ptr.sub_ptr(vec_ptr);
235+
let drop_offset = drop_ptr.offset_from_unsigned(vec_ptr);
236236
let to_drop = ptr::slice_from_raw_parts_mut(vec_ptr.add(drop_offset), drop_len);
237237
ptr::drop_in_place(to_drop);
238238
}

‎alloc/src/vec/in_place_collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ where
379379
let sink =
380380
self.try_fold::<_, _, Result<_, !>>(sink, write_in_place_with_drop(end)).into_ok();
381381
// iteration succeeded, don't drop head
382-
unsafe { ManuallyDrop::new(sink).dst.sub_ptr(dst_buf) }
382+
unsafe { ManuallyDrop::new(sink).dst.offset_from_unsigned(dst_buf) }
383383
}
384384
}
385385

‎alloc/src/vec/in_place_drop.rs

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

1515
impl<T> InPlaceDrop<T> {
1616
fn len(&self) -> usize {
17-
unsafe { self.dst.sub_ptr(self.inner) }
17+
unsafe { self.dst.offset_from_unsigned(self.inner) }
1818
}
1919
}
2020

‎alloc/src/vec/into_iter.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl<T, A: Allocator> IntoIter<T, A> {
179179
// say that they're all at the beginning of the "allocation".
180180
0..this.len()
181181
} else {
182-
this.ptr.sub_ptr(this.buf)..this.end.sub_ptr(buf)
182+
this.ptr.offset_from_unsigned(this.buf)..this.end.offset_from_unsigned(buf)
183183
};
184184
let cap = this.cap;
185185
let alloc = ManuallyDrop::take(&mut this.alloc);
@@ -230,7 +230,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
230230
let exact = if T::IS_ZST {
231231
self.end.addr().wrapping_sub(self.ptr.as_ptr().addr())
232232
} else {
233-
unsafe { non_null!(self.end, T).sub_ptr(self.ptr) }
233+
unsafe { non_null!(self.end, T).offset_from_unsigned(self.ptr) }
234234
};
235235
(exact, Some(exact))
236236
}

‎core/src/ptr/const_ptr.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ impl<T: ?Sized> *const T {
724724
/// that their safety preconditions are met:
725725
/// ```rust
726726
/// # unsafe fn blah(ptr: *const i32, origin: *const i32, count: usize) -> bool { unsafe {
727-
/// ptr.sub_ptr(origin) == count
727+
/// ptr.offset_from_unsigned(origin) == count
728728
/// # &&
729729
/// origin.add(count) == ptr
730730
/// # &&
@@ -755,20 +755,20 @@ impl<T: ?Sized> *const T {
755755
/// let ptr1: *const i32 = &a[1];
756756
/// let ptr2: *const i32 = &a[3];
757757
/// unsafe {
758-
/// assert_eq!(ptr2.sub_ptr(ptr1), 2);
758+
/// assert_eq!(ptr2.offset_from_unsigned(ptr1), 2);
759759
/// assert_eq!(ptr1.add(2), ptr2);
760760
/// assert_eq!(ptr2.sub(2), ptr1);
761-
/// assert_eq!(ptr2.sub_ptr(ptr2), 0);
761+
/// assert_eq!(ptr2.offset_from_unsigned(ptr2), 0);
762762
/// }
763763
///
764764
/// // This would be incorrect, as the pointers are not correctly ordered:
765-
/// // ptr1.sub_ptr(ptr2)
765+
/// // ptr1.offset_from_unsigned(ptr2)
766766
/// ```
767767
#[stable(feature = "ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
768768
#[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
769769
#[inline]
770770
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
771-
pub const unsafe fn sub_ptr(self, origin: *const T) -> usize
771+
pub const unsafe fn offset_from_unsigned(self, origin: *const T) -> usize
772772
where
773773
T: Sized,
774774
{
@@ -786,7 +786,7 @@ impl<T: ?Sized> *const T {
786786

787787
ub_checks::assert_unsafe_precondition!(
788788
check_language_ub,
789-
"ptr::sub_ptr requires `self >= origin`",
789+
"ptr::offset_from_unsigned requires `self >= origin`",
790790
(
791791
this: *const () = self as *const (),
792792
origin: *const () = origin as *const (),
@@ -804,7 +804,7 @@ impl<T: ?Sized> *const T {
804804
/// units of **bytes**.
805805
///
806806
/// This is purely a convenience for casting to a `u8` pointer and
807-
/// using [`sub_ptr`][pointer::sub_ptr] on it. See that method for
807+
/// using [`sub_ptr`][pointer::offset_from_unsigned] on it. See that method for
808808
/// documentation and safety requirements.
809809
///
810810
/// For non-`Sized` pointees this operation considers only the data pointers,
@@ -813,9 +813,9 @@ impl<T: ?Sized> *const T {
813813
#[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
814814
#[inline]
815815
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
816-
pub const unsafe fn byte_sub_ptr<U: ?Sized>(self, origin: *const U) -> usize {
816+
pub const unsafe fn byte_offset_from_unsigned<U: ?Sized>(self, origin: *const U) -> usize {
817817
// SAFETY: the caller must uphold the safety contract for `sub_ptr`.
818-
unsafe { self.cast::<u8>().sub_ptr(origin.cast::<u8>()) }
818+
unsafe { self.cast::<u8>().offset_from_unsigned(origin.cast::<u8>()) }
819819
}
820820

821821
/// Returns whether two pointers are guaranteed to be equal.

‎core/src/ptr/mut_ptr.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ impl<T: ?Sized> *mut T {
896896
/// that their safety preconditions are met:
897897
/// ```rust
898898
/// # unsafe fn blah(ptr: *mut i32, origin: *mut i32, count: usize) -> bool { unsafe {
899-
/// ptr.sub_ptr(origin) == count
899+
/// ptr.offset_from_unsigned(origin) == count
900900
/// # &&
901901
/// origin.add(count) == ptr
902902
/// # &&
@@ -929,10 +929,10 @@ impl<T: ?Sized> *mut T {
929929
/// let ptr1: *mut i32 = p.add(1);
930930
/// let ptr2: *mut i32 = p.add(3);
931931
///
932-
/// assert_eq!(ptr2.sub_ptr(ptr1), 2);
932+
/// assert_eq!(ptr2.offset_from_unsigned(ptr1), 2);
933933
/// assert_eq!(ptr1.add(2), ptr2);
934934
/// assert_eq!(ptr2.sub(2), ptr1);
935-
/// assert_eq!(ptr2.sub_ptr(ptr2), 0);
935+
/// assert_eq!(ptr2.offset_from_unsigned(ptr2), 0);
936936
/// }
937937
///
938938
/// // This would be incorrect, as the pointers are not correctly ordered:
@@ -941,20 +941,20 @@ impl<T: ?Sized> *mut T {
941941
#[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
942942
#[inline]
943943
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
944-
pub const unsafe fn sub_ptr(self, origin: *const T) -> usize
944+
pub const unsafe fn offset_from_unsigned(self, origin: *const T) -> usize
945945
where
946946
T: Sized,
947947
{
948948
// SAFETY: the caller must uphold the safety contract for `sub_ptr`.
949-
unsafe { (self as *const T).sub_ptr(origin) }
949+
unsafe { (self as *const T).offset_from_unsigned(origin) }
950950
}
951951

952952
/// Calculates the distance between two pointers within the same allocation, *where it's known that
953953
/// `self` is equal to or greater than `origin`*. The returned value is in
954954
/// units of **bytes**.
955955
///
956956
/// This is purely a convenience for casting to a `u8` pointer and
957-
/// using [`sub_ptr`][pointer::sub_ptr] on it. See that method for
957+
/// using [`sub_ptr`][pointer::offset_from_unsigned] on it. See that method for
958958
/// documentation and safety requirements.
959959
///
960960
/// For non-`Sized` pointees this operation considers only the data pointers,
@@ -963,9 +963,9 @@ impl<T: ?Sized> *mut T {
963963
#[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
964964
#[inline]
965965
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
966-
pub const unsafe fn byte_sub_ptr<U: ?Sized>(self, origin: *mut U) -> usize {
966+
pub const unsafe fn byte_offset_from_unsigned<U: ?Sized>(self, origin: *mut U) -> usize {
967967
// SAFETY: the caller must uphold the safety contract for `byte_sub_ptr`.
968-
unsafe { (self as *const T).byte_sub_ptr(origin) }
968+
unsafe { (self as *const T).byte_offset_from_unsigned(origin) }
969969
}
970970

971971
/// Adds an unsigned offset to a pointer.

‎core/src/ptr/non_null.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ impl<T: ?Sized> NonNull<T> {
857857
/// that their safety preconditions are met:
858858
/// ```rust
859859
/// # unsafe fn blah(ptr: std::ptr::NonNull<u32>, origin: std::ptr::NonNull<u32>, count: usize) -> bool { unsafe {
860-
/// ptr.sub_ptr(origin) == count
860+
/// ptr.offset_from_unsigned(origin) == count
861861
/// # &&
862862
/// origin.add(count) == ptr
863863
/// # &&
@@ -890,33 +890,33 @@ impl<T: ?Sized> NonNull<T> {
890890
/// let ptr1: NonNull<u32> = NonNull::from(&a[1]);
891891
/// let ptr2: NonNull<u32> = NonNull::from(&a[3]);
892892
/// unsafe {
893-
/// assert_eq!(ptr2.sub_ptr(ptr1), 2);
893+
/// assert_eq!(ptr2.offset_from_unsigned(ptr1), 2);
894894
/// assert_eq!(ptr1.add(2), ptr2);
895895
/// assert_eq!(ptr2.sub(2), ptr1);
896-
/// assert_eq!(ptr2.sub_ptr(ptr2), 0);
896+
/// assert_eq!(ptr2.offset_from_unsigned(ptr2), 0);
897897
/// }
898898
///
899899
/// // This would be incorrect, as the pointers are not correctly ordered:
900-
/// // ptr1.sub_ptr(ptr2)
900+
/// // ptr1.offset_from_unsigned(ptr2)
901901
/// ```
902902
#[inline]
903903
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
904904
#[stable(feature = "ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
905905
#[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
906-
pub const unsafe fn sub_ptr(self, subtracted: NonNull<T>) -> usize
906+
pub const unsafe fn offset_from_unsigned(self, subtracted: NonNull<T>) -> usize
907907
where
908908
T: Sized,
909909
{
910910
// SAFETY: the caller must uphold the safety contract for `sub_ptr`.
911-
unsafe { self.as_ptr().sub_ptr(subtracted.as_ptr()) }
911+
unsafe { self.as_ptr().offset_from_unsigned(subtracted.as_ptr()) }
912912
}
913913

914914
/// Calculates the distance between two pointers within the same allocation, *where it's known that
915915
/// `self` is equal to or greater than `origin`*. The returned value is in
916916
/// units of **bytes**.
917917
///
918918
/// This is purely a convenience for casting to a `u8` pointer and
919-
/// using [`sub_ptr`][NonNull::sub_ptr] on it. See that method for
919+
/// using [`sub_ptr`][NonNull::offset_from_unsigned] on it. See that method for
920920
/// documentation and safety requirements.
921921
///
922922
/// For non-`Sized` pointees this operation considers only the data pointers,
@@ -925,9 +925,9 @@ impl<T: ?Sized> NonNull<T> {
925925
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
926926
#[stable(feature = "ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
927927
#[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
928-
pub const unsafe fn byte_sub_ptr<U: ?Sized>(self, origin: NonNull<U>) -> usize {
928+
pub const unsafe fn byte_offset_from_unsigned<U: ?Sized>(self, origin: NonNull<U>) -> usize {
929929
// SAFETY: the caller must uphold the safety contract for `byte_sub_ptr`.
930-
unsafe { self.as_ptr().byte_sub_ptr(origin.as_ptr()) }
930+
unsafe { self.as_ptr().byte_offset_from_unsigned(origin.as_ptr()) }
931931
}
932932

933933
/// Reads the value from `self` without moving it. This leaves the

‎core/src/slice/iter/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ macro_rules! len {
5454
// To get rid of some bounds checks (see `position`), we use ptr_sub instead of
5555
// offset_from (Tested by `codegen/slice-position-bounds-check`.)
5656
// SAFETY: by the type invariant pointers are aligned and `start <= end`
57-
unsafe { end.sub_ptr($self.ptr) }
57+
unsafe { end.offset_from_unsigned($self.ptr) }
5858
},
5959
)
6060
}};

‎core/src/slice/raw.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ pub const fn from_mut<T>(s: &mut T) -> &mut [T] {
272272
#[rustc_const_unstable(feature = "const_slice_from_ptr_range", issue = "89792")]
273273
pub const unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
274274
// SAFETY: the caller must uphold the safety contract for `from_ptr_range`.
275-
unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) }
275+
unsafe { from_raw_parts(range.start, range.end.offset_from_unsigned(range.start)) }
276276
}
277277

278278
/// Forms a mutable slice from a pointer range.
@@ -342,5 +342,5 @@ pub const unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
342342
#[rustc_const_unstable(feature = "const_slice_from_mut_ptr_range", issue = "89792")]
343343
pub const unsafe fn from_mut_ptr_range<'a, T>(range: Range<*mut T>) -> &'a mut [T] {
344344
// SAFETY: the caller must uphold the safety contract for `from_mut_ptr_range`.
345-
unsafe { from_raw_parts_mut(range.start, range.end.sub_ptr(range.start)) }
345+
unsafe { from_raw_parts_mut(range.start, range.end.offset_from_unsigned(range.start)) }
346346
}

‎core/src/slice/sort/shared/pivot.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ pub fn choose_pivot<T, F: FnMut(&T, &T) -> bool>(v: &[T], is_less: &mut F) -> us
3131
let c = v_base.add(len_div_8 * 7); // [7*floor(n/8), 8*floor(n/8))
3232

3333
if len < PSEUDO_MEDIAN_REC_THRESHOLD {
34-
median3(&*a, &*b, &*c, is_less).sub_ptr(v_base)
34+
median3(&*a, &*b, &*c, is_less).offset_from_unsigned(v_base)
3535
} else {
36-
median3_rec(a, b, c, len_div_8, is_less).sub_ptr(v_base)
36+
median3_rec(a, b, c, len_div_8, is_less).offset_from_unsigned(v_base)
3737
}
3838
}
3939
}

‎core/src/slice/sort/stable/merge.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl<T> Drop for MergeState<T> {
143143
// leave the input slice `v` with each original element and all possible
144144
// modifications observed.
145145
unsafe {
146-
let len = self.end.sub_ptr(self.start);
146+
let len = self.end.offset_from_unsigned(self.start);
147147
ptr::copy_nonoverlapping(self.start, self.dst, len);
148148
}
149149
}

‎core/src/slice/sort/unstable/quicksort.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ where
224224
left = left.add(1);
225225
}
226226

227-
left.sub_ptr(v_base)
227+
left.offset_from_unsigned(v_base)
228228

229229
// `gap_opt` goes out of scope and overwrites the last wrong-side element on the right side
230230
// with the first wrong-side element of the left side that was initially overwritten by the

0 commit comments

Comments
 (0)