mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-25 22:22:44 +00:00
Rename unsigned_offset_from
to sub_ptr
This commit is contained in:
parent
89a18cb600
commit
e76b3f3b5b
@ -721,7 +721,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
|
||||
let diff_bytes = fx.bcx.ins().isub(ptr, base);
|
||||
// FIXME this can be an exact division.
|
||||
let diff = if intrinsic == sym::ptr_offset_from_unsigned {
|
||||
// Because diff_bytes ULT isize::MAX, this would be fine as signed,
|
||||
// Because diff_bytes ULE isize::MAX, this would be fine as signed,
|
||||
// but unsigned is slightly easier to codegen, so might as well.
|
||||
fx.bcx.ins().udiv_imm(diff_bytes, pointee_size as i64)
|
||||
} else {
|
||||
|
@ -1056,7 +1056,7 @@ where
|
||||
fn drop(&mut self) {
|
||||
// `T` is not a zero-sized type, and these are pointers into a slice's elements.
|
||||
unsafe {
|
||||
let len = self.end.unsigned_offset_from(self.start);
|
||||
let len = self.end.sub_ptr(self.start);
|
||||
ptr::copy_nonoverlapping(self.start, self.dest, len);
|
||||
}
|
||||
}
|
||||
|
@ -163,7 +163,7 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> {
|
||||
// it from the original vec but also avoid creating a &mut to the front since that could
|
||||
// invalidate raw pointers to it which some unsafe code might rely on.
|
||||
let vec_ptr = vec.as_mut().as_mut_ptr();
|
||||
let drop_offset = drop_ptr.unsigned_offset_from(vec_ptr);
|
||||
let drop_offset = drop_ptr.sub_ptr(vec_ptr);
|
||||
let to_drop = ptr::slice_from_raw_parts_mut(vec_ptr.add(drop_offset), drop_len);
|
||||
ptr::drop_in_place(to_drop);
|
||||
}
|
||||
|
@ -250,7 +250,7 @@ where
|
||||
let sink =
|
||||
self.try_fold::<_, _, Result<_, !>>(sink, write_in_place_with_drop(end)).unwrap();
|
||||
// iteration succeeded, don't drop head
|
||||
unsafe { ManuallyDrop::new(sink).dst.unsigned_offset_from(dst_buf) }
|
||||
unsafe { ManuallyDrop::new(sink).dst.sub_ptr(dst_buf) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,7 +10,7 @@ pub(super) struct InPlaceDrop<T> {
|
||||
|
||||
impl<T> InPlaceDrop<T> {
|
||||
fn len(&self) -> usize {
|
||||
unsafe { self.dst.unsigned_offset_from(self.inner) }
|
||||
unsafe { self.dst.sub_ptr(self.inner) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -169,7 +169,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
|
||||
let exact = if mem::size_of::<T>() == 0 {
|
||||
self.end.addr().wrapping_sub(self.ptr.addr())
|
||||
} else {
|
||||
unsafe { self.end.unsigned_offset_from(self.ptr) }
|
||||
unsafe { self.end.sub_ptr(self.ptr) }
|
||||
};
|
||||
(exact, Some(exact))
|
||||
}
|
||||
|
@ -1903,7 +1903,7 @@ extern "rust-intrinsic" {
|
||||
#[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")]
|
||||
pub fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
|
||||
|
||||
/// See documentation of `<*const T>::unsigned_offset_from` for details.
|
||||
/// See documentation of `<*const T>::sub_ptr` for details.
|
||||
#[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")]
|
||||
#[cfg(not(bootstrap))]
|
||||
pub fn ptr_offset_from_unsigned<T>(ptr: *const T, base: *const T) -> usize;
|
||||
|
@ -622,8 +622,20 @@ impl<T: ?Sized> *const T {
|
||||
/// but it provides slightly more information to the optimizer, which can
|
||||
/// sometimes allow it to optimize slightly better with some backends.
|
||||
///
|
||||
/// This method is the inverse of [`add`](#method.add) (and, with the parameters
|
||||
/// in the other order, of [`sub`](#method.sub)).
|
||||
/// This method can be though of as recovering the `count` that was passed
|
||||
/// to [`add`](#method.add) (or, with the parameters in the other order,
|
||||
/// to [`sub`](#method.sub)). The following are all equivalent, assuming
|
||||
/// that their safety preconditions are met:
|
||||
/// ```rust
|
||||
/// # #![feature(ptr_unsigned_offset_from)]
|
||||
/// # unsafe fn blah(ptr: *const i32, origin: *const i32, count: usize) -> bool {
|
||||
/// ptr.sub_ptr(origin) == count
|
||||
/// # &&
|
||||
/// origin.add(count) == ptr
|
||||
/// # &&
|
||||
/// ptr.sub(count) == origin
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
@ -650,10 +662,10 @@ impl<T: ?Sized> *const T {
|
||||
/// let ptr1: *const i32 = &a[1];
|
||||
/// let ptr2: *const i32 = &a[3];
|
||||
/// unsafe {
|
||||
/// assert_eq!(ptr2.unsigned_offset_from(ptr1), 2);
|
||||
/// assert_eq!(ptr2.sub_ptr(ptr1), 2);
|
||||
/// assert_eq!(ptr1.add(2), ptr2);
|
||||
/// assert_eq!(ptr2.sub(2), ptr1);
|
||||
/// assert_eq!(ptr2.unsigned_offset_from(ptr2), 0);
|
||||
/// assert_eq!(ptr2.sub_ptr(ptr2), 0);
|
||||
/// }
|
||||
///
|
||||
/// // This would be incorrect, as the pointers are not correctly ordered:
|
||||
@ -662,7 +674,7 @@ impl<T: ?Sized> *const T {
|
||||
#[unstable(feature = "ptr_unsigned_offset_from", issue = "88888888")]
|
||||
#[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")]
|
||||
#[inline]
|
||||
pub const unsafe fn unsigned_offset_from(self, origin: *const T) -> usize
|
||||
pub const unsafe fn sub_ptr(self, origin: *const T) -> usize
|
||||
where
|
||||
T: Sized,
|
||||
{
|
||||
|
@ -798,8 +798,20 @@ impl<T: ?Sized> *mut T {
|
||||
/// but it provides slightly more information to the optimizer, which can
|
||||
/// sometimes allow it to optimize slightly better with some backends.
|
||||
///
|
||||
/// This method is the inverse of [`add`](#method.add) (and, with the parameters
|
||||
/// in the other order, of [`sub`](#method.sub)).
|
||||
/// This method can be though of as recovering the `count` that was passed
|
||||
/// to [`add`](#method.add) (or, with the parameters in the other order,
|
||||
/// to [`sub`](#method.sub)). The following are all equivalent, assuming
|
||||
/// that their safety preconditions are met:
|
||||
/// ```rust
|
||||
/// # #![feature(ptr_unsigned_offset_from)]
|
||||
/// # unsafe fn blah(ptr: *mut i32, origin: *mut i32, count: usize) -> bool {
|
||||
/// ptr.sub_ptr(origin) == count
|
||||
/// # &&
|
||||
/// origin.add(count) == ptr
|
||||
/// # &&
|
||||
/// ptr.sub(count) == origin
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
@ -828,10 +840,10 @@ impl<T: ?Sized> *mut T {
|
||||
/// let ptr1: *mut i32 = p.add(1);
|
||||
/// let ptr2: *mut i32 = p.add(3);
|
||||
///
|
||||
/// assert_eq!(ptr2.unsigned_offset_from(ptr1), 2);
|
||||
/// assert_eq!(ptr2.sub_ptr(ptr1), 2);
|
||||
/// assert_eq!(ptr1.add(2), ptr2);
|
||||
/// assert_eq!(ptr2.sub(2), ptr1);
|
||||
/// assert_eq!(ptr2.unsigned_offset_from(ptr2), 0);
|
||||
/// assert_eq!(ptr2.sub_ptr(ptr2), 0);
|
||||
/// }
|
||||
///
|
||||
/// // This would be incorrect, as the pointers are not correctly ordered:
|
||||
@ -839,12 +851,12 @@ impl<T: ?Sized> *mut T {
|
||||
#[unstable(feature = "ptr_unsigned_offset_from", issue = "88888888")]
|
||||
#[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")]
|
||||
#[inline]
|
||||
pub const unsafe fn unsigned_offset_from(self, origin: *const T) -> usize
|
||||
pub const unsafe fn sub_ptr(self, origin: *const T) -> usize
|
||||
where
|
||||
T: Sized,
|
||||
{
|
||||
// SAFETY: the caller must uphold the safety contract for `unsigned_offset_from`.
|
||||
unsafe { (self as *const T).unsigned_offset_from(origin) }
|
||||
// SAFETY: the caller must uphold the safety contract for `sub_ptr`.
|
||||
unsafe { (self as *const T).sub_ptr(origin) }
|
||||
}
|
||||
|
||||
/// Calculates the offset from a pointer (convenience for `.offset(count as isize)`).
|
||||
|
@ -215,7 +215,7 @@ pub const fn from_mut<T>(s: &mut T) -> &mut [T] {
|
||||
#[unstable(feature = "slice_from_ptr_range", issue = "89792")]
|
||||
pub unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
|
||||
// SAFETY: the caller must uphold the safety contract for `from_ptr_range`.
|
||||
unsafe { from_raw_parts(range.start, range.end.unsigned_offset_from(range.start)) }
|
||||
unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) }
|
||||
}
|
||||
|
||||
/// 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] {
|
||||
#[unstable(feature = "slice_from_ptr_range", issue = "89792")]
|
||||
pub unsafe fn from_mut_ptr_range<'a, T>(range: Range<*mut T>) -> &'a mut [T] {
|
||||
// SAFETY: the caller must uphold the safety contract for `from_mut_ptr_range`.
|
||||
unsafe { from_raw_parts_mut(range.start, range.end.unsigned_offset_from(range.start)) }
|
||||
unsafe { from_raw_parts_mut(range.start, range.end.sub_ptr(range.start)) }
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ pub const OFFSET_EQUAL_INTS: isize = {
|
||||
pub const OFFSET_UNSIGNED: usize = {
|
||||
let a = ['a', 'b', 'c'];
|
||||
let ptr = a.as_ptr();
|
||||
unsafe { ptr.add(2).unsigned_offset_from(ptr) }
|
||||
unsafe { ptr.add(2).sub_ptr(ptr) }
|
||||
};
|
||||
|
||||
fn main() {
|
||||
|
Loading…
Reference in New Issue
Block a user