Skip to content

Commit 4a097ea

Browse files
committed
Documentation and naming improvements
1 parent 02b5851 commit 4a097ea

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

src/libcore/ptr.rs

+33-15
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ impl<T: ?Sized> *const T {
669669
/// `mem::size_of::<T>()` then the result of the division is rounded towards
670670
/// zero.
671671
///
672-
/// This function returns `None` if `T` is a zero-sized typed.
672+
/// This function returns `None` if `T` is a zero-sized type.
673673
///
674674
/// # Examples
675675
///
@@ -719,7 +719,7 @@ impl<T: ?Sized> *const T {
719719
/// * The distance between the pointers, **in bytes**, cannot overflow an `isize`.
720720
///
721721
/// * The distance between the pointers, in bytes, must be an exact multiple
722-
/// of the size of `T` and `T` must not be a Zero-Sized Type ("ZST").
722+
/// of the size of `T`.
723723
///
724724
/// * The distance being in bounds cannot rely on "wrapping around" the address space.
725725
///
@@ -740,6 +740,10 @@ impl<T: ?Sized> *const T {
740740
/// difficult to satisfy. The only advantage of this method is that it
741741
/// enables more aggressive compiler optimizations.
742742
///
743+
/// # Panics
744+
///
745+
/// This function panics if `T` is a Zero-Sized Type ("ZST").
746+
///
743747
/// # Examples
744748
///
745749
/// Basic usage:
@@ -759,12 +763,14 @@ impl<T: ?Sized> *const T {
759763
/// ```
760764
#[unstable(feature = "ptr_offset_from", issue = "41079")]
761765
#[inline]
762-
pub unsafe fn offset_from(self, other: *const T) -> isize where T: Sized {
766+
pub unsafe fn offset_from(self, origin: *const T) -> isize where T: Sized {
763767
let pointee_size = mem::size_of::<T>();
764768
assert!(0 < pointee_size && pointee_size <= isize::max_value() as usize);
765769

766-
// FIXME: can this be nuw/nsw?
767-
let d = isize::wrapping_sub(self as _, other as _);
770+
// This is the same sequence that Clang emits for pointer subtraction.
771+
// It can be neither `nsw` nor `nuw` because the input is treated as
772+
// unsigned but then the output is treated as signed, so neither works.
773+
let d = isize::wrapping_sub(self as _, origin as _);
768774
intrinsics::exact_div(d, pointee_size as _)
769775
}
770776

@@ -775,9 +781,13 @@ impl<T: ?Sized> *const T {
775781
/// `mem::size_of::<T>()` then the result of the division is rounded towards
776782
/// zero.
777783
///
784+
/// Though this method is safe for any two pointers, note that its result
785+
/// will be mostly useless if the two pointers aren't into the same allocated
786+
/// object, for example if they point to two different local variables.
787+
///
778788
/// # Panics
779789
///
780-
/// This function panics if `T` is a zero-sized typed.
790+
/// This function panics if `T` is a zero-sized type.
781791
///
782792
/// # Examples
783793
///
@@ -800,11 +810,11 @@ impl<T: ?Sized> *const T {
800810
/// ```
801811
#[unstable(feature = "ptr_wrapping_offset_from", issue = "41079")]
802812
#[inline]
803-
pub fn wrapping_offset_from(self, other: *const T) -> isize where T: Sized {
813+
pub fn wrapping_offset_from(self, origin: *const T) -> isize where T: Sized {
804814
let pointee_size = mem::size_of::<T>();
805815
assert!(0 < pointee_size && pointee_size <= isize::max_value() as usize);
806816

807-
let d = isize::wrapping_sub(self as _, other as _);
817+
let d = isize::wrapping_sub(self as _, origin as _);
808818
d.wrapping_div(pointee_size as _)
809819
}
810820

@@ -1424,7 +1434,7 @@ impl<T: ?Sized> *mut T {
14241434
/// `mem::size_of::<T>()` then the result of the division is rounded towards
14251435
/// zero.
14261436
///
1427-
/// This function returns `None` if `T` is a zero-sized typed.
1437+
/// This function returns `None` if `T` is a zero-sized type.
14281438
///
14291439
/// # Examples
14301440
///
@@ -1474,7 +1484,7 @@ impl<T: ?Sized> *mut T {
14741484
/// * The distance between the pointers, **in bytes**, cannot overflow an `isize`.
14751485
///
14761486
/// * The distance between the pointers, in bytes, must be an exact multiple
1477-
/// of the size of `T` and `T` must not be a Zero-Sized Type ("ZST").
1487+
/// of the size of `T`.
14781488
///
14791489
/// * The distance being in bounds cannot rely on "wrapping around" the address space.
14801490
///
@@ -1495,6 +1505,10 @@ impl<T: ?Sized> *mut T {
14951505
/// difficult to satisfy. The only advantage of this method is that it
14961506
/// enables more aggressive compiler optimizations.
14971507
///
1508+
/// # Panics
1509+
///
1510+
/// This function panics if `T` is a Zero-Sized Type ("ZST").
1511+
///
14981512
/// # Examples
14991513
///
15001514
/// Basic usage:
@@ -1514,8 +1528,8 @@ impl<T: ?Sized> *mut T {
15141528
/// ```
15151529
#[unstable(feature = "ptr_offset_from", issue = "41079")]
15161530
#[inline]
1517-
pub unsafe fn offset_from(self, other: *const T) -> isize where T: Sized {
1518-
(self as *const T).offset_from(other)
1531+
pub unsafe fn offset_from(self, origin: *const T) -> isize where T: Sized {
1532+
(self as *const T).offset_from(origin)
15191533
}
15201534

15211535
/// Calculates the distance between two pointers. The returned value is in
@@ -1525,9 +1539,13 @@ impl<T: ?Sized> *mut T {
15251539
/// `mem::size_of::<T>()` then the result of the division is rounded towards
15261540
/// zero.
15271541
///
1542+
/// Though this method is safe for any two pointers, note that its result
1543+
/// will be mostly useless if the two pointers aren't into the same allocated
1544+
/// object, for example if they point to two different local variables.
1545+
///
15281546
/// # Panics
15291547
///
1530-
/// This function panics if `T` is a zero-sized typed.
1548+
/// This function panics if `T` is a zero-sized type.
15311549
///
15321550
/// # Examples
15331551
///
@@ -1550,8 +1568,8 @@ impl<T: ?Sized> *mut T {
15501568
/// ```
15511569
#[unstable(feature = "ptr_wrapping_offset_from", issue = "41079")]
15521570
#[inline]
1553-
pub fn wrapping_offset_from(self, other: *const T) -> isize where T: Sized {
1554-
(self as *const T).wrapping_offset_from(other)
1571+
pub fn wrapping_offset_from(self, origin: *const T) -> isize where T: Sized {
1572+
(self as *const T).wrapping_offset_from(origin)
15551573
}
15561574

15571575
/// Computes the byte offset that needs to be applied in order to

0 commit comments

Comments
 (0)